-
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 #1451 from gtech-mulearn/dev
channel and affiliation confilit fixed
- Loading branch information
Showing
11 changed files
with
294 additions
and
232 deletions.
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 |
---|---|---|
@@ -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() |
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 |
---|---|---|
@@ -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 |
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
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() |
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,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 |
Oops, something went wrong.