Skip to content

Commit

Permalink
Merge pull request #1843 from ananthu666/dev
Browse files Browse the repository at this point in the history
listing the lcs
  • Loading branch information
adnankattekaden authored Dec 21, 2023
2 parents e3f316f + 2b3f3b3 commit 41544aa
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 23 deletions.
61 changes: 59 additions & 2 deletions api/dashboard/lc/dash_lc_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -63,7 +64,9 @@ class Meta:
'members',
'meet_place',
'meet_time',
'lead_name'
'lead_name',
'ismember',
'karma'
]

def get_lead_name(self, obj):
Expand All @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down
72 changes: 51 additions & 21 deletions api/dashboard/lc/dash_lc_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
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
from db.user import User
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, \
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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={
Expand Down
8 changes: 8 additions & 0 deletions utils/permission.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 41544aa

Please sign in to comment.