diff --git a/api/dashboard/lc/dash_lc_serializer.py b/api/dashboard/lc/dash_lc_serializer.py index c7a93f42..670c1ef3 100644 --- a/api/dashboard/lc/dash_lc_serializer.py +++ b/api/dashboard/lc/dash_lc_serializer.py @@ -52,7 +52,8 @@ class LearningCircleMainSerializer(serializers.ModelSerializer): member_count = serializers.SerializerMethodField() members = serializers.SerializerMethodField() lead_name = serializers.SerializerMethodField() - + ismember = serializers.SerializerMethodField() + karma = serializers.SerializerMethodField() class Meta: model = LearningCircle fields = [ @@ -63,7 +64,9 @@ class Meta: 'members', 'meet_place', 'meet_time', - 'lead_name' + 'lead_name', + 'ismember', + 'karma' ] def get_lead_name(self, obj): @@ -86,12 +89,32 @@ def get_members(self, obj): circle=obj, accepted=1 ) + + def get_ismember(self, obj): + user_id = self.context.get('user_id') + return obj.user_circle_link_circle.filter( + circle=obj, + user_id=user_id, + accepted=True + ).exists() + return [ { 'username': f'{member.user.full_name}' } for member in user_circle_link ] + def get_karma(self, obj): + + karma_activity_log = KarmaActivityLog.objects.filter( + user__user_circle_link_user__circle=obj, + ).aggregate( + karma=Sum( + 'karma' + ) + )['karma'] + + return karma_activity_log if karma_activity_log else 0 class LearningCircleCreateSerializer(serializers.ModelSerializer): @@ -620,6 +643,30 @@ def create(self, validated_data): validated_data['circle_id'] = self.context.get('circle_id') validated_data['created_by_id'] = self.context.get('user_id') validated_data['updated_by_id'] = self.context.get('user_id') + attendees_list = validated_data['attendees'].split(',') + + user_id = self.context.get('user_id') + task = TaskList.objects.filter(hashtag=Lc.TASK_HASHTAG.value).first() + + for attendee_id in attendees_list: + # Send karma points + KarmaActivityLog.objects.create( + id=uuid.uuid4(), + user_id=attendee_id, + karma=Lc.KARMA.value, + task=task, + updated_by_id=user_id, + created_by_id=user_id, + ) + + # Update the wallet with karma points + wallet = Wallet.objects.filter(user_id=attendee_id).first() + wallet.karma += Lc.KARMA.value + wallet.karma_last_updated_at = DateTimeUtils.get_current_utc_time() + wallet.updated_at = DateTimeUtils.get_current_utc_time() + wallet.save() + + return CircleMeetingLog.objects.create(**validated_data) def update(self, instance, validated_data): @@ -649,6 +696,16 @@ def validate_attendees(self, attendees): wallet.karma_last_updated_at = DateTimeUtils.get_current_utc_time() wallet.updated_at = DateTimeUtils.get_current_utc_time() wallet.save() + # DiscordWebhooks.general_updates( + # WebHookCategory.KARMA_INFO.value, + # WebHookCategory.UPDATE.value, + # user, + # Lc.KARMA.value, + # task.title, + # task.hashtag + # ) + + return attendees def validate(self, data): diff --git a/api/dashboard/lc/dash_lc_view.py b/api/dashboard/lc/dash_lc_view.py index 7208294c..09e00004 100644 --- a/api/dashboard/lc/dash_lc_view.py +++ b/api/dashboard/lc/dash_lc_view.py @@ -7,7 +7,7 @@ from django.db.models.functions import Concat from django.shortcuts import redirect from rest_framework.views import APIView - +from rest_framework import authentication from api.notification.notifications_utils import NotificationUtils from db.learning_circle import LearningCircle, UserCircleLink, CircleMeetingLog @@ -15,7 +15,7 @@ from db.task import TaskList, KarmaActivityLog from utils.permission import JWTUtils from utils.response import CustomResponse -from utils.utils import send_template_mail, DateTimeUtils +from utils.utils import send_template_mail, DateTimeUtils,DiscordWebhooks from .dash_lc_serializer import LearningCircleSerializer, LearningCircleCreateSerializer, LearningCircleDetailsSerializer, \ LearningCircleUpdateSerializer, LearningCircleJoinSerializer, \ LearningCircleMainSerializer, LearningCircleNoteSerializer, LearningCircleStatsSerializer, \ @@ -60,35 +60,64 @@ def get(self, request): # Lists user's learning circle class LearningCircleMainApi(APIView): def post(self, request): all_circles = LearningCircle.objects.all() - ig_id = request.data.get('ig_id') - org_id = request.data.get('org_id') - district_id = request.data.get('district_id') + if JWTUtils.is_logged_in(request): + ig_id = request.data.get('ig_id') + org_id = request.data.get('org_id') + district_id = request.data.get('district_id') - if district_id: - all_circles = all_circles.filter(org__district_id=district_id) + if district_id: + all_circles = all_circles.filter(org__district_id=district_id) - if org_id: - all_circles = all_circles.filter(org_id=org_id) + if org_id: + all_circles = all_circles.filter(org_id=org_id) - if ig_id: - all_circles = all_circles.filter(ig_id=ig_id) + if ig_id: + all_circles = all_circles.filter(ig_id=ig_id) - if ig_id or org_id or district_id: - serializer = LearningCircleMainSerializer( - all_circles, - many=True - ) + if ig_id or org_id or district_id: + serializer = LearningCircleMainSerializer( + all_circles, + many=True + ) + else: + + + random_circles = all_circles.exclude( + Q(meet_time__isnull=True) | Q(meet_time='') and + Q(meet_place__isnull=True) | Q(meet_place='') + ).order_by('?')[:9] + + # random_circles = all_circles.order_by('?')[:9] + + serializer = LearningCircleMainSerializer( + random_circles, + many=True + ) + sorted_data = sorted(serializer.data, key=lambda x: x.get('karma', 0), reverse=True) + return CustomResponse( + response=sorted_data + ).get_success_response() else: - random_circles = all_circles.order_by('?')[:9] + + + random_circles = all_circles.exclude( + Q(meet_time__isnull=True) | Q(meet_time='') & + Q(meet_place__isnull=True) | Q(meet_place='') + ).order_by('?')[:9] serializer = LearningCircleMainSerializer( random_circles, many=True ) + # for ordered_dict in serializer.data: + # ordered_dict.pop('ismember', None) + sorted_data = sorted(serializer.data, key=lambda x: x.get('karma', 0), reverse=True) + return CustomResponse( + response=sorted_data + ).get_success_response() + + - return CustomResponse( - response=serializer.data - ).get_success_response() class LearningCircleStatsAPI(APIView): @@ -426,7 +455,8 @@ def get(self, request, circle_id, report_id=None): def post(self, request, circle_id): user_id = JWTUtils.fetch_user_id(request) time = request.data.get('time') - + + serializer = MeetRecordsCreateEditDeleteSerializer( data=request.data, context={ diff --git a/utils/permission.py b/utils/permission.py index 56d0976a..ff0b94dc 100644 --- a/utils/permission.py +++ b/utils/permission.py @@ -155,6 +155,14 @@ def is_jwt_authenticated(request): } ) from e + @staticmethod + def is_logged_in(request): + try: + JWTUtils.is_jwt_authenticated(request) + return True + except UnauthorizedAccessException: + return False + def role_required(roles): def decorator(view_func):