From 3dd2e782074ae17ed740c046b43a254721c40c78 Mon Sep 17 00:00:00 2001 From: Aswanth Vc Date: Wed, 9 Oct 2024 00:28:09 +0530 Subject: [PATCH 1/3] feat: add interest selected field in user info api --- api/dashboard/user/dash_user_serializer.py | 75 +++++++++++++--------- api/dashboard/user/dash_user_views.py | 8 +-- 2 files changed, 48 insertions(+), 35 deletions(-) diff --git a/api/dashboard/user/dash_user_serializer.py b/api/dashboard/user/dash_user_serializer.py index c391c8a4..b3e3c833 100644 --- a/api/dashboard/user/dash_user_serializer.py +++ b/api/dashboard/user/dash_user_serializer.py @@ -11,14 +11,14 @@ from utils.types import OrganizationType from utils.utils import DateTimeUtils from db.user import DynamicRole, DynamicUser +from db.user import UserInterests -BE_DOMAIN_NAME = decouple_config('BE_DOMAIN_NAME') +BE_DOMAIN_NAME = decouple_config("BE_DOMAIN_NAME") class UserDashboardSerializer(serializers.ModelSerializer): karma = serializers.IntegerField(source="wallet_user.karma", default=None) - level = serializers.CharField( - source="user_lvl_link_user.level.name", default=None) + level = serializers.CharField(source="user_lvl_link_user.level.name", default=None) class Meta: model = User @@ -39,6 +39,7 @@ class UserSerializer(serializers.ModelSerializer): joined = serializers.CharField(source="created_at") roles = serializers.SerializerMethodField() dynamic_type = serializers.SerializerMethodField() + interest_selected = serializers.SerializerMethodField() class Meta: model = User @@ -54,8 +55,15 @@ class Meta: "roles", "profile_pic", "dynamic_type", + "interest_selected", ] + def get_interest_selected(self, obj): + print("SDAFAS") + if not UserInterests.objects.filter(user=obj).exists(): + return "Please select your interests" + return None + def get_roles(self, obj): return [ user_role_link.role.title @@ -63,18 +71,23 @@ def get_roles(self, obj): ] def get_dynamic_type(self, obj): - return {dynamic_role.type for dynamic_role in - DynamicRole.objects.filter(role__title__in=self.get_roles(obj))}.union( - {dynamic_user.type for dynamic_user in DynamicUser.objects.filter(user=obj)}) + return { + dynamic_role.type + for dynamic_role in DynamicRole.objects.filter( + role__title__in=self.get_roles(obj) + ) + }.union( + {dynamic_user.type for dynamic_user in DynamicUser.objects.filter(user=obj)} + ) class CollegeSerializer(serializers.ModelSerializer): org_type = serializers.CharField(source="org.org_type") department = serializers.CharField(source="department.pk", allow_null=True) country = serializers.CharField( - source="org.district.zone.state.country.pk", allow_null=True) - state = serializers.CharField( - source="org.district.zone.state.pk", allow_null=True) + source="org.district.zone.state.country.pk", allow_null=True + ) + state = serializers.CharField(source="org.district.zone.state.pk", allow_null=True) district = serializers.CharField(source="org.district.pk", allow_null=True) class Meta: @@ -126,7 +139,7 @@ class Meta: "graduation_year", "interest_groups", "igs", - "district" + "district", ] def validate(self, data): @@ -134,10 +147,10 @@ def validate(self, data): raise serializers.ValidationError("User id is a required field") if ( - "email" in data - and User.objects.filter(email=data["email"]) - .exclude(id=data["user_id"].id) - .all() + "email" in data + and User.objects.filter(email=data["email"]) + .exclude(id=data["user_id"].id) + .all() ): raise serializers.ValidationError("This email is already in use") return super().validate(data) @@ -188,14 +201,16 @@ def update(self, instance, validated_data): return super().update(instance, validated_data) def get_organizations(self, user): - organization_links = user.user_organization_link_user.select_related( - "org") + organization_links = user.user_organization_link_user.select_related("org") if not organization_links.exists(): return None organizations_data = [] for link in organization_links: - if link.org.org_type == OrganizationType.COLLEGE.value or OrganizationType.SCHOOL.value: + if ( + link.org.org_type == OrganizationType.COLLEGE.value + or OrganizationType.SCHOOL.value + ): serializer = CollegeSerializer(link) else: serializer = OrgSerializer(link) @@ -259,18 +274,18 @@ class Meta: "department", "graduation_year", "admin", - "district" + "district", ] def to_representation(self, instance): data = super().to_representation(instance) if ( - college := instance.user_organization_link_user.filter( - org__org_type=OrganizationType.COLLEGE.value - ) - .select_related("org__district__zone__state__country", "department") - .first() + college := instance.user_organization_link_user.filter( + org__org_type=OrganizationType.COLLEGE.value + ) + .select_related("org__district__zone__state__country", "department") + .first() ): data.update( { @@ -307,7 +322,7 @@ def update(self, instance, validated_data): with transaction.atomic(): if isinstance( - organization_ids := validated_data.pop("organizations", None), list + organization_ids := validated_data.pop("organizations", None), list ): instance.user_organization_link_user.all().delete() organizations = Organization.objects.filter( @@ -315,8 +330,8 @@ def update(self, instance, validated_data): ).order_by("org_type") if ( - organizations.exists() - and organizations.first().org_type != OrganizationType.COLLEGE.value + organizations.exists() + and organizations.first().org_type != OrganizationType.COLLEGE.value ): validated_data.pop("department", None) validated_data.pop("graduation_year", None) @@ -329,10 +344,8 @@ def update(self, instance, validated_data): created_by=admin, created_at=current_time, verified=True, - department_id=validated_data.pop( - "department", None), - graduation_year=validated_data.pop( - "graduation_year", None), + department_id=validated_data.pop("department", None), + graduation_year=validated_data.pop("graduation_year", None), ) for org in organizations ] @@ -354,7 +367,7 @@ def update(self, instance, validated_data): ) if isinstance( - interest_group_ids := validated_data.pop("interest_groups", None), list + interest_group_ids := validated_data.pop("interest_groups", None), list ): instance.user_ig_link_user.all().delete() UserIgLink.objects.bulk_create( diff --git a/api/dashboard/user/dash_user_views.py b/api/dashboard/user/dash_user_views.py index 5abe83c7..d669a626 100644 --- a/api/dashboard/user/dash_user_views.py +++ b/api/dashboard/user/dash_user_views.py @@ -23,10 +23,10 @@ class UserInfoAPI(APIView): def get(self, request): user_muid = JWTUtils.fetch_muid(request) - user = cache.get(f"db_user_{user_muid}") - if not user: - user = User.objects.filter(muid=user_muid).first() - cache.set(f"db_user_{user_muid}", user, timeout=10) + # user = cache.get(f"db_user_{user_muid}") + # if not user: + user = User.objects.filter(muid=user_muid).first() + cache.set(f"db_user_{user_muid}", user, timeout=10) if user is None: return CustomResponse( general_message="No user data available" From fa91cccfad23d92848c990725231f44b1765ad89 Mon Sep 17 00:00:00 2001 From: Aswanth Vc Date: Wed, 9 Oct 2024 00:51:49 +0530 Subject: [PATCH 2/3] cache register flag --- api/register/register_views.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/api/register/register_views.py b/api/register/register_views.py index df08f9c0..7291b9dd 100644 --- a/api/register/register_views.py +++ b/api/register/register_views.py @@ -199,6 +199,9 @@ class RegisterDataAPI(APIView): def post(self, request): data = request.data data = {key: value for key, value in data.items() if value} + # create_user = serializers.UserSerializer( + # data=data.get("user"), context={"request": request} + # ) create_user = serializers.RegisterSerializer( data=data, context={"request": request} @@ -210,7 +213,7 @@ def post(self, request): user = create_user.save() cache.set(f"db_user_{user.muid}", user, timeout=20) password = request.data["user"]["password"] - + cache.set(f"flag_register_{user.muid}", True, timeout=5) res_data = get_auth_token(user.muid, password) response_data = serializers.UserDetailSerializer(user, many=False).data From 2e24a468947e26aff1f3a89393da18892627312e Mon Sep 17 00:00:00 2001 From: Aswanth Vc Date: Wed, 9 Oct 2024 01:18:44 +0530 Subject: [PATCH 3/3] fix: rank calculation issue fixed --- api/dashboard/profile/profile_serializer.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/api/dashboard/profile/profile_serializer.py b/api/dashboard/profile/profile_serializer.py index 19e10b06..8bb73f52 100644 --- a/api/dashboard/profile/profile_serializer.py +++ b/api/dashboard/profile/profile_serializer.py @@ -166,7 +166,7 @@ def get_rank(self, obj): ] ) ) - .order_by("-karma") + .order_by("-karma", "-updated_at", "created_at") ) ranks = list(ranks.values_list("user_id", flat=True)) return ranks.index(obj.id) + 1 @@ -284,7 +284,7 @@ def get_rank(self, obj): ] ) ) - .order_by("-karma") + .order_by("-karma", "-updated_at", "created_at") ) ranks = list(ranks.values_list("user_id", flat=True))