-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1683 from gtech-mulearn/dev
events crud
- Loading branch information
Showing
5 changed files
with
160 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()), | ||
|
||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters