File size: 1,213 Bytes
fe9d3dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from django.http import JsonResponse
from django.views import View
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt
from rest_framework.parsers import JSONParser
from .serializers import ItemSerializer
from .logic import main_detection
from .models import ImageUrl
import json

@method_decorator(csrf_exempt, name='dispatch')
class StatusView(View):
    def get(self, request):
        return JsonResponse({"status": "AI Server is running"}, status=200)

@method_decorator(csrf_exempt, name='dispatch')
class TechView(View):
    async def post(self, request):
        try:
            data = json.loads(request.body.decode('utf-8'))  # Correct way to parse JSON data
        except json.JSONDecodeError:
            return JsonResponse({'error': 'Invalid JSON'}, status=400)

        serializer = ItemSerializer(data=data)
        if serializer.is_valid():
            # Assuming `main_detection` is an asynchronous function you've set up
            result = await main_detection(serializer.validated_data['url'])
            return JsonResponse(result, safe=False, status=200)
        else:
            return JsonResponse(serializer.errors, status=400)