HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: /var/www/html/calendar-planning/app/views.py
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from app.serializers import EventSerializer
from app.models import Event, Category
from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt
from django.utils.decorators import method_decorator
from app.utils.generate_events_from_openai import event_generator

@method_decorator(csrf_exempt, name='dispatch')
class GenerateResponseView(APIView):
    def post(self, request):
        print(request.data)
        serializer = EventSerializer(data=request.data)

        if serializer.is_valid():
            validated_data = serializer.validated_data
            print("Validated data:", validated_data)

            # Retrieve or create Category objects
            category_names = validated_data.pop('categories', [])
            categories = [
                Category.objects.get_or_create(name=name.strip())[0]
                for name in category_names
            ]

            # Create the Event object
            event = Event.objects.create(
                country=validated_data["country"],
                year=validated_data["year"],
                gpt_response=[],
            )
            event.categories.set(categories)

            # Generate response data
            response_serializer = EventSerializer(event)
            # print(response_serializer.data)
            country = response_serializer.data["country"]
            year = response_serializer.data["year"]
            categories_data = []
            for _ in response_serializer.data["category_objects"]:
                categories_data.append(_["name"])
            
            # print("categories : ",categories_data)
            # print("country : ",country)
            # print("year : ",year)
            try:
                response_data = event_generator(categories=categories_data,year=year,country=country)
                return Response(
                    {
                        "success": True,
                        "status": status.HTTP_200_OK,
                        "message": "Response Created",
                        "data": response_data,
                    },
                    status=status.HTTP_200_OK,
                )
            except Exception as e:
                return Response(
                {
                    "success": False,
                    "status": status.HTTP_500_INTERNAL_SERVER_ERROR,
                    "errors": f"Error Occured : {str(e)}",
                },
                status=status.HTTP_500_INTERNAL_SERVER_ERROR
            )
        return Response(
            {
                "success": False,
                "status": status.HTTP_400_BAD_REQUEST,
                "errors": serializer.errors,
            },
            status=status.HTTP_400_BAD_REQUEST,
        )

def index(request):
    return render(request, 'api/index.html')