Skip to content

Commit

Permalink
Merge pull request #1395 from aswanthabam/dev
Browse files Browse the repository at this point in the history
Added Channel and OrgAffiliation CRUD operations
  • Loading branch information
Aashish Vinu authored Oct 24, 2023
2 parents 3da11e7 + d9b7730 commit 2324de6
Show file tree
Hide file tree
Showing 8 changed files with 979 additions and 0 deletions.
77 changes: 77 additions & 0 deletions api/dashboard/affiliation/affiliation_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
from rest_framework.views import APIView
from utils.permission import CustomizePermission,role_required,JWTUtils
from utils.types import RoleType
from .serializers import AffiliationReadSerializer, AffiliationCreateUpdateSerializer
from db.organization import OrgAffiliation
from utils.response import CustomResponse
from utils.utils import CommonUtils

class AffiliationView(APIView):
authentication_classes = [CustomizePermission]


def get(self,request):
all_affiliations = OrgAffiliation.objects.all()
pager_ = CommonUtils.get_paginated_queryset(all_affiliations,request,['title'],sort_fields={'created':'created_at','updated':'updated_at','title':'title'})
serial_ = AffiliationReadSerializer(pager_.get('queryset'),many=True)
# serial_.is_valid()
return CustomResponse().paginated_response(data=serial_.data,pagination=pager_.get("pagination"))

@role_required([RoleType.ADMIN.value])
def post(self,request):
user_id = JWTUtils.fetch_user_id(request)

serial_ = AffiliationCreateUpdateSerializer(data=request.data,context={'user_id':user_id})

if serial_.is_valid():
serial_.save()

return CustomResponse(
general_message=f"{request.data.get('title')} added successfully"
).get_success_response()

return CustomResponse(
general_message=serial_.errors
).get_failure_response()

@role_required([RoleType.ADMIN.value])
def put(self,request,affiliation_id):
user_id = JWTUtils.fetch_user_id(request)
affiliation = OrgAffiliation.objects.filter(id=affiliation_id).first()

if affiliation is None:
return CustomResponse(
general_message="Invalid affiliation"
).get_failure_response()

serializer = AffiliationCreateUpdateSerializer(
affiliation,
data=request.data,
context={
"user_id": user_id
}
)
if serializer.is_valid():
serializer.save()

return CustomResponse(
general_message=f"{affiliation.title} Edited Successfully"
).get_success_response()

return CustomResponse(
message=serializer.errors
).get_failure_response()

@role_required([RoleType.ADMIN.value])
def delete(self,request,affiliation_id):
user_id = JWTUtils.fetch_user_id(request)
affiliation = OrgAffiliation.objects.filter(id=affiliation_id).first()

if affiliation is None:
return CustomResponse(
general_message="Invalid affiliation"
).get_failure_response()
affiliation.delete()
return CustomResponse(
general_message=f"{affiliation.title} Deleted Successfully"
).get_success_response()
37 changes: 37 additions & 0 deletions api/dashboard/affiliation/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from rest_framework import serializers
from db.organization import OrgAffiliation
import uuid

class AffiliationReadSerializer(serializers.ModelSerializer):

title = serializers.ReadOnlyField()
affiliation_id = serializers.ReadOnlyField(source='id')

class Meta:
model = OrgAffiliation
fields = [
"title",
"affiliation_id"
]

class AffiliationCreateUpdateSerializer(serializers.ModelSerializer):
class Meta:
model = OrgAffiliation
fields = [
"title"
]

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 OrgAffiliation.objects.create(**validated_data)

def update(self, instance, validated_data):
user_id = self.context.get('user_id')
instance.title = validated_data.get('title', instance.title)
instance.updated_by_id = user_id

instance.save()
return instance
10 changes: 10 additions & 0 deletions api/dashboard/affiliation/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django.urls import path

from . import affiliation_views

urlpatterns = [
path('',affiliation_views.AffiliationView.as_view()),
path('create/',affiliation_views.AffiliationView.as_view()),
path('edit/<str:affiliation_id>/',affiliation_views.AffiliationView.as_view()),
path('delete/<str:affiliation_id>/',affiliation_views.AffiliationView.as_view())
]
76 changes: 76 additions & 0 deletions api/dashboard/channels/channel_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
from rest_framework.views import APIView
from utils.permission import CustomizePermission,role_required,JWTUtils
from utils.types import RoleType
from .serializers import ChannelReadSerializer,ChannelCreateUpdateSerializer
from db.task import Channel
from utils.response import CustomResponse
from utils.utils import CommonUtils

class ChannelView(APIView):
authentication_classes = [CustomizePermission]

def get(self,request):
all_channels = Channel.objects.all()
pager_ = CommonUtils.get_paginated_queryset(all_channels,request,['name','discord_id','id'],sort_fields={'created':'created_at','updated':'updated_at','name':'name'})
serial_ = ChannelReadSerializer(pager_.get("queryset"),many=True)
# serial_.is_valid()
return CustomResponse().paginated_response(data=serial_.data,pagination=pager_.get("pagination"))

@role_required([RoleType.ADMIN.value])
def post(self,request):
user_id = JWTUtils.fetch_user_id(request)

serial_ = ChannelCreateUpdateSerializer(data=request.data,context={'user_id':user_id})

if serial_.is_valid():
serial_.save()

return CustomResponse(
general_message=f"{request.data.get('name')} added successfully"
).get_success_response()

return CustomResponse(
general_message=serial_.errors
).get_failure_response()

@role_required([RoleType.ADMIN.value])
def put(self,request,channel_id):
user_id = JWTUtils.fetch_user_id(request)
channel = Channel.objects.filter(id=channel_id).first()

if channel is None:
return CustomResponse(
general_message="Invalid channel"
).get_failure_response()

serializer = ChannelCreateUpdateSerializer(
channel,
data=request.data,
context={
"user_id": user_id
}
)
if serializer.is_valid():
serializer.save()

return CustomResponse(
general_message=f"{channel.name} Edited Successfully"
).get_success_response()

return CustomResponse(
message=serializer.errors
).get_failure_response()

@role_required([RoleType.ADMIN.value])
def delete(self,request,channel_id):
user_id = JWTUtils.fetch_user_id(request)
channel = Channel.objects.filter(id=channel_id).first()

if channel is None:
return CustomResponse(
general_message="Invalid channel"
).get_failure_response()
channel.delete()
return CustomResponse(
general_message=f"{channel.name} Deleted Successfully"
).get_success_response()
48 changes: 48 additions & 0 deletions api/dashboard/channels/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from rest_framework import serializers
from db.task import Channel
from django.utils import timezone
from utils.utils import DateTimeUtils
import uuid
class ChannelReadSerializer(serializers.ModelSerializer):

name = serializers.ReadOnlyField()
discord_id = serializers.ReadOnlyField()
channel_id = serializers.ReadOnlyField(source='id')
updated_at = serializers.ReadOnlyField()
created_at = serializers.ReadOnlyField()

class Meta:
model = Channel
fields = [
"name",
"channel_id",
"discord_id",
"created_at",
"updated_at"
]

class ChannelCreateUpdateSerializer(serializers.ModelSerializer):
class Meta:
model = Channel
fields = [
"name",
"discord_id"
]

def create(self, validated_data):
user_id = self.context.get('user_id')
validated_data['id'] = uuid.uuid4()
validated_data['created_by_id'] = user_id
validated_data['updated_by_id'] = user_id
validated_data['created_at'] = DateTimeUtils.get_current_utc_time()
validated_data['updated_at'] = DateTimeUtils.get_current_utc_time()
return Channel.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.discord_id = validated_data.get('discord_id', instance.name)
instance.updated_by_id = user_id
instance.updated_at = DateTimeUtils.get_current_utc_time()
instance.save()
return instance
10 changes: 10 additions & 0 deletions api/dashboard/channels/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django.urls import path

from . import channel_views

urlpatterns = [
path('',channel_views.ChannelView.as_view()),
path('create/',channel_views.ChannelView.as_view()),
path('edit/<str:channel_id>/',channel_views.ChannelView.as_view()),
path('delete/<str:channel_id>/',channel_views.ChannelView.as_view())
]
2 changes: 2 additions & 0 deletions api/dashboard/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@
path('organisation/', include('api.dashboard.organisation.urls')),
path('dynamic-management/', include('api.dashboard.dynamic_management.urls')),
path('error-log/', include('api.dashboard.error_log.urls')),
path('affiliation/',include('api.dashboard.affiliation.urls')),
path('channels/',include('api.dashboard.channels.urls'))
]
Loading

0 comments on commit 2324de6

Please sign in to comment.