From c959bcb8d06eedfa9198c259a9345a90b38d8829 Mon Sep 17 00:00:00 2001 From: Aswanth Vc Date: Thu, 31 Oct 2024 21:12:18 +0530 Subject: [PATCH 1/2] feat(register): save graduation year and department for unverified organization --- api/dashboard/ig/dash_ig_view.py | 117 ++++++++++++------ .../organisation/organisation_views.py | 6 +- api/dashboard/organisation/serializers.py | 14 ++- api/dashboard/user/dash_user_views.py | 2 +- api/register/register_views.py | 2 +- api/register/serializers.py | 7 +- db/organization.py | 2 + 7 files changed, 103 insertions(+), 47 deletions(-) diff --git a/api/dashboard/ig/dash_ig_view.py b/api/dashboard/ig/dash_ig_view.py index d91384d8..78895519 100644 --- a/api/dashboard/ig/dash_ig_view.py +++ b/api/dashboard/ig/dash_ig_view.py @@ -14,6 +14,7 @@ from api.dashboard.roles.dash_roles_serializer import RoleDashboardSerializer from db.user import Role + class InterestGroupAPI(APIView): authentication_classes = [CustomizePermission] @@ -21,7 +22,8 @@ class InterestGroupAPI(APIView): def get(self, request): ig_queryset = ( InterestGroup.objects.select_related("created_by", "updated_by") - .prefetch_related("user_ig_link_ig").annotate(members=Count("user_ig_link_ig")) + .prefetch_related("user_ig_link_ig") + .annotate(members=Count("user_ig_link_ig")) .all() ) paginated_queryset = CommonUtils.get_paginated_queryset( @@ -65,47 +67,63 @@ def post(self, request): if serializer.is_valid(): serializer.save() - role_serializer = RoleDashboardSerializer(data={ - 'title': request_data.get("name"), - 'description': request_data.get("name") + " Interest Group Member", - 'created_by': request_data.get("created_by"), - 'updated_by': request_data.get("updated_by"), - },context={'request': request}) + role_serializer = RoleDashboardSerializer( + data={ + "title": request_data.get("name"), + "description": request_data.get("name") + " Interest Group Member", + "created_by": request_data.get("created_by"), + "updated_by": request_data.get("updated_by"), + }, + context={"request": request}, + ) if role_serializer.is_valid(): role_serializer.save() else: - return CustomResponse(general_message=role_serializer.errors).get_failure_response() - - campus_role_serializer = RoleDashboardSerializer(data={ - 'title': RoleType.IG_CAMPUS_LEAD_ROLE(request_data.get("code")), - 'description': request_data.get("name") + " Intrest Group Campus Lead", - 'created_by': request_data.get("created_by"), - 'updated_by': request_data.get("updated_by"), - },context={'request': request}) + return CustomResponse( + general_message=role_serializer.errors + ).get_failure_response() + + campus_role_serializer = RoleDashboardSerializer( + data={ + "title": RoleType.IG_CAMPUS_LEAD_ROLE(request_data.get("code")), + "description": request_data.get("name") + + " Intrest Group Campus Lead", + "created_by": request_data.get("created_by"), + "updated_by": request_data.get("updated_by"), + }, + context={"request": request}, + ) if campus_role_serializer.is_valid(): campus_role_serializer.save() else: - return CustomResponse(general_message=campus_role_serializer.errors).get_failure_response() - - ig_lead_role_serializer = RoleDashboardSerializer(data={ - 'title': RoleType.IG_LEAD_ROLE(request_data.get("code")), - 'description': request_data.get("name") + " Interest Group Lead", - 'created_by': request_data.get("created_by"), - 'updated_by': request_data.get("updated_by"), - },context={'request': request}) + return CustomResponse( + general_message=campus_role_serializer.errors + ).get_failure_response() + + ig_lead_role_serializer = RoleDashboardSerializer( + data={ + "title": RoleType.IG_LEAD_ROLE(request_data.get("code")), + "description": request_data.get("name") + " Interest Group Lead", + "created_by": request_data.get("created_by"), + "updated_by": request_data.get("updated_by"), + }, + context={"request": request}, + ) if ig_lead_role_serializer.is_valid(): ig_lead_role_serializer.save() else: - return CustomResponse(general_message=ig_lead_role_serializer.errors).get_failure_response() + return CustomResponse( + general_message=ig_lead_role_serializer.errors + ).get_failure_response() DiscordWebhooks.general_updates( WebHookCategory.INTEREST_GROUP.value, WebHookActions.CREATE.value, request_data.get("name"), - request_data.get("code") + request_data.get("code"), ) return CustomResponse( @@ -135,33 +153,39 @@ def put(self, request, pk): ig_new_code = ig.code ig_role = Role.objects.filter(title=ig_old_name).first() - + if ig_role: ig_role.title = ig_new_name ig_role.description = ig_new_name + " Interest Group Member" ig_role.save() - - ig_campus_lead_role = Role.objects.filter(title=RoleType.IG_CAMPUS_LEAD_ROLE(ig_old_code)).first() - + + ig_campus_lead_role = Role.objects.filter( + title=RoleType.IG_CAMPUS_LEAD_ROLE(ig_old_code) + ).first() + if ig_campus_lead_role: - ig_campus_lead_role.title = ig_new_code+' CampusLead' - ig_campus_lead_role.description = ig_new_name + " Interest Group Campus Lead" + ig_campus_lead_role.title = ig_new_code + " CampusLead" + ig_campus_lead_role.description = ( + ig_new_name + " Interest Group Campus Lead" + ) ig_campus_lead_role.save() - ig_lead_role = Role.objects.filter(title=RoleType.IG_LEAD_ROLE(ig_old_code)).first() + ig_lead_role = Role.objects.filter( + title=RoleType.IG_LEAD_ROLE(ig_old_code) + ).first() if ig_lead_role: ig_lead_role.title = RoleType.IG_LEAD_ROLE(ig_new_code) ig_lead_role.description = ig_new_name + " Interest Group Lead" ig_lead_role.save() - + DiscordWebhooks.general_updates( WebHookCategory.INTEREST_GROUP.value, WebHookActions.EDIT.value, ig_new_name, ig_new_code, ig_old_name, - ig_old_code + ig_old_code, ) return CustomResponse( response={"interestGroup": serializer.data} @@ -176,18 +200,23 @@ def delete(self, request, pk): if ig is None: return CustomResponse(general_message="invalid ig").get_success_response() ig_role = Role.objects.filter(title=ig.name).first() - if ig_role:ig_role.delete() - ig_campus_role = Role.objects.filter(title=RoleType.IG_CAMPUS_LEAD_ROLE(ig.code)).first() - if ig_campus_role:ig_campus_role.delete() + if ig_role: + ig_role.delete() + ig_campus_role = Role.objects.filter( + title=RoleType.IG_CAMPUS_LEAD_ROLE(ig.code) + ).first() + if ig_campus_role: + ig_campus_role.delete() ig_lead_role = Role.objects.filter(title=RoleType.IG_LEAD_ROLE(ig.code)).first() - if ig_lead_role:ig_lead_role.delete() + if ig_lead_role: + ig_lead_role.delete() ig.delete() DiscordWebhooks.general_updates( WebHookCategory.INTEREST_GROUP.value, WebHookActions.DELETE.value, ig.name, - ig.code + ig.code, ) return CustomResponse( general_message="ig deleted successfully" @@ -201,7 +230,8 @@ class InterestGroupCSV(APIView): def get(self, request): ig_serializer = ( InterestGroup.objects.select_related("created_by", "updated_by") - .prefetch_related("user_ig_link_ig").annotate(members=Count("user_ig_link_ig")) + .prefetch_related("user_ig_link_ig") + .annotate(members=Count("user_ig_link_ig")) .all() ) @@ -231,7 +261,12 @@ def get(self, request, pk): class InterestGroupListApi(APIView): def get(self, request): - ig = InterestGroup.objects.all() + ig = ( + InterestGroup.objects.all() + .select_related("created_by", "updated_by") + .prefetch_related("user_ig_link_ig") + .annotate(members=Count("user_ig_link_ig")) + ) serializer = InterestGroupSerializer(ig, many=True) diff --git a/api/dashboard/organisation/organisation_views.py b/api/dashboard/organisation/organisation_views.py index c1ecb6b1..f4d7a07e 100644 --- a/api/dashboard/organisation/organisation_views.py +++ b/api/dashboard/organisation/organisation_views.py @@ -728,7 +728,11 @@ class UnverifiedOrganizationsListAPI(APIView): permission_classes = [CustomizePermission] def get(self, request): - unverified_orgs = UnverifiedOrganization.objects.filter(verified__isnull=True) + unverified_orgs = ( + UnverifiedOrganization.objects.select_related("created_by", "department") + .filter(verified__isnull=True) + .order_by("-created_at") + ) seializer = UnverifiedOrganizationsSerializer(unverified_orgs, many=True) return CustomResponse(response=seializer.data).get_success_response() diff --git a/api/dashboard/organisation/serializers.py b/api/dashboard/organisation/serializers.py index b4429f01..067ee33b 100644 --- a/api/dashboard/organisation/serializers.py +++ b/api/dashboard/organisation/serializers.py @@ -460,10 +460,11 @@ def update(self, instance, validated_data): raise serializers.ValidationError( "Unable to assign organization to user" ) - print(validated_data.get("org_id")) UserOrganizationLink.objects.create( user_id=instance.created_by_id, org=validated_data.get("org_id"), + department_id=instance.department_id, + graduation_year=instance.graduation_year, verified=True, created_by_id=instance.verified_by_id, ) @@ -477,7 +478,16 @@ class Meta: class UnverifiedOrganizationsSerializer(serializers.ModelSerializer): id = serializers.CharField(read_only=True) created_by = serializers.CharField(source="created_by.full_name", read_only=True) + department = serializers.CharField(source="department.title", read_only=True) class Meta: model = UnverifiedOrganization - fields = ["id", "title", "org_type", "created_by", "created_at"] + fields = [ + "id", + "title", + "org_type", + "graduation_year", + "department", + "created_by", + "created_at", + ] diff --git a/api/dashboard/user/dash_user_views.py b/api/dashboard/user/dash_user_views.py index c7d8c875..287072f5 100644 --- a/api/dashboard/user/dash_user_views.py +++ b/api/dashboard/user/dash_user_views.py @@ -27,7 +27,7 @@ def get(self, 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) + cache.set(f"db_user_{user_muid}", user, timeout=60) if user is None: return CustomResponse( general_message="No user data available" diff --git a/api/register/register_views.py b/api/register/register_views.py index ed7d5345..84f886b5 100644 --- a/api/register/register_views.py +++ b/api/register/register_views.py @@ -231,7 +231,7 @@ def post(self, request): return CustomResponse(message=create_user.errors).get_failure_response() user = create_user.save() - cache.set(f"db_user_{user.muid}", user, timeout=20) + cache.set(f"db_user_{user.muid}", user, timeout=60) password = request.data["user"]["password"] cache.set(f"flag_register_{user.muid}", True, timeout=5) res_data = get_auth_token(user.muid, password) diff --git a/api/register/serializers.py b/api/register/serializers.py index 8af636f3..4f51202a 100644 --- a/api/register/serializers.py +++ b/api/register/serializers.py @@ -274,6 +274,11 @@ def create(self, validated_data): class UnverifiedOrganizationCreateSerializer(serializers.ModelSerializer): + graduation_year = serializers.IntegerField(required=False, allow_null=True) + department = serializers.PrimaryKeyRelatedField( + queryset=Department.objects.all(), required=False, allow_null=True + ) + def create(self, validated_data): validated_data["created_by_id"] = self.context.get("user_id") return UnverifiedOrganization.objects.create(**validated_data) @@ -285,7 +290,7 @@ def validate_org_type(self, org_type): class Meta: model = UnverifiedOrganization - fields = ["title", "org_type"] + fields = ["title", "org_type", "graduation_year", "department"] class UserSerializer(serializers.ModelSerializer): diff --git a/db/organization.py b/db/organization.py index 29066d55..690dbf36 100644 --- a/db/organization.py +++ b/db/organization.py @@ -224,6 +224,8 @@ class UnverifiedOrganization(models.Model): id = models.CharField(primary_key=True, max_length=36, default=lambda:str(uuid.uuid4())) title = models.CharField(max_length=100) org_type = models.CharField(max_length=25) + graduation_year = models.IntegerField(blank=True, null=True) + department = models.ForeignKey(Department, models.DO_NOTHING, related_name='unverified_organizations_dept', null=True) verified = models.BooleanField(null=True) verified_by = models.ForeignKey(User, models.DO_NOTHING, db_column='verified_by', related_name='unverified_organizations_verified_by', null=True) verified_at = models.DateTimeField(null=True) From 2602e91db08514887a781cb9cdf9b46b563e9e17 Mon Sep 17 00:00:00 2001 From: Aswanth Vc Date: Thu, 31 Oct 2024 21:25:06 +0530 Subject: [PATCH 2/2] feat(ig): cache interest groups --- api/dashboard/ig/dash_ig_view.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/api/dashboard/ig/dash_ig_view.py b/api/dashboard/ig/dash_ig_view.py index 78895519..908fc1b7 100644 --- a/api/dashboard/ig/dash_ig_view.py +++ b/api/dashboard/ig/dash_ig_view.py @@ -11,6 +11,8 @@ InterestGroupSerializer, InterestGroupCreateUpdateSerializer, ) +from django.utils.decorators import method_decorator +from django.views.decorators.cache import cache_page from api.dashboard.roles.dash_roles_serializer import RoleDashboardSerializer from db.user import Role @@ -260,6 +262,7 @@ def get(self, request, pk): class InterestGroupListApi(APIView): + @method_decorator(cache_page(60 * 10)) def get(self, request): ig = ( InterestGroup.objects.all()