-
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 #1413 from gtech-mulearn/dev
Devserv
- Loading branch information
Showing
10 changed files
with
987 additions
and
5 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 |
---|---|---|
@@ -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() |
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,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 |
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,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()) | ||
] |
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,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() |
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,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 |
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,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()) | ||
] |
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 |
---|---|---|
|
@@ -41,4 +41,4 @@ class Meta: | |
"icon", | ||
"created_by", | ||
"updated_by" | ||
] | ||
] |
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
Oops, something went wrong.