From d8f9e0d92096ee12833f189192e59f1fe6083951 Mon Sep 17 00:00:00 2001 From: jelanmathewjames Date: Thu, 20 Jun 2024 22:11:48 +0530 Subject: [PATCH 1/2] applying conditional filtering --- api/launchpad/launchpad_views.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/api/launchpad/launchpad_views.py b/api/launchpad/launchpad_views.py index d36740a2..765ccbce 100644 --- a/api/launchpad/launchpad_views.py +++ b/api/launchpad/launchpad_views.py @@ -101,7 +101,9 @@ def get(self, request): district_name=F("user_organization_link_user__org__district__name"), state=F("user_organization_link_user__org__district__zone__state__name"), level=F("user_role_link_user__role__title"), - time_=Max("karma_activity_log_user__created_at") + time_=Max("karma_activity_log_user__created_at"), + ).filter( + Q(level__in=allowed_levels) | Q(level__isnull=True) ).distinct() paginated_queryset = CommonUtils.get_paginated_queryset( From 86129b8afefad5ee506b286b7fd97d89ed8dab4c Mon Sep 17 00:00:00 2001 From: jelanmathewjames Date: Sun, 23 Jun 2024 20:55:41 +0530 Subject: [PATCH 2/2] refactor: Update ListParticipantsAPI and LaunchpadParticipantsSerializer --- alter-scripts/alter-1.50.py | 35 -------------------------------- api/launchpad/launchpad_views.py | 16 +++++++-------- api/launchpad/serializers.py | 17 ++++++++++++---- 3 files changed, 21 insertions(+), 47 deletions(-) delete mode 100644 alter-scripts/alter-1.50.py diff --git a/alter-scripts/alter-1.50.py b/alter-scripts/alter-1.50.py deleted file mode 100644 index 34abb27a..00000000 --- a/alter-scripts/alter-1.50.py +++ /dev/null @@ -1,35 +0,0 @@ -import os -import sys -import uuid -from decouple import config -import django - -from connection import execute - -os.chdir('..') -sys.path.append(os.getcwd()) -os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mulearnbackend.settings') -django.setup() - -from db.launchpad import LaunchPadUsers -from utils.types import LaunchPadRoles - -def create_launchpad_admin(): - query = f"SELECT id FROM launchpad_user WHERE email = '{config('LAUNCHPAD_ADMIN_EMAIL')}'" - if execute(query): - return - query = f""" - INSERT INTO launchpad_user (id, email, role, created_at, updated_at) - VALUES ( - '{uuid.uuid4()}', - '{config('LAUNCHPAD_ADMIN_EMAIL')}', - '{LaunchPadRoles.ADMIN.value}', - UTC_TIMESTAMP, - UTC_TIMESTAMP - ) - """ - execute(query) - -if __name__ == '__main__': - create_launchpad_admin() - execute("UPDATE system_setting SET value = '1.50', updated_at = now() WHERE `key` = 'db.version';") diff --git a/api/launchpad/launchpad_views.py b/api/launchpad/launchpad_views.py index 765ccbce..9a666c59 100644 --- a/api/launchpad/launchpad_views.py +++ b/api/launchpad/launchpad_views.py @@ -4,7 +4,7 @@ from rest_framework.views import APIView -from .serializers import LaunchpadLeaderBoardSerializer, LaunchpadParticipantsSerializer, \ +from .serializers import LaunchpadLeaderBoardSerializer, LaunchpadParticipantsSerializer, LaunchpadUserListSerializer,\ CollegeDataSerializer, LaunchpadUserSerializer, UserProfileUpdateSerializer, LaunchpadUpdateUserSerializer from utils.response import CustomResponse from utils.utils import CommonUtils, ImportCSV @@ -231,7 +231,7 @@ def post(self, request): if not serializer.is_valid(): return CustomResponse(message=serializer.errors).get_failure_response() - colleges = data.get('college') + colleges = data.get('colleges') errors = {} error = False not_found_colleges = [] @@ -267,7 +267,7 @@ def get(self, request): ["full_name", "phone_number", "email", "role", "district", "zone"] ) - serializer = LaunchpadUserSerializer( + serializer = LaunchpadUserListSerializer( paginated_queryset.get("queryset"), many=True ) return CustomResponse().paginated_response( @@ -297,7 +297,7 @@ def get(self, request, email): user = LaunchPadUsers.objects.get(email=email) except LaunchPadUsers.DoesNotExist: return CustomResponse(general_message="User not found").get_failure_response() - serializer = LaunchpadUserSerializer(user) + serializer = LaunchpadUserListSerializer(user) return CustomResponse(response=serializer.data).get_success_response() @@ -308,7 +308,7 @@ def get(self, request): if not LaunchPadUsers.objects.filter(email=auth_mail).exists(): return CustomResponse(general_message="Unauthorized").get_failure_response() user = LaunchPadUsers.objects.get(email=auth_mail) - serializer = LaunchpadUserSerializer(user) + serializer = LaunchpadUserListSerializer(user) return CustomResponse(data=serializer.data).get_success_response() def put(self, request): @@ -406,14 +406,14 @@ def post(self, request): for data in excel_data[1:]: not_found_colleges = [] - data['college'] = data['college'].split(",") if data.get('college') else [] + data['colleges'] = data['colleges'].split(",") if data.get('colleges') else [] serializer = LaunchpadUserSerializer(data=data) if not serializer.is_valid(): continue user = serializer.save() - if data.get('college') is None: + if data.get('colleges') is None: continue - for college in data.get('college'): + for college in data.get('colleges'): if not (org := Organization.objects.filter(title=college, org_type="College").first()): error = True not_found_colleges.append(college) diff --git a/api/launchpad/serializers.py b/api/launchpad/serializers.py index 0fe5bdf5..b000e217 100644 --- a/api/launchpad/serializers.py +++ b/api/launchpad/serializers.py @@ -91,6 +91,7 @@ class CollegeDataSerializer(serializers.ModelSerializer): class Meta: model = Organization fields = ( + "id", "title", "district_name", "state", @@ -104,15 +105,15 @@ class Meta: class LaunchpadUserSerializer(serializers.ModelSerializer): id = serializers.CharField(max_length=36, read_only=True) role = serializers.ChoiceField(choices=LaunchPadRoles.get_all_values()) - college = serializers.ListField(child=serializers.CharField(), allow_empty=True, write_only=True) - colleges = serializers.SerializerMethodField() + colleges = serializers.ListField(child=serializers.CharField(), allow_empty=True, write_only=True) + class Meta: model = LaunchPadUsers - fields = ("id", "full_name", "email", "phone_number", "role", "college", "district", "zone", "colleges") + fields = ("id", "full_name", "email", "phone_number", "role", "district", "zone", "colleges") def create(self, validated_data): - validated_data.pop("college") + validated_data.pop("colleges") validated_data["id"] = uuid.uuid4() validated_data["created_at"] = DateTimeUtils.get_current_utc_time() @@ -121,6 +122,14 @@ def create(self, validated_data): return user + +class LaunchpadUserListSerializer(serializers.ModelSerializer): + colleges = serializers.SerializerMethodField() + + class Meta: + model = LaunchPadUsers + fields = ("id", "full_name", "email", "phone_number", "role", "district", "zone", "colleges") + def get_colleges(self, obj): return LaunchPadUserCollegeLink.objects.filter(user=obj).values_list("college_id", "college__title")