Skip to content

Commit

Permalink
Merge pull request #1410 from gtech-mulearn:dev
Browse files Browse the repository at this point in the history
[STYLE, PATCH] Interest Group Patch, DB style
  • Loading branch information
MZaFaRM authored Oct 23, 2023
2 parents 11a1dc9 + e28c56b commit 72eae39
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 213 deletions.
52 changes: 4 additions & 48 deletions api/dashboard/ig/dash_ig_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,56 +33,12 @@ def get_user_ig_link_ig(self, obj):

class InterestGroupCreateUpdateSerializer(serializers.ModelSerializer):

name = serializers.CharField(required=True)
icon = serializers.CharField(required=True)
code = serializers.CharField(required=True)

class Meta:
model = InterestGroup
fields = [
"name",
"code",
"icon"
]

def create(self, validated_data):
user_id = self.context.get('user_id')

validated_data['id'] = str(uuid.uuid4())
validated_data['created_by_id'] = user_id
validated_data['updated_by_id'] = user_id

return InterestGroup.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.code = validated_data.get('code', instance.code)
instance.icon = validated_data.get('icon', instance.icon)
instance.updated_by_id = user_id
instance.updated_by = DateTimeUtils.get_current_utc_time()

instance.save()
return instance

def validate_name(self, name):
ig_obj = InterestGroup.objects.filter(name=name).first()
if ig_obj:
raise serializers.ValidationError("interest group with name is already exists")
if len(name) >= 75:
raise serializers.ValidationError("name should contain only 75 characters")
return name

def validate_code(self, code):
ig_obj = InterestGroup.objects.filter(code=code).first()
if ig_obj:
raise serializers.ValidationError("interest group with code is already exists")
if len(code) >= 10:
raise serializers.ValidationError("name should contain only 75 characters")
return code

def validate_icon(self, icon):
if len(icon) >= 75:
raise serializers.ValidationError("name should contain only 75 characters")
return icon
"icon",
"created_by",
"updated_by"
]
116 changes: 34 additions & 82 deletions api/dashboard/ig/dash_ig_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from utils.utils import CommonUtils, DiscordWebhooks
from .dash_ig_serializer import (
InterestGroupSerializer,
InterestGroupCreateUpdateSerializer
InterestGroupCreateUpdateSerializer,
)


Expand All @@ -17,14 +17,10 @@ class InterestGroupAPI(APIView):

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

ig_queryset = (
InterestGroup.objects.select_related(
"created_by",
"updated_by"
).prefetch_related(
"user_ig_link_ig"
).all()
InterestGroup.objects.select_related("created_by", "updated_by")
.prefetch_related("user_ig_link_ig")
.all()
)
paginated_queryset = CommonUtils.get_paginated_queryset(
ig_queryset,
Expand All @@ -41,31 +37,26 @@ def get(self, request):
"updated_on": "updated_at",
"updated_by": "updated_by",
"created_on": "created_at",
"created_by": "created_by"
"created_by": "created_by",
},
)

ig_serializer_data = InterestGroupSerializer(
paginated_queryset.get(
"queryset"
),
many=True
paginated_queryset.get("queryset"), many=True
).data

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

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

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

if serializer.is_valid():
Expand All @@ -74,66 +65,50 @@ def post(self, request):
DiscordWebhooks.general_updates(
WebHookCategory.INTEREST_GROUP.value,
WebHookActions.CREATE.value,
request.data.get("name"),
request_data.get("name"),
)

return CustomResponse(
response={
"interestGroup": serializer.data
}
response={"interestGroup": serializer.data}
).get_success_response()

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

@role_required([RoleType.ADMIN.value])
def put(self, request, pk):
user_id = JWTUtils.fetch_user_id(request)
ig = InterestGroup.objects.get(id=pk)
ig_old_name = ig.name

ig_old_name = InterestGroup.objects.get(
id=pk
).name
request_data = request.data
request_data["updated_by"] = user_id

serializer = InterestGroupCreateUpdateSerializer(
data=request.data,
instance=InterestGroup.objects.get(
id=pk
),
context={
"user_id": user_id
},
data=request_data, instance=ig, partial=True
)

if serializer.is_valid():
serializer.save()
ig_new_name = ig.name

DiscordWebhooks.general_updates(
WebHookCategory.INTEREST_GROUP.value,
WebHookActions.EDIT.value,
InterestGroup.objects.get(id=pk).name,
ig_new_name,
ig_old_name,
)
return CustomResponse(
response={
"interestGroup": serializer.data
}
response={"interestGroup": serializer.data}
).get_success_response()

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

@role_required([RoleType.ADMIN.value])
def delete(self, request, pk):
ig = InterestGroup.objects.filter(
id=pk
).first()
ig = InterestGroup.objects.filter(id=pk).first()

if ig is None:
return CustomResponse(
general_message="invalid ig"
).get_success_response()
return CustomResponse(general_message="invalid ig").get_success_response()

ig.delete()

Expand All @@ -152,65 +127,42 @@ class InterestGroupCSV(APIView):

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

ig_serializer = (
InterestGroup.objects.select_related(
"created_by",
"updated_by"
).prefetch_related(
"user_ig_link_ig"
).all()
InterestGroup.objects.select_related("created_by", "updated_by")
.prefetch_related("user_ig_link_ig")
.all()
)

ig_serializer_data = InterestGroupSerializer(
ig_serializer,
many=True
).data
ig_serializer_data = InterestGroupSerializer(ig_serializer, many=True).data

return CommonUtils.generate_csv(
ig_serializer_data,
"Interest Group"
)
return CommonUtils.generate_csv(ig_serializer_data, "Interest Group")


class InterestGroupGetAPI(APIView):
authentication_classes = [CustomizePermission]

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

ig_data = InterestGroup.objects.filter(
id=pk
).first()
ig_data = InterestGroup.objects.filter(id=pk).first()

if not ig_data:
return CustomResponse(
general_message="Interest Group Does Not Exist"
).get_failure_response()

serializer = InterestGroupSerializer(
ig_data,
many=False
)
serializer = InterestGroupSerializer(ig_data, many=False)

return CustomResponse(
response={
"interestGroup": serializer.data
}
response={"interestGroup": serializer.data}
).get_success_response()


class InterestGroupListApi(APIView):
def get(self, request):
ig = InterestGroup.objects.all()

serializer = InterestGroupSerializer(
ig,
many=True
)
serializer = InterestGroupSerializer(ig, many=True)

return CustomResponse(
response={
"interestGroup": serializer.data
}
response={"interestGroup": serializer.data}
).get_success_response()
16 changes: 8 additions & 8 deletions db/hackathon.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ class Hackathon(models.Model):
event_end = models.DateTimeField(blank=True, null=True)
status = models.CharField(max_length=20, blank=True, null=True)
updated_by = models.ForeignKey(User, on_delete=models.CASCADE, db_column='updated_by', related_name='hackathon_updated_by')
updated_at = models.DateTimeField()
updated_at = models.DateTimeField(auto_now=True)
created_by = models.ForeignKey(User, on_delete=models.CASCADE, db_column='created_by', related_name='hackathon_created_by')
created_at = models.DateTimeField()
created_at = models.DateTimeField(auto_now_add=True)

class Meta:
managed = False
Expand All @@ -41,9 +41,9 @@ class HackathonForm(models.Model):
field_type = models.CharField(max_length=50)
is_required = models.BooleanField(default=False)
updated_by = models.ForeignKey(User, on_delete=models.CASCADE, db_column='updated_by', related_name='hackathon_form_updated_by')
updated_at = models.DateTimeField()
updated_at = models.DateTimeField(auto_now=True)
created_by = models.ForeignKey(User, on_delete=models.CASCADE, db_column='created_by', related_name='hackathon_form_created_by')
created_at = models.DateTimeField()
created_at = models.DateTimeField(auto_now_add=True)

class Meta:
managed = False
Expand All @@ -55,9 +55,9 @@ class HackathonOrganiserLink(models.Model):
organiser = models.ForeignKey(User, on_delete=models.CASCADE)
hackathon = models.ForeignKey(Hackathon, on_delete=models.CASCADE)
updated_by = models.ForeignKey(User, on_delete=models.CASCADE, db_column='updated_by', related_name='hackathon_organiser_link_updated_by')
updated_at = models.DateTimeField()
updated_at = models.DateTimeField(auto_now=True)
created_by = models.ForeignKey(User, on_delete=models.CASCADE, db_column='created_by', related_name='hackathon_organiser_link_created_by')
created_at = models.DateTimeField()
created_at = models.DateTimeField(auto_now_add=True)

class Meta:
managed = False
Expand All @@ -69,9 +69,9 @@ class HackathonUserSubmission(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
hackathon = models.ForeignKey(Hackathon, on_delete=models.CASCADE)
updated_by = models.ForeignKey(User, on_delete=models.CASCADE, db_column='updated_by', related_name='hackathon_submission_updated_by')
updated_at = models.DateTimeField()
updated_at = models.DateTimeField(auto_now=True)
created_by = models.ForeignKey(User, on_delete=models.CASCADE, db_column='created_by', related_name='hackathon_submission_created_by')
created_at = models.DateTimeField()
created_at = models.DateTimeField(auto_now_add=True)
data = models.JSONField(max_length=2000, blank=True, null=True)

class Meta:
Expand Down
4 changes: 2 additions & 2 deletions db/integrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class Meta:
#
class IntegrationAuthorization(models.Model):
id = models.CharField(max_length=36, primary_key=True, default=uuid.uuid4)
integration = models.ForeignKey(Integration,on_delete=models.CASCADE,null=False,related_name="integration_authorization_integration",)
user = models.ForeignKey(User,on_delete=models.CASCADE,related_name="integration_authorization_user",null=False,)
integration = models.ForeignKey(Integration, on_delete=models.CASCADE, null=False, related_name="integration_authorization_integration",)
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="integration_authorization_user", null=False,)
integration_value = models.CharField(max_length=255, unique=True, null=False)
additional_field = models.CharField(max_length=255)
verified = models.BooleanField(default=False, null=False)
Expand Down
10 changes: 5 additions & 5 deletions db/learning_circle.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ class LearningCircle(models.Model):
meet_time = models.CharField(max_length=10, blank=True, null=True)
day = models.CharField(max_length=20, blank=True, null=True)
note = models.CharField(max_length=500, blank=True, null=True)
updated_by = models.ForeignKey(User, on_delete=models.CASCADE, db_column="updated_by",related_name="learning_circle_updated_by")
updated_at = models.DateTimeField()
created_by = models.ForeignKey(User, on_delete=models.CASCADE, db_column="created_by",related_name="learning_circle_created_by")
created_at = models.DateTimeField()
updated_by = models.ForeignKey(User, on_delete=models.CASCADE, db_column="updated_by", related_name="learning_circle_updated_by")
updated_at = models.DateTimeField(auto_now=True)
created_by = models.ForeignKey(User, on_delete=models.CASCADE, db_column="created_by", related_name="learning_circle_created_by")
created_at = models.DateTimeField(auto_now_add=True)

class Meta:
managed = False
Expand All @@ -32,7 +32,7 @@ class UserCircleLink(models.Model):
is_invited = models.BooleanField(default=False)
accepted = models.BooleanField()
accepted_at= models.DateTimeField(blank=True, null=True)
created_at = models.DateTimeField()
created_at = models.DateTimeField(auto_now_add=True)

class Meta:
managed = False
Expand Down
2 changes: 1 addition & 1 deletion db/notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Notification(models.Model):
description = models.CharField(max_length=200, blank=False)
button = models.CharField(max_length=10, blank=True, null=True)
url = models.CharField(max_length=100, blank=True, null=True)
created_at = models.DateTimeField()
created_at = models.DateTimeField(auto_now_add=True)
created_by = models.ForeignKey(User, on_delete=models.CASCADE, db_column="created_by", related_name="created_notifications")

class Meta:
Expand Down
Loading

0 comments on commit 72eae39

Please sign in to comment.