mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-07-15 18:07:02 +00:00
136 lines
3.6 KiB
Python
136 lines
3.6 KiB
Python
"""
|
|
web_admin analytics serializer file
|
|
"""
|
|
# third party imports
|
|
from rest_framework import serializers
|
|
|
|
# django imports
|
|
from django.contrib.auth import get_user_model
|
|
|
|
# local imports
|
|
from base.constants import USER_TYPE
|
|
|
|
from junior.models import JuniorPoints, Junior
|
|
|
|
USER = get_user_model()
|
|
|
|
|
|
class JuniorLeaderboardSerializer(serializers.ModelSerializer):
|
|
"""
|
|
junior leaderboard serializer
|
|
"""
|
|
name = serializers.SerializerMethodField()
|
|
first_name = serializers.SerializerMethodField()
|
|
last_name = serializers.SerializerMethodField()
|
|
|
|
class Meta:
|
|
"""
|
|
meta class
|
|
"""
|
|
model = Junior
|
|
fields = ('id', 'name', 'first_name', 'last_name', 'is_active', 'image')
|
|
|
|
@staticmethod
|
|
def get_name(obj):
|
|
"""
|
|
:param obj: junior object
|
|
:return: full name
|
|
"""
|
|
return f"{obj.auth.first_name} {obj.auth.last_name}" if obj.auth.last_name else obj.auth.first_name
|
|
|
|
@staticmethod
|
|
def get_first_name(obj):
|
|
"""
|
|
:param obj: junior object
|
|
:return: first name
|
|
"""
|
|
return obj.auth.first_name
|
|
|
|
@staticmethod
|
|
def get_last_name(obj):
|
|
"""
|
|
:param obj: junior object
|
|
:return: last name
|
|
"""
|
|
return obj.auth.last_name
|
|
|
|
|
|
class LeaderboardSerializer(serializers.ModelSerializer):
|
|
"""
|
|
leaderboard serializer
|
|
"""
|
|
junior = JuniorLeaderboardSerializer()
|
|
rank = serializers.IntegerField()
|
|
|
|
class Meta:
|
|
"""
|
|
meta class
|
|
"""
|
|
model = JuniorPoints
|
|
fields = ('total_points', 'rank', 'junior')
|
|
|
|
|
|
class UserCSVReportSerializer(serializers.ModelSerializer):
|
|
"""
|
|
user csv/xls report serializer
|
|
"""
|
|
phone_number = serializers.SerializerMethodField()
|
|
user_type = serializers.SerializerMethodField()
|
|
is_active = serializers.SerializerMethodField()
|
|
date_joined = serializers.SerializerMethodField()
|
|
|
|
class Meta:
|
|
"""
|
|
meta class
|
|
"""
|
|
model = USER
|
|
fields = ('first_name', 'last_name', 'email', 'phone_number', 'user_type', 'is_active', 'date_joined')
|
|
|
|
@staticmethod
|
|
def get_phone_number(obj):
|
|
"""
|
|
:param obj: user object
|
|
:return: user phone number
|
|
"""
|
|
if profile := obj.guardian_profile.all().first():
|
|
return f"+{profile.country_code}{profile.phone}" \
|
|
if profile.country_code and profile.phone else profile.phone
|
|
elif profile := obj.junior_profile.all().first():
|
|
return f"+{profile.country_code}{profile.phone}" \
|
|
if profile.country_code and profile.phone else profile.phone
|
|
else:
|
|
return None
|
|
|
|
@staticmethod
|
|
def get_user_type(obj):
|
|
"""
|
|
:param obj: user object
|
|
:return: user type
|
|
"""
|
|
if obj.guardian_profile.all().first():
|
|
return dict(USER_TYPE).get('2').capitalize()
|
|
elif obj.junior_profile.all().first():
|
|
return dict(USER_TYPE).get('1').capitalize()
|
|
else:
|
|
return None
|
|
|
|
@staticmethod
|
|
def get_is_active(obj):
|
|
"""
|
|
:param obj: user object
|
|
:return: user type
|
|
"""
|
|
if profile := obj.guardian_profile.all().first():
|
|
return "Active" if profile.is_active else "Inactive"
|
|
elif profile := obj.junior_profile.all().first():
|
|
return "Active" if profile.is_active else "Inactive"
|
|
|
|
@staticmethod
|
|
def get_date_joined(obj):
|
|
"""
|
|
:param obj: user obj
|
|
:return: formatted date
|
|
"""
|
|
date = obj.date_joined.strftime("%d %b %Y")
|
|
return date
|