diff --git a/api/launchpad/launchpad_views.py b/api/launchpad/launchpad_views.py index af988d3e..88b6c6ea 100644 --- a/api/launchpad/launchpad_views.py +++ b/api/launchpad/launchpad_views.py @@ -6,13 +6,16 @@ from .serializers import LaunchpadLeaderBoardSerializer, LaunchpadParticipantsSerializer, LaunchpadUserListSerializer,\ CollegeDataSerializer, LaunchpadUserSerializer, UserProfileUpdateSerializer, LaunchpadUpdateUserSerializer +from api.dashboard.profile.profile_serializer import UserProfileSerializer , LinkSocials ,UserLevelSerializer ,UserLogSerializer + from utils.response import CustomResponse from utils.utils import CommonUtils, ImportCSV from utils.types import LaunchPadLevels, LaunchPadRoles -from db.user import User, UserRoleLink +from utils.permission import JWTUtils +from db.user import User, UserRoleLink , Role , Socials from db.organization import UserOrganizationLink, Organization -from db.task import KarmaActivityLog -from db.launchpad import LaunchPadUsers, LaunchPadUserCollegeLink +from db.task import KarmaActivityLog , Level +from db.launchpad import LaunchPadUsers, LaunchPadUserCollegeLink , LaunchPad @@ -470,3 +473,120 @@ def post(self, request): if error: return CustomResponse(message=errors).get_failure_response() return CustomResponse(general_message="Successfully added users").get_success_response() + + +class LaunchPadListAdmin(APIView): + + def get(self, request): + data = request.data + auth_mail = data.pop('current_user', None) + auth_mail = auth_mail[0] if isinstance(auth_mail, list) else auth_mail + if not (auth_user := LaunchPadUsers.objects.filter(email=auth_mail, role=LaunchPadRoles.ADMIN.value).first()): + return CustomResponse(general_message="Unauthorized").get_failure_response() + total_karma_subquery = KarmaActivityLog.objects.filter( + user=OuterRef('id'), + task__event='launchpad', + appraiser_approved=True, + ).values('user').annotate( + total_karma=Sum('karma') + ).values('total_karma') + allowed_org_types = ["College", "School", "Company"] + + intro_task_completed_users = KarmaActivityLog.objects.filter( + task__event='launchpad', + appraiser_approved=True, + task__hashtag='#lp24-introduction', + ).values('user') + + latest_org_link = UserOrganizationLink.objects.filter( + user=OuterRef('id'), + org__org_type__in=allowed_org_types + ).order_by('-created_at').values('org__title')[:1] + + latest_district = UserOrganizationLink.objects.filter( + user=OuterRef('id'), + org__org_type__in=allowed_org_types + ).order_by('-created_at').values('org__district__name')[:1] + + latest_state = UserOrganizationLink.objects.filter( + user=OuterRef('id'), + org__org_type__in=allowed_org_types + ).order_by('-created_at').values('org__district__zone__state__name')[:1] + + users = User.objects.filter( + karma_activity_log_user__task__event="launchpad", + karma_activity_log_user__appraiser_approved=True, + id__in=intro_task_completed_users + ).annotate( + karma=Subquery(total_karma_subquery, output_field=IntegerField()), + org=Subquery(latest_org_link), + district_name=Subquery(latest_district), + state=Subquery(latest_state), + time_=Max("karma_activity_log_user__created_at"), + ).order_by("-karma", "time_") + + paginated_queryset = CommonUtils.get_paginated_queryset( + users, + request, + ["full_name", "karma", "org", "district_name", "state"] + ) + + serializer = LaunchpadLeaderBoardSerializer( + paginated_queryset.get("queryset"), many=True + ) + return CustomResponse().paginated_response( + data=serializer.data, pagination=paginated_queryset.get("pagination") + ) + + +class BaseAPI(APIView): + def get_authenticated_user(self, request, launchpad_id): + data = request.data + auth_mail = data.pop('current_user', None) + auth_mail = auth_mail[0] if isinstance(auth_mail, list) else auth_mail + if launchpad_id is None: + return None, CustomResponse(general_message="No launchpad id provided").get_failure_response() + if not (auth_user := LaunchPadUsers.objects.filter(email=auth_mail, role=LaunchPadRoles.ADMIN.value).first()): + return None, CustomResponse(general_message="Unauthorized").get_failure_response() + try: + user = LaunchPad.objects.get(launchpad_id=launchpad_id).user + except LaunchPad.DoesNotExist: + return None, CustomResponse(general_message="Invalid Launchpad ID").get_failure_response() + return user, None + +class UserProfileAPI(BaseAPI): + def get(self, request, launchpad_id=None): + user, response = self.get_authenticated_user(request, launchpad_id) + if response: + return response + serializer = UserProfileSerializer(user, many=False) + return CustomResponse(response=serializer.data).get_success_response() + +class GetSocialsAPI(BaseAPI): + def get(self, request, launchpad_id=None): + user, response = self.get_authenticated_user(request, launchpad_id) + if response: + return response + social_instance = Socials.objects.filter(user_id=user.id).first() + serializer = LinkSocials(instance=social_instance) + return CustomResponse(response=serializer.data).get_success_response() + +class UserLevelsAPI(BaseAPI): + def get(self, request, launchpad_id=None): + user, response = self.get_authenticated_user(request, launchpad_id) + if response: + return response + user_levels_link_query = Level.objects.all().order_by("level_order") + serializer = UserLevelSerializer(user_levels_link_query, many=True, context={"user_id": user.id}) + return CustomResponse(response=serializer.data).get_success_response() + +class UserLogAPI(BaseAPI): + def get(self, request, launchpad_id=None): + user, response = self.get_authenticated_user(request, launchpad_id) + if response: + return response + karma_activity_log = KarmaActivityLog.objects.filter(user=user.id, appraiser_approved=True).order_by("-created_at") + if not karma_activity_log: + return CustomResponse(general_message="No karma details available for user").get_success_response() + serializer = UserLogSerializer(karma_activity_log, many=True) + return CustomResponse(response=serializer.data).get_success_response() \ No newline at end of file diff --git a/api/launchpad/serializers.py b/api/launchpad/serializers.py index 741a7207..f9a5d899 100644 --- a/api/launchpad/serializers.py +++ b/api/launchpad/serializers.py @@ -7,10 +7,15 @@ from db.user import User from db.organization import UserOrganizationLink, Organization from db.task import KarmaActivityLog -from db.launchpad import LaunchPadUsers, LaunchPadUserCollegeLink +from db.launchpad import LaunchPadUsers, LaunchPadUserCollegeLink, LaunchPad from utils.types import LaunchPadRoles from utils.utils import DateTimeUtils +class LaunchPadIDSerializer(serializers.ModelSerializer): + class Meta: + model = LaunchPad + fields = ('launchpad_id') + class LaunchpadLeaderBoardSerializer(serializers.ModelSerializer): rank = serializers.SerializerMethodField() @@ -19,10 +24,11 @@ class LaunchpadLeaderBoardSerializer(serializers.ModelSerializer): org = serializers.CharField(allow_null=True, allow_blank=True) district_name = serializers.CharField(allow_null=True, allow_blank=True) state = serializers.CharField(allow_null=True, allow_blank=True) + launchpad_id = LaunchPadIDSerializer(read_only=True) class Meta: model = User - fields = ("rank", "full_name", "actual_karma", "karma", "org", "district_name", "state") + fields = ("rank", "full_name", "actual_karma", "karma", "org", "district_name", "state","launchpad_id") def get_rank(self, obj): total_karma_subquery = KarmaActivityLog.objects.filter( diff --git a/api/launchpad/urls.py b/api/launchpad/urls.py index 987c171b..03840284 100644 --- a/api/launchpad/urls.py +++ b/api/launchpad/urls.py @@ -13,4 +13,9 @@ path('user-profile/', launchpad_views.UserProfile.as_view()), path('user-college-data/', launchpad_views.UserBasedCollegeData.as_view()), path('bulk-user-college-link/', launchpad_views.BulkLaunchpadUser.as_view()), + path('list-participants-admin/', launchpad_views.LaunchPadListAdmin.as_view()), + path('user-profile//', launchpad_views.UserProfileAPI.as_view()), + path('socials//', launchpad_views.GetSocialsAPI.as_view()), + path('user-log//', launchpad_views.UserLogAPI.as_view()), + path('get-user-levels//', launchpad_views.UserLevelsAPI.as_view()), ] diff --git a/db/launchpad.py b/db/launchpad.py index f958db3e..66cbd3d5 100644 --- a/db/launchpad.py +++ b/db/launchpad.py @@ -1,5 +1,6 @@ from django.db import models - +from django.conf import settings +from db.user import User from db.organization import Organization @@ -30,4 +31,17 @@ class LaunchPadUserCollegeLink(models.Model): class Meta: managed = False - db_table = 'launchpad_user_college_link' \ No newline at end of file + db_table = 'launchpad_user_college_link' + +class LaunchPad(models.Model): + id = models.CharField(primary_key=True, max_length=36) + user = models.ForeignKey(User, on_delete=models.SET(settings.SYSTEM_ADMIN_ID), related_name="launchpad_user") + launchpad_id = models.CharField(max_length=100, unique=True) + created_at = models.DateTimeField(auto_now_add=True) + updated_at = models.DateTimeField(auto_now=True) + created_by = models.ForeignKey(User,on_delete=models.SET(settings.SYSTEM_ADMIN_ID), related_name="launchpad_created_by") + updated_by = models.ForeignKey(User,on_delete=models.SET(settings.SYSTEM_ADMIN_ID), related_name="launchpad_updated_by") + + class Meta: + managed = False + db_table = 'launchpad' \ No newline at end of file