diff --git a/api/launchpad/launchpad_views.py b/api/launchpad/launchpad_views.py index ff31dee7..3ac8f237 100644 --- a/api/launchpad/launchpad_views.py +++ b/api/launchpad/launchpad_views.py @@ -1,12 +1,12 @@ -from django.db.models import Sum, Max, Prefetch, F, OuterRef, Subquery, IntegerField +from django.db.models import Sum, Max, Prefetch, F, OuterRef, Subquery, IntegerField, Count, Q from rest_framework.views import APIView -from .serializers import LaunchpadLeaderBoardSerializer, LaunchpadParticipantsSerializer +from .serializers import LaunchpadLeaderBoardSerializer, LaunchpadParticipantsSerializer, CollegeDataSerializer from utils.response import CustomResponse from utils.utils import CommonUtils from db.user import User, UserRoleLink -from db.organization import UserOrganizationLink +from db.organization import UserOrganizationLink, Organization from db.task import KarmaActivityLog @@ -174,3 +174,58 @@ def get(self, request): } return CustomResponse(response=level_counts).get_success_response() + +class CollegeData(APIView): + def get(self, request): + allowed_levels = [ + "IEEE Launchpad Level 1", + "IEEE Launchpad Level 2", + "IEEE Launchpad Level 3", + "IEEE Launchpad Level 4" + ] + + org = Organization.objects.filter( + org_type="College", + ).prefetch_related( + Prefetch( + "user_organization_link_org", + queryset=UserOrganizationLink.objects.filter( + user__user_role_link_user__role__title__in=allowed_levels + ) + ) + ).filter( + user_organization_link_org__user__user_role_link_user__role__title__in=allowed_levels + ).annotate( + district_name=F("district__name"), + state=F("district__zone__state__name"), + total_users=Count("user_organization_link_org__user"), + level1 = Count( + "user_organization_link_org__user", + filter=Q(user_organization_link_org__user__user_role_link_user__role__title="IEEE Launchpad Level 1") + ), + level2 = Count( + "user_organization_link_org__user", + filter=Q(user_organization_link_org__user__user_role_link_user__role__title="IEEE Launchpad Level 2") + ), + level3 = Count( + "user_organization_link_org__user", + filter=Q(user_organization_link_org__user__user_role_link_user__role__title="IEEE Launchpad Level 3") + ), + level4 = Count( + "user_organization_link_org__user", + filter=Q(user_organization_link_org__user__user_role_link_user__role__title="IEEE Launchpad Level 4") + ) + ).order_by("-total_users") + + paginated_queryset = CommonUtils.get_paginated_queryset( + org, + request, + ["title", "district_name", "state"] + ) + + serializer = CollegeDataSerializer( + paginated_queryset.get("queryset"), many=True + ) + return CustomResponse().paginated_response( + data=serializer.data, pagination=paginated_queryset.get("pagination") + ) diff --git a/api/launchpad/serializers.py b/api/launchpad/serializers.py index 59c79785..6da2dca5 100644 --- a/api/launchpad/serializers.py +++ b/api/launchpad/serializers.py @@ -3,7 +3,7 @@ from rest_framework import serializers from db.user import User -from db.organization import UserOrganizationLink +from db.organization import UserOrganizationLink, Organization from db.task import KarmaActivityLog @@ -72,3 +72,26 @@ class LaunchpadParticipantsSerializer(serializers.ModelSerializer): class Meta: model = User fields = ("full_name", "level", "org", "district_name", "state") + + +class CollegeDataSerializer(serializers.ModelSerializer): + district_name = serializers.CharField() + state = serializers.CharField() + total_users = serializers.IntegerField() + level1 = serializers.IntegerField() + level2 = serializers.IntegerField() + level3 = serializers.IntegerField() + level4 = serializers.IntegerField() + + class Meta: + model = Organization + fields = ( + "title", + "district_name", + "state", + "total_users", + "level1", + "level2", + "level3", + "level4" + ) diff --git a/api/launchpad/urls.py b/api/launchpad/urls.py index 093c6ec2..6ac7a4b5 100644 --- a/api/launchpad/urls.py +++ b/api/launchpad/urls.py @@ -5,5 +5,6 @@ urlpatterns = [ path('leaderboard/', launchpad_views.Leaderboard.as_view()), path('list-participants/', launchpad_views.ListParticipantsAPI.as_view()), - path('launchpad-details/', launchpad_views.LaunchpadDetailsCount.as_view()) -] \ No newline at end of file + path('launchpad-details/', launchpad_views.LaunchpadDetailsCount.as_view()), + path('college-data/', launchpad_views.CollegeData.as_view()) +]