Skip to content

Commit

Permalink
Merge pull request #1451 from gtech-mulearn/dev
Browse files Browse the repository at this point in the history
channel and affiliation confilit fixed
  • Loading branch information
adnankattekaden authored Oct 29, 2023
2 parents 0a7eb6d + 97acc50 commit 50f5cbc
Show file tree
Hide file tree
Showing 11 changed files with 294 additions and 232 deletions.
148 changes: 89 additions & 59 deletions api/dashboard/affiliation/affiliation_views.py
Original file line number Diff line number Diff line change
@@ -1,77 +1,107 @@
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.permission import CustomizePermission, JWTUtils
from utils.permission import role_required
from utils.response import CustomResponse
from utils.types import RoleType
from utils.utils import CommonUtils
from .serializers import AffiliationCUDSerializer, AffiliationListSerializer

class AffiliationCRUDAPI(APIView):
authentication_classes = [CustomizePermission]

def get(self, request):

affiliation = OrgAffiliation.objects.all()
paginated_queryset = CommonUtils.get_paginated_queryset(
affiliation,
request,
['id', 'title']

)

class AffiliationView(APIView):
authentication_classes = [CustomizePermission]
serializer = AffiliationListSerializer(
paginated_queryset.get("queryset"),
many=True
)

return CustomResponse().paginated_response(
data=serializer.data,
pagination=paginated_queryset.get(
"pagination"
)
)

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})
@role_required([RoleType.ADMIN.value])
def post(self, request):

user_id = JWTUtils.fetch_user_id(request)

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

if serializer.is_valid():
serializer.save()

return CustomResponse(
general_message=f"{request.data.get('title')} Affiliation created successfully",
response=serializer.data
).get_success_response()

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

return CustomResponse(
general_message=serial_.errors
).get_failure_response()
@role_required([RoleType.ADMIN.value])
def put(self, request, affiliation_id):

@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()
user_id = JWTUtils.fetch_user_id(request)

if affiliation is None:
affiliation = OrgAffiliation.objects.filter(
id=affiliation_id
).first()

if affiliation is None:
return CustomResponse(
general_message="Invalid affiliation"
general_message="Invalid affiliation id"
).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:
serializer = AffiliationCUDSerializer(
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):

affiliation = OrgAffiliation.objects.filter(
id=affiliation_id
).first()

if affiliation is None:
return CustomResponse(
general_message="Invalid affiliation"
general_message="Invalid affiliation id"
).get_failure_response()
affiliation.delete()
return CustomResponse(
general_message=f"{affiliation.title} Deleted Successfully"
).get_success_response()

affiliation.delete()

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

class AffiliationReadSerializer(serializers.ModelSerializer):
from rest_framework import serializers

from db.organization import OrgAffiliation
from utils.utils import DateTimeUtils

title = serializers.ReadOnlyField()
affiliation_id = serializers.ReadOnlyField(source='id')
class AffiliationListSerializer(serializers.ModelSerializer):
created_by = serializers.CharField(source="created_by.fullname")
updated_by = serializers.CharField(source="updated_by.fullname")

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

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

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()
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
user_id = self.context.get("user_id")

instance.title = validated_data.get("title", instance.title)
instance.updated_by_id = user_id
instance.updated_at = DateTimeUtils.get_current_utc_time()
instance.save()

return instance
8 changes: 3 additions & 5 deletions api/dashboard/affiliation/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
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())
]
path("", affiliation_views.AffiliationCRUDAPI.as_view()),
path("<str:affiliation_id>/", affiliation_views.AffiliationCRUDAPI.as_view()),
]
106 changes: 106 additions & 0 deletions api/dashboard/channel/channel_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
from rest_framework.views import APIView

from db.task import Channel
from utils.permission import CustomizePermission, JWTUtils
from utils.permission import role_required
from utils.response import CustomResponse
from utils.types import RoleType
from utils.utils import CommonUtils
from .serializers import ChannelCUDSerializer, ChannelListSerializer

class ChannelCRUDAPI(APIView):
authentication_classes = [CustomizePermission]

def get(self, request):

channel = Channel.objects.all()
paginated_queryset = CommonUtils.get_paginated_queryset(
channel,
request,
['id', 'name', 'discord_id']
)

serializer = ChannelListSerializer(
paginated_queryset.get("queryset"),
many=True
)

return CustomResponse().paginated_response(
data=serializer.data,
pagination=paginated_queryset.get(
"pagination"
)
)

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

user_id = JWTUtils.fetch_user_id(request)

serializer = ChannelCUDSerializer(
data=request.data,
context={
"user_id": user_id,
}
)

if serializer.is_valid():
serializer.save()

return CustomResponse(
general_message=f"{request.data.get('name')} Channel created successfully",
response=serializer.data
).get_success_response()

return CustomResponse(
general_message=serializer.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 id"
).get_failure_response()

serializer = ChannelCUDSerializer(
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):

channel = Channel.objects.filter(
id=channel_id
).first()

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

channel.delete()

return CustomResponse(
general_message=f"{channel.name} Deleted Successfully"
).get_success_response()
41 changes: 41 additions & 0 deletions api/dashboard/channel/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import uuid

from rest_framework import serializers

from db.task import Channel
from utils.utils import DateTimeUtils


class ChannelListSerializer(serializers.ModelSerializer):
created_by = serializers.CharField(source="created_by.fullname")
updated_by = serializers.CharField(source="updated_by.fullname")

class Meta:
model = Channel
fields = "__all__"


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

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 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.discord_id)
instance.updated_by_id = user_id
instance.updated_at = DateTimeUtils.get_current_utc_time()
instance.save()

return instance
Loading

0 comments on commit 50f5cbc

Please sign in to comment.