Skip to content

Commit

Permalink
Merge pull request #1518 from gtech-mulearn/dev-server
Browse files Browse the repository at this point in the history
fix lc
  • Loading branch information
adnankattekaden authored Nov 4, 2023
2 parents e85203d + daa3500 commit 3a1ffbf
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 96 deletions.
19 changes: 11 additions & 8 deletions api/common/common_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.db.models import Sum, F, Case, When, Value, CharField, Count, Q
from django.db.models.functions import Coalesce
from rest_framework.views import APIView

from django.db import models
from db.learning_circle import LearningCircle
from db.learning_circle import UserCircleLink
from db.organization import Organization
Expand All @@ -14,7 +14,7 @@
from utils.types import IntegrationType, OrganizationType, RoleType
from utils.utils import CommonUtils
from .serializer import StudentInfoSerializer, CollegeInfoSerializer

from django.db.models import Count, Subquery, OuterRef

class LcDashboardAPI(APIView):
def get(self, request):
Expand All @@ -27,20 +27,23 @@ def get(self, request):
learning_circle_count = LearningCircle.objects.all().count()

total_no_enrollment = UserCircleLink.objects.filter(accepted=True).count()
user_circle_link_count = UserCircleLink.objects.filter(circle=OuterRef('pk')).values('circle_id').annotate(
total_users=Count('id')).values('total_users')

query = InterestGroup.objects.annotate(
total_circles=Count("learningcircle"),
total_users=Count("learningcircle__usercirclelink__user", distinct=True),
total_circles=Count("learning_circle_ig", distinct=True),
total_users=Subquery(user_circle_link_count, output_field=models.IntegerField())
).values("name", "total_circles", "total_users")

circle_count_by_ig = (
query.values("name")
.order_by("name")
.annotate(
total_circles=Count("learningcircle", distinct=True),
total_users=Count(
"learningcircle__usercirclelink__user", distinct=True
),
total_circles=Count("learning_circle_ig", distinct=True),
total_users=Count("learning_circle_ig__user_circle_link_circle", distinct=True),
)
)

unique_user_count = (
UserCircleLink.objects.filter(accepted=True)
.values("user")
Expand Down
2 changes: 1 addition & 1 deletion api/common/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
path('college-wise-lc-report/csv/', common_views.CollegeWiseLcReportCSV.as_view()),
path('lc-report/csv/', common_views.LcReportDownloadAPI.as_view()),
path('global-count/', common_views.GlobalCountAPI.as_view()),
path('gta-sandshore/',common_views.GTASANDSHOREAPI.as_view())
path('gta-sandshore/', common_views.GTASANDSHOREAPI.as_view())
]
53 changes: 32 additions & 21 deletions api/dashboard/lc/dash_lc_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
from rest_framework import serializers

from db.learning_circle import LearningCircle, UserCircleLink, InterestGroup, CircleMeetingLog
from db.task import TaskList
from db.organization import UserOrganizationLink
from db.task import KarmaActivityLog
from utils.types import OrganizationType
from utils.utils import DateTimeUtils
from utils.types import Lc


class LearningCircleSerializer(serializers.ModelSerializer):
Expand All @@ -17,12 +19,6 @@ class LearningCircleSerializer(serializers.ModelSerializer):
org = serializers.CharField(source='org.title', allow_null=True)
member_count = serializers.SerializerMethodField()

def get_member_count(self, obj):
return UserCircleLink.objects.filter(
circle_id=obj.id,
accepted=1
).count()

class Meta:
model = LearningCircle
fields = [
Expand All @@ -40,20 +36,14 @@ class Meta:
"member_count",
]

def get_member_count(self, obj):
return obj.user_circle_link_circle.filter(
circle_id=obj.id,
accepted=1
).count()


class LearningCircleCreateSerializer(serializers.ModelSerializer):
ig = serializers.CharField(
required=True,
error_messages={
'required': 'ig field must not be left blank.'
}
)
name = serializers.CharField(
required=True,
error_messages={
'required': 'name field must not be left blank.'
}
)

class Meta:
model = LearningCircle
Expand All @@ -64,8 +54,8 @@ class Meta:

def validate(self, data):
user_id = self.context.get('user_id')

ig_id = data.get('ig')

if not InterestGroup.objects.filter(
id=ig_id
).exists():
Expand All @@ -87,11 +77,11 @@ def validate(self, data):
raise serializers.ValidationError(
"Already a member of a learning circle with the same interest group"
)

return data

def create(self, validated_data):
user_id = self.context.get('user_id')

org_link = UserOrganizationLink.objects.filter(
user_id=user_id,
org__org_type=OrganizationType.COLLEGE.value
Expand Down Expand Up @@ -516,7 +506,7 @@ class Meta:
]

def create(self, validated_data):
print()
validated_data['id'] = uuid.uuid4()
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')
Expand All @@ -526,3 +516,24 @@ def update(self, instance, validated_data):
instance.updated_by_id = self.context.get('user_id')
instance.save()
return instance

def validate_attendees(self, attendees):
task = TaskList.objects.filter(hashtag=Lc.TASK_HASHTAG.value).first()

attendees = attendees.split(',')

user_id = self.context.get('user_id')

KarmaActivityLog.objects.bulk_create([
KarmaActivityLog(
id=uuid.uuid4(),
user_id=user,
karma=Lc.KARMA.value,
task_id=task.id,
updated_by_id=user_id,
created_by_id=user_id,
)
for user in attendees
])

return attendees
99 changes: 62 additions & 37 deletions api/dashboard/lc/dash_lc_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,34 @@
from_mail = config("FROM_MAIL")


class LearningCircleListApi(APIView):
"""
API endpoint for listing a user's learning circles.
Endpoint: /api/v1/dashboard/lc/ (GET)
Returns:
CustomResponse: A custom response containing a list of learning circles
associated with the user.
"""
def get(self, request): # Lists user's learning circle
user_id = JWTUtils.fetch_user_id(request)

learning_queryset = LearningCircle.objects.filter(
user_circle_link_circle__user_id=user_id,
user_circle_link_circle__accepted=1
)

learning_serializer = LearningCircleSerializer(
learning_queryset,
many=True
)

return CustomResponse(
response=learning_serializer.data
).get_success_response()


class TotalLearningCircleListApi(APIView):
def post(self, request, circle_code=None):
user_id = JWTUtils.fetch_user_id(request)
Expand Down Expand Up @@ -99,49 +127,41 @@ class LearningCircleJoinApi(APIView):
def post(self, request, circle_id):
user_id = JWTUtils.fetch_user_id(request)
user = User.objects.filter(id=user_id).first()

full_name = f'{user.first_name} {user.last_name}' if user.last_name else user.first_name
lc = UserCircleLink.objects.filter(circle_id=circle_id, lead=True).first()
serializer = LearningCircleJoinSerializer(data=request.data,
context={ 'user_id': user_id, 'circle_id': circle_id })

lc = UserCircleLink.objects.filter(
circle_id=circle_id,
lead=True
).first()

serializer = LearningCircleJoinSerializer(
data=request.data,
context={
'user_id': user_id,
'circle_id': circle_id
}
)
if serializer.is_valid():
serializer.save()
lead_obj = User.objects.filter(id=lc.user.id).first()
NotificationUtils.insert_notification(user=lead_obj,
title="Member Request",
description=f"{full_name} has requested to join your learning circle",
button="LC",
url=f'{domain}/api/v1/dashboard/lc/{circle_id}/{user_id}/',
created_by=user)
return CustomResponse(general_message='Request sent').get_success_response()
return CustomResponse(message=serializer.errors).get_failure_response()


class UserLearningCircleListApi(APIView):
"""
API endpoint for listing a user's learning circles.
Endpoint: /api/v1/dashboard/lc/ (GET)
Returns:
CustomResponse: A custom response containing a list of learning circles
associated with the user.
"""
def get(self, request): # Lists user's learning circle
user_id = JWTUtils.fetch_user_id(request)

learning_queryset = LearningCircle.objects.filter(
user_circle_link_circle__user_id=user_id,
user_circle_link_circle__accepted=1
)
NotificationUtils.insert_notification(
user=lead_obj,
title="Member Request",
description=f"{full_name} has requested to join your learning circle",
button="LC",
url=f'{domain}/api/v1/dashboard/lc/{circle_id}/{user_id}/',
created_by=user
)

learning_serializer = LearningCircleSerializer(
learning_queryset,
many=True
)
return CustomResponse(
general_message='Request sent'
).get_success_response()

return CustomResponse(
response=learning_serializer.data
).get_success_response()
message=serializer.errors
).get_failure_response()


class MeetCreateEditDeleteAPI(APIView):
Expand Down Expand Up @@ -241,6 +261,7 @@ def get(self, request, circle_id, member_id=None):
if not LearningCircle.objects.filter(
id=circle_id
).exists():

return CustomResponse(
general_message='Learning Circle not found'
).get_failure_response()
Expand Down Expand Up @@ -345,8 +366,12 @@ def put(self, request, circle_id):
serializer = LearningCircleNoteSerializer(learning_circle, data=request.data)
if serializer.is_valid():
serializer.save()
return CustomResponse(general_message='Note updated successfully').get_success_response()
return CustomResponse(message=serializer.errors).get_failure_response()
return CustomResponse(
general_message='Note updated successfully').get_success_response()

return CustomResponse(
message=serializer.errors
).get_failure_response()

def delete(self, request, circle_id):
user_id = JWTUtils.fetch_user_id(request)
Expand Down
2 changes: 1 addition & 1 deletion api/dashboard/lc/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from . import dash_lc_view

urlpatterns = [
path('', dash_lc_view.UserLearningCircleListApi.as_view()),
path('', dash_lc_view.LearningCircleListApi.as_view()),
path('list/', dash_lc_view.LearningCircleMainApi.as_view()),
path('data/', dash_lc_view.LearningCircleDataAPI.as_view()),
path('list-all/', dash_lc_view.TotalLearningCircleListApi.as_view()),
Expand Down
2 changes: 1 addition & 1 deletion api/dashboard/user/dash_user_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def get(self, request):
).get_success_response()


class UserEditAPI(APIView):
class UserGetPatchDeleteAPI(APIView):
authentication_classes = [CustomizePermission]

@role_required([RoleType.ADMIN.value])
Expand Down
6 changes: 3 additions & 3 deletions api/dashboard/user/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
path('csv/', dash_user_views.UserManagementCSV.as_view(), name="csv-user"),
path('', dash_user_views.UserAPI.as_view(), name='list-user'),

path('<str:user_id>/', dash_user_views.UserEditAPI.as_view(), name="detail-user"),
path('<str:user_id>/', dash_user_views.UserEditAPI.as_view(), name="edit-user"),
path('<str:user_id>/', dash_user_views.UserEditAPI.as_view(), name="delete-user"),
path('<str:user_id>/', dash_user_views.UserGetPatchDeleteAPI.as_view(), name="detail-user"),
path('<str:user_id>/', dash_user_views.UserGetPatchDeleteAPI.as_view(), name="edit-user"),
path('<str:user_id>/', dash_user_views.UserGetPatchDeleteAPI.as_view(), name="delete-user"),


]
Loading

0 comments on commit 3a1ffbf

Please sign in to comment.