From 509543a4635bcad609adecb3835293a79f39f351 Mon Sep 17 00:00:00 2001 From: Ajay Vishnu E Date: Sat, 2 Dec 2023 01:17:53 +0530 Subject: [PATCH] events table and crud --- api/dashboard/events/events_serializer.py | 36 +++++++++ api/dashboard/events/events_views.py | 98 +++++++++++++++++++++++ api/dashboard/events/urls.py | 9 +++ api/dashboard/urls.py | 3 +- db/task.py | 15 ++++ 5 files changed, 160 insertions(+), 1 deletion(-) create mode 100644 api/dashboard/events/events_serializer.py create mode 100644 api/dashboard/events/events_views.py create mode 100644 api/dashboard/events/urls.py diff --git a/api/dashboard/events/events_serializer.py b/api/dashboard/events/events_serializer.py new file mode 100644 index 00000000..bdf18e82 --- /dev/null +++ b/api/dashboard/events/events_serializer.py @@ -0,0 +1,36 @@ +import uuid +from rest_framework import serializers +from db.task import Events +from utils.utils import DateTimeUtils + + +class EventsListSerializer(serializers.ModelSerializer): + created_by = serializers.CharField(source="created_by.fullname") + updated_by = serializers.CharField(source="updated_by.fullname") + + class Meta: + model = Events + fields = "__all__" + +class EventsCUDSerializer(serializers.ModelSerializer): + class Meta: + model = Events + fields = ['name'] + + def create(self, validated_data): + user_id = self.context.get("user_id") + + validated_data["created_by_id"] = user_id + validated_data["updated_by_id"] = user_id + validated_data["id"] = uuid.uuid4() + return Events.objects.create(**validated_data) + + def update(self, instance, validated_data): + user_id = self.context.get("user_id") + + instance.name = validated_data.get("name", instance.name) + instance.updated_by_id = user_id + instance.updated_at = DateTimeUtils.get_current_utc_time() + instance.save() + + return instance \ No newline at end of file diff --git a/api/dashboard/events/events_views.py b/api/dashboard/events/events_views.py new file mode 100644 index 00000000..bf34a4d0 --- /dev/null +++ b/api/dashboard/events/events_views.py @@ -0,0 +1,98 @@ +from rest_framework.views import APIView +from utils.permission import CustomizePermission, JWTUtils, role_required +from utils.response import CustomResponse +from utils.types import RoleType +from utils.utils import CommonUtils +from .events_serializer import EventsCUDSerializer, EventsListSerializer +from db.task import Events + + +class EventAPI(APIView): + authentication_classes = [CustomizePermission] + + def get(self, request): + events = Events.objects.all() + paginated_queryset = CommonUtils.get_paginated_queryset( + events, + request, + ['id', 'name'] + ) + + serializer = EventsListSerializer( + paginated_queryset.get("queryset"), + many=True + ) + + return CustomResponse().paginated_response( + data=serializer.data, + pagination=paginated_queryset.get( + "pagination" + ) + ) + + + def post(self, request): + user_id = JWTUtils.fetch_user_id(request) + + serializer = EventsCUDSerializer( + data=request.data, + context={ + "user_id": user_id, + } + ) + + if serializer.is_valid(): + serializer.save() + + return CustomResponse( + general_message=f"{request.data.get('name')} Event created successfully", + response=serializer.data + ).get_success_response() + + return CustomResponse( + general_message=serializer.errors, + ).get_failure_response() + + + def put(self, request, event_id): + user_id = JWTUtils.fetch_user_id(request) + + events = Events.objects.filter(id=event_id).first() + + if events is None: + return CustomResponse( + general_message="Invalid Event id" + ).get_failure_response() + + serializer = EventsCUDSerializer( + events, + data=request.data, + context={"user_id": user_id} + ) + + if serializer.is_valid(): + serializer.save() + + return CustomResponse( + general_message=f"{events.name} Edited Successfully" + ).get_success_response() + + return CustomResponse( + message=serializer.errors + ).get_failure_response() + + + def delete(self, request, event_id): + + events = Events.objects.filter(id=event_id).first() + + if events is None: + return CustomResponse( + general_message="Invalid event id" + ).get_failure_response() + + events.delete() + + return CustomResponse( + general_message=f"{events.name} Deleted Successfully" + ).get_success_response() \ No newline at end of file diff --git a/api/dashboard/events/urls.py b/api/dashboard/events/urls.py new file mode 100644 index 00000000..c9c36f2d --- /dev/null +++ b/api/dashboard/events/urls.py @@ -0,0 +1,9 @@ +from django.urls import path + +from . import events_views + +urlpatterns = [ + path('', events_views.EventAPI.as_view()), + path('/', events_views.EventAPI.as_view()), + +] \ No newline at end of file diff --git a/api/dashboard/urls.py b/api/dashboard/urls.py index 7632000b..797c97a2 100644 --- a/api/dashboard/urls.py +++ b/api/dashboard/urls.py @@ -20,6 +20,7 @@ path('error-log/', include('api.dashboard.error_log.urls')), path('affiliation/',include('api.dashboard.affiliation.urls')), path('channels/',include('api.dashboard.channels.urls')), - path('discord-moderator/',include('api.dashboard.discord_moderator.urls')) + path('discord-moderator/',include('api.dashboard.discord_moderator.urls')), + path('events/',include('api.dashboard.events.urls')) ] diff --git a/db/task.py b/db/task.py index 729fdc29..6a4f594b 100644 --- a/db/task.py +++ b/db/task.py @@ -238,3 +238,18 @@ class VoucherLog(models.Model): class Meta: managed = False db_table = "voucher_log" + + +class Events(models.Model): + id = models.CharField(primary_key=True, max_length=36, default=uuid.uuid4) + name = models.CharField(max_length=75) + updated_by = models.ForeignKey(User, on_delete=models.CASCADE, db_column="updated_by", + related_name="event_updated_by") + updated_at = models.DateTimeField(auto_now=True) + created_by = models.ForeignKey(User, on_delete=models.CASCADE, db_column="created_by", + related_name="event_created_by") + created_at = models.DateTimeField(auto_now_add=True) + + class Meta: + managed = False + db_table = "events"