Skip to content

Commit

Permalink
Merge pull request #1683 from gtech-mulearn/dev
Browse files Browse the repository at this point in the history
events crud
  • Loading branch information
adnankattekaden authored Dec 1, 2023
2 parents 78acfc6 + 8ebef05 commit afbb7c4
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 1 deletion.
36 changes: 36 additions & 0 deletions api/dashboard/events/events_serializer.py
Original file line number Diff line number Diff line change
@@ -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
98 changes: 98 additions & 0 deletions api/dashboard/events/events_views.py
Original file line number Diff line number Diff line change
@@ -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()
9 changes: 9 additions & 0 deletions api/dashboard/events/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.urls import path

from . import events_views

urlpatterns = [
path('', events_views.EventAPI.as_view()),
path('<str:event_id>/', events_views.EventAPI.as_view()),

]
3 changes: 2 additions & 1 deletion api/dashboard/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'))

]
15 changes: 15 additions & 0 deletions db/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit afbb7c4

Please sign in to comment.