""" web_admin analytics serializer file """ from rest_framework import serializers from django.contrib.auth import get_user_model from base.constants import USER_TYPE from junior.models import JuniorPoints, Junior from web_admin.serializers.user_management_serializer import JuniorSerializer, GuardianSerializer 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() 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') elif obj.junior_profile.all().first(): return dict(USER_TYPE).get('1') 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"