From 2bb2b6e0d07624b02abf0328a1bbb7b1e36839cc Mon Sep 17 00:00:00 2001 From: anirudh-mk Date: Tue, 31 Oct 2023 16:52:09 +0530 Subject: [PATCH 1/2] optim(some fixes in lc api) --- api/dashboard/lc/dash_lc_serializer.py | 93 ++++++--- api/dashboard/lc/dash_lc_view.py | 264 ++++++++++++++++++++----- api/dashboard/lc/urls.py | 12 +- db/learning_circle.py | 2 +- 4 files changed, 292 insertions(+), 79 deletions(-) diff --git a/api/dashboard/lc/dash_lc_serializer.py b/api/dashboard/lc/dash_lc_serializer.py index 4a377fcd..f492bad1 100644 --- a/api/dashboard/lc/dash_lc_serializer.py +++ b/api/dashboard/lc/dash_lc_serializer.py @@ -18,7 +18,10 @@ class LearningCircleSerializer(serializers.ModelSerializer): member_count = serializers.SerializerMethodField() def get_member_count(self, obj): - return UserCircleLink.objects.filter(circle_id=obj.id, accepted=1).count() + return UserCircleLink.objects.filter( + circle_id=obj.id, + accepted=1 + ).count() class Meta: model = LearningCircle @@ -39,12 +42,18 @@ class Meta: class LearningCircleCreateSerializer(serializers.ModelSerializer): - ig = serializers.CharField(required=True, error_messages={ - 'required': 'ig field must not be left blank.' - }) - name = serializers.CharField(required=True, error_messages={ - 'required': 'name field must not be left blank.'} - ) + ig = serializers.CharField( + required=True, + error_messages={ + 'required': 'ig field must not be left blank.' + } + ) + name = serializers.CharField( + required=True, + error_messages={ + 'required': 'name field must not be left blank.' + } + ) class Meta: model = LearningCircle @@ -57,25 +66,42 @@ def validate(self, data): user_id = self.context.get('user_id') ig_id = data.get('ig') - if not InterestGroup.objects.filter(id=ig_id).exists(): - raise serializers.ValidationError("Invalid interest group") + if not InterestGroup.objects.filter( + id=ig_id + ).exists(): + raise serializers.ValidationError( + "Invalid interest group" + ) # org_link = UserOrganizationLink.objects.filter(user_id=user_id, # org__org_type=OrganizationType.COLLEGE.value).first() # if not org_link: # raise serializers.ValidationError("User must be associated with a college organization") - if UserCircleLink.objects.filter(user_id=user_id, circle__ig_id=ig_id, accepted=True).exists(): - raise serializers.ValidationError("Already a member of a learning circle with the same interest group") + if UserCircleLink.objects.filter( + user_id=user_id, + circle__ig_id=ig_id, + accepted=True + ).exists(): + + raise serializers.ValidationError( + "Already a member of a learning circle with the same interest group" + ) return data def create(self, validated_data): user_id = self.context.get('user_id') - org_link = UserOrganizationLink.objects.filter(user_id=user_id, - org__org_type=OrganizationType.COLLEGE.value).first() + org_link = UserOrganizationLink.objects.filter( + user_id=user_id, + org__org_type=OrganizationType.COLLEGE.value + ).first() - ig = InterestGroup.objects.filter(id=validated_data.get('ig')).first() + ig = InterestGroup.objects.filter( + id=validated_data.get( + 'ig' + ) + ).first() if org_link: if len(org_link.org.code) > 4: @@ -183,8 +209,8 @@ def get_is_lead(self, obj): def get_total_karma(self, obj): return ( KarmaActivityLog.objects.filter( - user__usercirclelink__circle=obj, - user__usercirclelink__accepted=True, + user__user_circle_link_user__circle=obj, + user__user_circle_link_user__accepted=True, task__ig=obj.ig, appraiser_approved=True, ).aggregate( @@ -230,8 +256,8 @@ def _get_member_info(self, obj, accepted): def get_rank(self, obj): total_karma = KarmaActivityLog.objects.filter( - user__usercirclelink__circle=obj, - user__usercirclelink__accepted=True, + user__user_circle_link_user__circle=obj, + user__user_circle_link_user__accepted=True, task__ig=obj.ig, appraiser_approved=True ).aggregate( @@ -250,8 +276,8 @@ def get_rank(self, obj): for lc in all_learning_circles: total_karma_lc = KarmaActivityLog.objects.filter( - user__usercirclelink__circle=lc, - user__usercirclelink__accepted=True, + user__user_circle_link_user__circle=lc, + user__user_circle_link_user__accepted=True, task__ig=lc.ig, appraiser_approved=True ).aggregate( @@ -383,10 +409,23 @@ class LearningCircleMainSerializer(serializers.ModelSerializer): class Meta: model = LearningCircle - fields = ['name', 'ig_name', 'member_count', 'members', 'meet_place', 'meet_time', 'lead_name'] + fields = [ + 'name', + 'ig_name', + 'member_count', + 'members', + 'meet_place', + 'meet_time', + 'lead_name' + ] def get_lead_name(self, obj): - user_circle_link = UserCircleLink.objects.filter(circle=obj, accepted=1, lead=True).first() + user_circle_link = UserCircleLink.objects.filter( + circle=obj, + accepted=1, + lead=True + ).first() + if user_circle_link: user = user_circle_link.user return f'{user.first_name} {user.last_name}' @@ -396,10 +435,16 @@ def get_ig_name(self, obj): return obj.ig.name if obj.ig else None def get_member_count(self, obj): - return UserCircleLink.objects.filter(circle=obj, accepted=1).count() + return UserCircleLink.objects.filter( + circle=obj, + accepted=1 + ).count() def get_members(self, obj): - members = UserCircleLink.objects.filter(circle=obj, accepted=1) + members = UserCircleLink.objects.filter( + circle=obj, + accepted=1 + ) return [ { 'username': f'{member.user.first_name} {member.user.last_name}' diff --git a/api/dashboard/lc/dash_lc_view.py b/api/dashboard/lc/dash_lc_view.py index 4bf10407..304196f6 100644 --- a/api/dashboard/lc/dash_lc_view.py +++ b/api/dashboard/lc/dash_lc_view.py @@ -27,34 +27,72 @@ def post(self, request, circle_code=None): user_id = JWTUtils.fetch_user_id(request) filters = Q() - filters &= ~Q(usercirclelink__accepted=1, usercirclelink__user_id=user_id) + filters &= ~Q( + user_circle_link_circle__accepted=1, + user_circle_link_circle__user_id=user_id + ) + if district_id := request.data.get('district_id'): - filters &= Q(org__district_id=district_id) + filters &= Q( + org__district_id=district_id + ) if org_id := request.data.get('org_id'): filters &= Q(org_id=org_id) if interest_group_id := request.data.get('ig_id'): filters &= Q(ig_id=interest_group_id) if circle_code: - if not LearningCircle.objects.filter(Q(circle_code=circle_code) | Q(name__icontains=circle_code)).exists(): - return CustomResponse(general_message='invalid circle code or Circle Name').get_failure_response() - filters &= Q(circle_code=circle_code) | Q(name__icontains=circle_code) + if not LearningCircle.objects.filter( + Q(circle_code=circle_code) | + Q(name__icontains=circle_code) + ).exists(): + + return CustomResponse( + general_message='invalid circle code or Circle Name' + ).get_failure_response() + + filters &= ( + Q(circle_code=circle_code) | + Q(name__icontains=circle_code) + ) - learning_queryset = LearningCircle.objects.filter(filters) - learning_serializer = LearningCircleSerializer(learning_queryset, many=True) + learning_queryset = LearningCircle.objects.filter( + filters + ) - return CustomResponse(response=learning_serializer.data).get_success_response() + learning_serializer = LearningCircleSerializer( + learning_queryset, + many=True + ) + + return CustomResponse( + response=learning_serializer.data + ).get_success_response() class LearningCircleCreateApi(APIView): def post(self, request): user_id = JWTUtils.fetch_user_id(request) - serializer = LearningCircleCreateSerializer(data=request.data, context={ 'user_id': user_id }) + + serializer = LearningCircleCreateSerializer( + data=request.data, + context={ + 'user_id': user_id + } + ) if serializer.is_valid(): circle = serializer.save() - return CustomResponse(general_message='LearningCircle created successfully', - response={ 'circle_id': circle.id }).get_success_response() - return CustomResponse(message=serializer.errors).get_failure_response() + + return CustomResponse( + general_message='LearningCircle created successfully', + response={ + 'circle_id': circle.id + } + ).get_success_response() + + return CustomResponse( + message=serializer.errors + ).get_failure_response() class LearningCircleJoinApi(APIView): @@ -79,38 +117,95 @@ def post(self, request, circle_id): class UserLearningCircleListApi(APIView): + """ + API endpoint for listing a user's learning circles. + + Endpoint: /api/v1/dashboard/lc/ (GET) + + Returns: + CustomResponse: A custom response containing a list of learning circles + associated with the user. + """ def get(self, request): # Lists user's learning circle user_id = JWTUtils.fetch_user_id(request) - learning_queryset = LearningCircle.objects.filter(usercirclelink__user_id=user_id, usercirclelink__accepted=1) - learning_serializer = LearningCircleSerializer(learning_queryset, many=True) - return CustomResponse(response=learning_serializer.data).get_success_response() + + learning_queryset = LearningCircle.objects.filter( + user_circle_link_circle__user_id=user_id, + user_circle_link_circle__accepted=1 + ) + + learning_serializer = LearningCircleSerializer( + learning_queryset, + many=True + ) + + return CustomResponse( + response=learning_serializer.data + ).get_success_response() + class LearningCircleMeetAPI(APIView): def patch(self, request, circle_id): - learning_circle = LearningCircle.objects.filter(id=circle_id).first() - serializer = LearningCircleMeetSerializer(learning_circle, data=request.data) + + learning_circle = LearningCircle.objects.filter( + id=circle_id + ).first() + + serializer = LearningCircleMeetSerializer( + learning_circle, + data=request.data + ) if serializer.is_valid(): serializer.save() - return CustomResponse(general_message='Meet updated successfully').get_success_response() - return CustomResponse(message=serializer.errors).get_failure_response() + + return CustomResponse( + general_message='Meet updated successfully' + ).get_success_response() + + return CustomResponse( + message=serializer.errors + ).get_failure_response() class LearningCircleLeadTransfer(APIView): def patch(self, request, circle_id, lead_id): user_id = JWTUtils.fetch_user_id(request) - usr_circle_link = UserCircleLink.objects.filter(circle__id=circle_id, user__id=user_id).first() - lead_circle_link = UserCircleLink.objects.filter(circle__id=circle_id, user__id=lead_id).first() - if not LearningCircle.objects.filter(id=circle_id).exists(): - return CustomResponse(general_message='Learning Circle not found').get_failure_response() + + usr_circle_link = UserCircleLink.objects.filter( + circle__id=circle_id, + user__id=user_id + ).first() + + lead_circle_link = UserCircleLink.objects.filter( + circle__id=circle_id, + user__id=lead_id + ).first() + + if not LearningCircle.objects.filter( + id=circle_id + ).exists(): + + return CustomResponse( + general_message='Learning Circle not found' + ).get_failure_response() + if usr_circle_link is None or usr_circle_link.lead != 1: - return CustomResponse(general_message='User is not lead').get_failure_response() + return CustomResponse( + general_message='User is not lead' + ).get_failure_response() + if lead_circle_link is None: - return CustomResponse(general_message='New lead not found in the circle').get_failure_response() + return CustomResponse( + general_message='New lead not found in the circle' + ).get_failure_response() + usr_circle_link.lead = None lead_circle_link.lead = 1 usr_circle_link.save() lead_circle_link.save() - return CustomResponse(general_message='Lead transferred successfully').get_success_response() + return CustomResponse( + general_message='Lead transferred successfully' + ).get_success_response() class LearningCircleHomeApi(APIView): @@ -280,27 +375,65 @@ def post(self, request): all_circles = all_circles.filter(ig_id=ig_id) if ig_id or org_id or district_id: - serializer = LearningCircleMainSerializer(all_circles, many=True) + serializer = LearningCircleMainSerializer( + all_circles, + many=True + ) else: random_circles = all_circles.order_by('?')[:9] - serializer = LearningCircleMainSerializer(random_circles, many=True) - return CustomResponse(response=serializer.data).get_success_response() + + serializer = LearningCircleMainSerializer( + random_circles, + many=True + ) + + return CustomResponse( + response=serializer.data + ).get_success_response() class LearningCircleDataAPI(APIView): + """ + API endpoint for retrieving basic data about all learning circles. + + Endpoint: /api/v1/dashboard/lc/data/ (GET) + + Returns: + CustomResponse: A custom response containing data about all learning circles. + """ + def get(self, request): all_circles = LearningCircle.objects.all() - serializer = LearningCircleDataSerializer(all_circles, many=False) - return CustomResponse(response=serializer.data).get_success_response() + + serializer = LearningCircleDataSerializer( + all_circles, + many=False + ) + + return CustomResponse( + response=serializer.data + ).get_success_response() class LearningCircleListMembersApi(APIView): - def get(self, request, circle_name): - lc = LearningCircle.objects.filter(name=circle_name) - if lc is None: - return CustomResponse(general_message='Learning Circle Not Exists').get_failure_response() - serializer = LearningCircleMemberlistSerializer(lc, many=True) - return CustomResponse(response=serializer.data).get_success_response() + def get(self, request, circle_id): + learning_circle = LearningCircle.objects.filter( + id=circle_id + ) + + if learning_circle is None: + return CustomResponse( + general_message='Learning Circle Not Exists' + ).get_failure_response() + + serializer = LearningCircleMemberlistSerializer( + learning_circle, + many=True + ) + + return CustomResponse( + response=serializer.data + ).get_success_response() class LearningCircleInviteLeadAPI(APIView): @@ -345,19 +478,35 @@ def post(self, request, circle_id, muid): """ user = User.objects.filter(muid=muid).first() if not user: - return CustomResponse(general_message='Muid is Invalid').get_failure_response() - usr_circle_link = UserCircleLink.objects.filter(circle__id=circle_id, user__id=user.id).first() + return CustomResponse( + general_message='Muid is Invalid' + ).get_failure_response() + + usr_circle_link = UserCircleLink.objects.filter( + circle__id=circle_id, + user__id=user.id + ).first() + if usr_circle_link: if usr_circle_link.accepted: - return CustomResponse(general_message='User already part of circle').get_failure_response() + + return CustomResponse( + general_message='User already part of circle' + ).get_failure_response() + elif usr_circle_link.is_invited: - return CustomResponse(general_message='User already invited').get_failure_response() + return CustomResponse( + general_message='User already invited' + ).get_failure_response() + receiver_email = user.email html_address = ["lc_invitation.html"] inviter = User.objects.filter(id=JWTUtils.fetch_user_id(request)).first() inviter_name = inviter.first_name + " " + inviter.last_name context = { - "circle_name": LearningCircle.objects.filter(id=circle_id).first().name, + "circle_name": LearningCircle.objects.filter( + id=circle_id + ).first().name, "inviter_name": inviter_name, "circle_id": circle_id, "muid": muid, @@ -368,6 +517,7 @@ def post(self, request, circle_id, muid): subject="MuLearn - Invitation to learning circle", address=html_address, ) + if status == 1: usr_circle_link_new = UserCircleLink( id=uuid.uuid4(), @@ -378,8 +528,14 @@ def post(self, request, circle_id, muid): created_at=DateTimeUtils.get_current_utc_time(), ) usr_circle_link_new.save() - return CustomResponse(general_message='User Invited').get_success_response() - return CustomResponse(general_message='Mail not sent').get_failure_response() + + return CustomResponse( + general_message='User Invited' + ).get_success_response() + + return CustomResponse( + general_message='Mail not sent' + ).get_failure_response() class LearningCircleInvitationStatus(APIView): @@ -397,11 +553,19 @@ def post(self, request, circle_id, muid, status): """ user = User.objects.filter(muid=muid).first() if not user: - return CustomResponse(general_message='Muid is Invalid').get_failure_response() - usr_circle_link = UserCircleLink.objects.filter(circle__id=circle_id, user__id=user.id).first() + return CustomResponse( + general_message='Muid is Invalid' + ).get_failure_response() + + usr_circle_link = UserCircleLink.objects.filter( + circle__id=circle_id, + user__id=user.id + ).first() if not usr_circle_link: - return CustomResponse(general_message='User not invited').get_failure_response() + return CustomResponse( + general_message='User not invited' + ).get_failure_response() if status == "accepted": usr_circle_link.accepted = True @@ -409,9 +573,13 @@ def post(self, request, circle_id, muid, status): usr_circle_link.save() # return CustomResponse(general_message='User added to circle').get_success_response() return redirect(f'{domain}/dashboard/learning-circle/') + elif status == "rejected": usr_circle_link.delete() - return CustomResponse(general_message='User rejected invitation').get_failure_response() + + return CustomResponse( + general_message='User rejected invitation' + ).get_failure_response() class PreviousMeetingsDetailsAPI(APIView): diff --git a/api/dashboard/lc/urls.py b/api/dashboard/lc/urls.py index f692871e..d087f999 100644 --- a/api/dashboard/lc/urls.py +++ b/api/dashboard/lc/urls.py @@ -4,20 +4,20 @@ urlpatterns = [ path('', dash_lc_view.UserLearningCircleListApi.as_view()), - path('list-members//', dash_lc_view.LearningCircleListMembersApi.as_view()), - # path('invite/', dash_lc_view.LearningCircleInviteLeadAPI.as_view()), + path('/', dash_lc_view.LearningCircleHomeApi.as_view()), path('list/', dash_lc_view.LearningCircleMainApi.as_view()), path('data/', dash_lc_view.LearningCircleDataAPI.as_view()), path('list-all/', dash_lc_view.TotalLearningCircleListApi.as_view()), - path('list-all//', dash_lc_view.TotalLearningCircleListApi.as_view()), path('create/', dash_lc_view.LearningCircleCreateApi.as_view()), + path('list-members//', dash_lc_view.LearningCircleListMembersApi.as_view()), + # path('invite/', dash_lc_view.LearningCircleInviteLeadAPI.as_view()), + path('list-all//', dash_lc_view.TotalLearningCircleListApi.as_view()), path('meet//', dash_lc_view.LearningCircleMeetAPI.as_view()), + path('//', dash_lc_view.LearningCircleHomeApi.as_view()), + path('meeting-log//', dash_lc_view.PreviousMeetingsDetailsAPI.as_view()), path('meet/create//', dash_lc_view.MeetingCreateUpdateDeleteAPI.as_view()), path('join//', dash_lc_view.LearningCircleJoinApi.as_view()), path('member/invite///', dash_lc_view.LearningCircleInviteMember.as_view()), path('member/invite/status////', dash_lc_view.LearningCircleInvitationStatus.as_view()), - path('meeting-log//', dash_lc_view.PreviousMeetingsDetailsAPI.as_view()), - path('/', dash_lc_view.LearningCircleHomeApi.as_view()), - path('//', dash_lc_view.LearningCircleHomeApi.as_view()), path('lead///', dash_lc_view.LearningCircleLeadTransfer.as_view()), ] diff --git a/db/learning_circle.py b/db/learning_circle.py index fa5abc00..cf027066 100644 --- a/db/learning_circle.py +++ b/db/learning_circle.py @@ -46,7 +46,7 @@ class Meta: class CircleMeetingLog(models.Model): id = models.CharField(primary_key=True, max_length=36, default=uuid.uuid4()) circle = models.ForeignKey(LearningCircle, on_delete=models.CASCADE, related_name='circle_meeting_log_learning_circle') - meet_time = models.DateTimeField(auto_now=True) + meet_time = models.DateTimeField() meet_place = models.CharField(max_length=255, blank=True, null=True) day = models.CharField(max_length=20) attendees = models.CharField(max_length=216) From 6a1144594f89d405fe187259372b14bd8e3f66f0 Mon Sep 17 00:00:00 2001 From: Adnan Kattekaden Date: Tue, 31 Oct 2023 22:32:03 +0530 Subject: [PATCH 2/2] gta sandshore api --- api/common/common_views.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/api/common/common_views.py b/api/common/common_views.py index dac338b0..d402bedb 100644 --- a/api/common/common_views.py +++ b/api/common/common_views.py @@ -261,7 +261,6 @@ def get(self, request): class GTASANDSHOREAPI(APIView): def get(self, request): - response = requests.get('https://devfolio.vez.social/rank') if response.status_code == 200: # Save JSON response to a local file @@ -276,7 +275,6 @@ def get(self, request): # Create a dictionary to store the grouped data grouped_colleges = {} - for college, count in data.items(): # Clean the college name by removing spaces and converting to lowercase cleaned_college = college.replace(" ", "").lower()