Skip to content

Commit

Permalink
Merge pull request #1615 from SurajSuresh123/dev
Browse files Browse the repository at this point in the history
[FIX] college api lead_id bug
  • Loading branch information
shaheenhyderk authored Nov 26, 2023
2 parents 4dc15d9 + d9575fa commit a078267
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
28 changes: 23 additions & 5 deletions api/dashboard/college/college_view.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from rest_framework.views import APIView

from db.organization import College, OrgDiscordLink
from utils.types import OrganizationType, RoleType
from db.organization import College
from db.user import User
from utils.permission import JWTUtils
from utils.response import CustomResponse
from .serializer import (
Expand All @@ -9,25 +10,42 @@
CollegeEditSerializer,
)
from utils.utils import CommonUtils

from django.db.models import Case,When,CharField,F

class CollegeApi(APIView):
def get(self, request, college_code=None):
if college_code:
colleges = College.objects.filter(id=college_code)
else:
colleges = College.objects.all().select_related(
"created_by", "updated_by", "org", "lead"
"created_by", "updated_by", "org"
)

leads = (
User.objects.filter(
user_organization_link_user__org__org_type=OrganizationType.COLLEGE.value,
user_role_link_user__role__title=RoleType.CAMPUS_LEAD.value,
)
.distinct()
.annotate(
college=Case(
When(
user_organization_link_user__org__org_type=OrganizationType.COLLEGE.value,
then=F("user_organization_link_user__org__id"),
),
default=None,
output_field=CharField(),
)
)
)
paginated_queryset = CommonUtils.get_paginated_queryset(
colleges,
request,
search_fields=["created_by__firstname"],
sort_fields={"created_by": "created_by__firstname"},
)
serializer = CollegeListSerializer(
paginated_queryset.get("queryset"), many=True
paginated_queryset.get("queryset"), many=True,context={"leads":leads}
)

return CustomResponse().paginated_response(
Expand Down
14 changes: 10 additions & 4 deletions api/dashboard/college/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ class CollegeListSerializer(serializers.ModelSerializer):
updated_by = serializers.CharField(source="updated_by.fullname")
org = serializers.CharField(source="org.title")
number_of_students = serializers.SerializerMethodField()
lead_name = serializers.CharField(source="lead.fullname", default=None)
lead_contact = serializers.CharField(source="lead.mobile", default=None)
total_karma = serializers.SerializerMethodField()
no_of_lc = serializers.SerializerMethodField()

Expand All @@ -28,8 +26,6 @@ class Meta:
"updated_at",
"created_at",
"number_of_students",
"lead_name",
"lead_contact",
"total_karma",
"no_of_lc",
]
Expand All @@ -53,6 +49,16 @@ def get_total_karma(self, obj):
or 0
)

# def get_lead_name(self, obj):
# leads = self.context.get("leads")
# college_lead = [lead for lead in leads if lead.college == obj.id]
# return college_lead.fullname if college_lead else None

# def get_lead_contact(self, obj):
# leads = self.context.get("leads")
# college_lead = [lead for lead in leads if lead.college == obj.id]
# return college_lead.mobile if college_lead else None


class CollegeCreateDeleteSerializer(serializers.ModelSerializer):
class Meta:
Expand Down
2 changes: 1 addition & 1 deletion db/organization.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class College(models.Model):
updated_at = models.DateTimeField(auto_now=True)
created_by = models.ForeignKey(User, on_delete=models.CASCADE, db_column='created_by', related_name='college_created_by')
created_at = models.DateTimeField(auto_now_add=True)
lead = models.ForeignKey(User, on_delete=models.CASCADE, related_name='college_lead_name', blank=True, null=True)

class Meta:
managed = False
db_table = 'college'
Expand Down

0 comments on commit a078267

Please sign in to comment.