Skip to content

Commit

Permalink
Merge pull request #1405 from gtech-mulearn/dev
Browse files Browse the repository at this point in the history
km and ig issue fix
  • Loading branch information
adnankattekaden authored Oct 22, 2023
2 parents c47cecb + 63386c4 commit 181edaf
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 45 deletions.
74 changes: 39 additions & 35 deletions api/dashboard/ig/dash_ig_serializer.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import uuid

from rest_framework import serializers

from db.task import InterestGroup
Expand Down Expand Up @@ -26,23 +28,14 @@ class Meta:
]

def get_user_ig_link_ig(self, obj):
return len(obj.user_ig_link_ig.all())
return obj.user_ig_link_ig.all().count()


class InterestGroupCreateSerializer(serializers.ModelSerializer):
class InterestGroupCreateUpdateSerializer(serializers.ModelSerializer):

name = serializers.CharField(
required=True,
error_messages={
'required': 'code field must not be left blank.'
}
)
icon = serializers.CharField(
required=True,
error_messages={
'required': 'icon field must not be left blank.'
}
)
name = serializers.CharField(required=True)
icon = serializers.CharField(required=True)
code = serializers.CharField(required=True)

class Meta:
model = InterestGroup
Expand All @@ -54,31 +47,42 @@ class Meta:

def create(self, validated_data):
user_id = self.context.get('user_id')
return InterestGroup.objects.create(
name=validated_data.get('name'),
code=validated_data.get('code'),
icon=validated_data.get('icon'),
updated_by_id=user_id,
created_by_id=user_id,
)

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

class InterestGroupUpdateSerializer(serializers.ModelSerializer):
class Meta:
model = InterestGroup
fields = [
"name",
"code",
"icon"
]
return InterestGroup.objects.create(**validated_data)

def update(self, instance, validated_data):
user_id = self.context.get('user_id')
user = User.objects.filter(id=user_id).first()
instance.name = validated_data.get('name')
instance.code = validated_data.get('code')
instance.icon = validated_data.get('icon')
instance.updated_by = user
instance.updated_at = DateTimeUtils.get_current_utc_time()

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
29 changes: 20 additions & 9 deletions api/dashboard/ig/dash_ig_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
from utils.utils import CommonUtils, DiscordWebhooks
from .dash_ig_serializer import (
InterestGroupSerializer,
InterestGroupCreateSerializer,
InterestGroupUpdateSerializer,
InterestGroupCreateUpdateSerializer
)


Expand All @@ -27,7 +26,6 @@ def get(self, request):
"user_ig_link_ig"
).all()
)

paginated_queryset = CommonUtils.get_paginated_queryset(
ig_queryset,
request,
Expand All @@ -39,7 +37,11 @@ def get(self, request):
"updated_by__last_name",
],
{
"name": "name"
"name": "name",
"updated_on": "updated_at",
"updated_by": "updated_by",
"created_on": "created_at",
"created_by": "created_by"
},
)

Expand All @@ -59,12 +61,13 @@ def get(self, request):
def post(self, request):
user_id = JWTUtils.fetch_user_id(request)

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

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

Expand All @@ -81,7 +84,7 @@ def post(self, request):
).get_success_response()

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

@role_required([RoleType.ADMIN.value])
Expand All @@ -92,7 +95,7 @@ def put(self, request, pk):
id=pk
).name

serializer = InterestGroupUpdateSerializer(
serializer = InterestGroupCreateUpdateSerializer(
data=request.data,
instance=InterestGroup.objects.get(
id=pk
Expand Down Expand Up @@ -149,7 +152,15 @@ class InterestGroupCSV(APIView):

@role_required([RoleType.ADMIN.value])
def get(self, request):
ig_serializer = InterestGroup.objects.all()

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

ig_serializer_data = InterestGroupSerializer(
ig_serializer,
Expand Down
2 changes: 1 addition & 1 deletion db/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class InterestGroup(models.Model):
id = models.CharField(primary_key=True, max_length=36, default=uuid.uuid4)
name = models.CharField(max_length=75, unique=True)
code = models.CharField(max_length=10, unique=True)
icon = models.CharField(max_length=10, unique=True)
icon = models.CharField(max_length=10)
updated_by = models.ForeignKey(
User,
on_delete=models.CASCADE,
Expand Down

0 comments on commit 181edaf

Please sign in to comment.