Skip to content

Commit

Permalink
Merge pull request #1445 from gtech-mulearn:dev
Browse files Browse the repository at this point in the history
[FEAT] Location Searching
  • Loading branch information
MZaFaRM authored Oct 29, 2023
2 parents 0a51ff0 + 89b3c1d commit c45995c
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 118 deletions.
259 changes: 165 additions & 94 deletions api/common/common_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,154 +11,225 @@
from db.task import InterestGroup
from db.user import User, UserRoleLink
from utils.response import CustomResponse
from utils.types import IntegrationType, OrganizationType
from utils.types import IntegrationType, OrganizationType, RoleType
from utils.utils import CommonUtils
from .serializer import StudentInfoSerializer


class LcDashboardAPI(APIView):

def get(self, request):
date = request.GET.get('date')
date = request.GET.get("date")
if date:
learning_circle_count = LearningCircle.objects.filter(created_at__gt=date).count()
learning_circle_count = LearningCircle.objects.filter(
created_at__gt=date
).count()
else:
learning_circle_count = LearningCircle.objects.all().count()

total_no_enrollment = UserCircleLink.objects.filter(accepted=True).count()
query = InterestGroup.objects.annotate(
total_circles=Count('learningcircle'),
total_users=Count('learningcircle__usercirclelink__user', distinct=True)
).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("learningcircle"),
total_users=Count("learningcircle__usercirclelink__user", distinct=True),
).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
),
)
)
unique_user_count = (
UserCircleLink.objects.filter(accepted=True)
.values("user")
.distinct()
.count()
)
unique_user_count = UserCircleLink.objects.filter(accepted=True).values('user').distinct().count()

return CustomResponse(response={'lc_count': learning_circle_count, 'total_enrollment': total_no_enrollment,
'circle_count_by_ig': circle_count_by_ig,
'unique_users': unique_user_count}).get_success_response()
return CustomResponse(
response={
"lc_count": learning_circle_count,
"total_enrollment": total_no_enrollment,
"circle_count_by_ig": circle_count_by_ig,
"unique_users": unique_user_count,
}
).get_success_response()


class LcReportAPI(APIView):

def get(self, request):
date = request.GET.get('date')
date = request.GET.get("date")
if date:
student_info = UserCircleLink.objects.filter(
accepted=True,
user__user_organization_link_user__org__org_type=OrganizationType.COLLEGE.value,
created_at=date,
).values(
first_name=F('user__first_name'),
last_name=F('user__last_name'),
muid=F('user__muid'),
circle_name=F('circle__name'),
circle_ig=F('circle__ig__name'),
organisation=F('user__user_organization_link_user__org__title'),
dwms_id=Case(
When(
user__integration_authorization_user__integration__name=IntegrationType.KKEM.value,
then=F('user__integration_authorization_user__additional_field')
student_info = (
UserCircleLink.objects.filter(
accepted=True,
user__user_organization_link_user__org__org_type=OrganizationType.COLLEGE.value,
created_at=date,
)
.values(
first_name=F("user__first_name"),
last_name=F("user__last_name"),
muid=F("user__muid"),
circle_name=F("circle__name"),
circle_ig=F("circle__ig__name"),
organisation=F("user__user_organization_link_user__org__title"),
dwms_id=Case(
When(
user__integration_authorization_user__integration__name=IntegrationType.KKEM.value,
then=F(
"user__integration_authorization_user__additional_field"
),
),
default=Value(None, output_field=CharField()),
output_field=CharField(),
),
default=Value(None, output_field=CharField()),
output_field=CharField()
)
).annotate(karma_earned=Sum('user__karma_activity_log_user__task__karma',
filter=Q(user__karma_activity_log_user__task__ig=F('circle__ig'))))
.annotate(
karma_earned=Sum(
"user__karma_activity_log_user__task__karma",
filter=Q(
user__karma_activity_log_user__task__ig=F("circle__ig")
),
)
)
)
else:
student_info = UserCircleLink.objects.filter(
accepted=True,
user__user_organization_link_user__org__org_type=OrganizationType.COLLEGE.value
).values(
first_name=F('user__first_name'),
last_name=F('user__last_name'),
muid=F('user__muid'),
circle_name=F('circle__name'),
circle_ig=F('circle__ig__name'),
organisation=F('user__user_organization_link_user__org__title'),
dwms_id=Case(
When(
user__integration_authorization_user__integration__name=IntegrationType.KKEM.value,
then=F('user__integration_authorization_user__additional_field')
student_info = (
UserCircleLink.objects.filter(
accepted=True,
user__user_organization_link_user__org__org_type=OrganizationType.COLLEGE.value,
)
.values(
first_name=F("user__first_name"),
last_name=F("user__last_name"),
muid=F("user__muid"),
circle_name=F("circle__name"),
circle_ig=F("circle__ig__name"),
organisation=F("user__user_organization_link_user__org__title"),
dwms_id=Case(
When(
user__integration_authorization_user__integration__name=IntegrationType.KKEM.value,
then=F(
"user__integration_authorization_user__additional_field"
),
),
default=Value(None, output_field=CharField()),
output_field=CharField(),
),
default=Value(None, output_field=CharField()),
output_field=CharField()
)
).annotate(karma_earned=Sum('user__karma_activity_log_user__task__karma',
filter=Q(user__karma_activity_log_user__task__ig=F('circle__ig'))))
.annotate(
karma_earned=Sum(
"user__karma_activity_log_user__task__karma",
filter=Q(
user__karma_activity_log_user__task__ig=F("circle__ig")
),
)
)
)

paginated_queryset = CommonUtils.get_paginated_queryset(student_info, request,
search_fields=['first_name', 'last_name', 'muid'],
sort_fields={'first_name': 'first_name',
'muid': 'muid'})
paginated_queryset = CommonUtils.get_paginated_queryset(
student_info,
request,
search_fields=["first_name", "last_name", "muid"],
sort_fields={"first_name": "first_name", "muid": "muid"},
)

student_info_data = StudentInfoSerializer(paginated_queryset.get('queryset'), many=True).data
student_info_data = StudentInfoSerializer(
paginated_queryset.get("queryset"), many=True
).data

return CustomResponse().paginated_response(data=student_info_data,
pagination=paginated_queryset.get('pagination'))
return CustomResponse().paginated_response(
data=student_info_data, pagination=paginated_queryset.get("pagination")
)


class LcReportDownloadAPI(APIView):

def get(self, request):
student_info = UserCircleLink.objects.filter(lead=False, accepted=True,
user__user_organization_link_user__org__org_type=OrganizationType.COLLEGE.value).values(
first_name=F('user__first_name'),
last_name=F('user__last_name'),
muid=F('user__muid'),
circle_name=F('circle__name'),
circle_ig=F('circle__ig__name'),
organisation=F('user__user_organization_link_user__org__title'),
dwms_id=Case(
When(
user__integration_authorization_user__integration__name=IntegrationType.KKEM.value,
then=F('user__integration_authorization_user__additional_field')
student_info = (
UserCircleLink.objects.filter(
lead=False,
accepted=True,
user__user_organization_link_user__org__org_type=OrganizationType.COLLEGE.value,
)
.values(
first_name=F("user__first_name"),
last_name=F("user__last_name"),
muid=F("user__muid"),
circle_name=F("circle__name"),
circle_ig=F("circle__ig__name"),
organisation=F("user__user_organization_link_user__org__title"),
dwms_id=Case(
When(
user__integration_authorization_user__integration__name=IntegrationType.KKEM.value,
then=F(
"user__integration_authorization_user__additional_field"
),
),
default=Value(None, output_field=CharField()),
output_field=CharField(),
),
default=Value(None, output_field=CharField()),
output_field=CharField()
)
).annotate(karma_earned=Sum('user__karma_activity_log_user__task__karma',
filter=Q(user__karma_activity_log_user__task__ig=F('circle__ig'))))
.annotate(
karma_earned=Sum(
"user__karma_activity_log_user__task__karma",
filter=Q(user__karma_activity_log_user__task__ig=F("circle__ig")),
)
)
)

student_info_data = StudentInfoSerializer(student_info, many=True).data

return CommonUtils.generate_csv(student_info_data, "Learning Circle Report")


class CollegeWiseLcReport(APIView):

def get(self, request):
learning_circles_info = LearningCircle.objects.filter(org__org_type=OrganizationType.COLLEGE.value) \
.values(org_title=F('org__title')) \
.annotate(learning_circle_count=Count('id'), user_count=Count('usercirclelink')) \
.order_by('org_title')
learning_circles_info = (
LearningCircle.objects.filter(org__org_type=OrganizationType.COLLEGE.value)
.values(org_title=F("org__title"))
.annotate(
learning_circle_count=Count("id"), user_count=Count("usercirclelink")
)
.order_by("org_title")
)

return CustomResponse(response=learning_circles_info).get_success_response()


class GlobalCountAPI(APIView):

def get(self, request):
members_count = User.objects.all().count()
org_type_counts = Organization.objects.filter(
org_type__in=[OrganizationType.COLLEGE.value, OrganizationType.COMPANY.value,
OrganizationType.COMMUNITY.value]
).values('org_type').annotate(org_count=Coalesce(Count('org_type'), 0))
org_type_counts = (
Organization.objects.filter(
org_type__in=[
OrganizationType.COLLEGE.value,
OrganizationType.COMPANY.value,
OrganizationType.COMMUNITY.value,
]
)
.values("org_type")
.annotate(org_count=Coalesce(Count("org_type"), 0))
)

enablers_mentors_count = UserRoleLink.objects.filter(role__title__in=["Mentor", "Enabler"]).values(
'role__title').annotate(role_count=Coalesce(Count('role__title'), 0))
enablers_mentors_count = (
UserRoleLink.objects.filter(
role__title__in=[RoleType.MENTOR.value, RoleType.ENABLER.value]
)
.values("role__title")
.annotate(role_count=Coalesce(Count("role__title"), 0))
)

interest_groups_count = InterestGroup.objects.all().count()
learning_circles_count = LearningCircle.objects.all().count()

data = {
'members': members_count,
'org_type_counts': org_type_counts,
'enablers_mentors_count': enablers_mentors_count,
'ig_count': interest_groups_count,
'learning_circle_count': learning_circles_count
"members": members_count,
"org_type_counts": org_type_counts,
"enablers_mentors_count": enablers_mentors_count,
"ig_count": interest_groups_count,
"learning_circle_count": learning_circles_count,
}
return CustomResponse(response=data).get_success_response()

Expand Down
3 changes: 0 additions & 3 deletions api/dashboard/ig/dash_ig_serializer.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import uuid

from rest_framework import serializers

from db.task import InterestGroup
from db.user import User
from utils.utils import DateTimeUtils


class InterestGroupSerializer(serializers.ModelSerializer):
Expand Down
26 changes: 26 additions & 0 deletions api/register/register_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,29 @@ def get(self, request):
zone_serializer = serializers.UserZoneSerializer(zone_object, many=True).data

return CustomResponse(response=zone_serializer).get_success_response()


class LocationSearchView(APIView):
def get(self, request):
query = request.GET.get("q")
MAX_RESULTS = 5

if not query:
return CustomResponse(
general_message="Query parameter 'q' is required"
).get_failure_response()

queries = [q.strip() for q in query.split(",")]

# Building the Q object for the OR-based lookup
query_filter = Q()
for q in queries:
query_filter |= Q(name__icontains=q)
query_filter |= Q(zone__name__icontains=q)
query_filter |= Q(zone__state__name__icontains=q)
query_filter |= Q(zone__state__country__name__icontains=q)

districts = District.objects.filter(query_filter)[:MAX_RESULTS]
all_districts = serializers.LocationSerializer(districts, many=True).data

return CustomResponse(response=all_districts).get_success_response()
13 changes: 12 additions & 1 deletion api/register/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class ReferralSerializer(serializers.ModelSerializer):
class Meta:
model = UserReferralLink
fields = ["muid", "user", "invite_code", "is_coin"]

def to_representation(self, instance):
data = super().to_representation(instance)
if "muid" in data:
Expand Down Expand Up @@ -331,3 +331,14 @@ class UserZoneSerializer(serializers.ModelSerializer):
class Meta:
model = Zone
fields = ["zone_name"]


class LocationSerializer(serializers.ModelSerializer):
location = serializers.SerializerMethodField()

class Meta:
model = District
fields = ("id", "location")

def get_location(self, obj):
return f"{obj.name}, {obj.zone.name}, {obj.zone.state.name}, {obj.zone.state.country.name}"
1 change: 1 addition & 0 deletions api/register/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
path("colleges/", register_views.CollegesAPI.as_view()),
path("department/list/", register_views.DepartmentAPI.as_view()),

path("location/", register_views.LocationSearchView.as_view()),

path("country/list/", register_views.CountryAPI.as_view()),
path("state/list/", register_views.StateAPI.as_view()),
Expand Down
Loading

0 comments on commit c45995c

Please sign in to comment.