""" web_admin user_management serializers file """ # django imports from rest_framework import serializers from django.contrib.auth import get_user_model from base.constants import (ARTICLE_SURVEY_POINTS, MAX_ARTICLE_CARD, MIN_ARTICLE_SURVEY, MAX_ARTICLE_SURVEY, NUMBER, USER_TYPE, ARTICLE_CARD_IMAGE_FOLDER) # local imports from base.messages import ERROR_CODE from guardian.models import Guardian from guardian.utils import upload_image_to_alibaba from junior.models import Junior from web_admin.models import Article, ArticleCard, SurveyOption, ArticleSurvey, DefaultArticleCardImage from web_admin.utils import pop_id, get_image_url USER = get_user_model() class UserManagementListSerializer(serializers.ModelSerializer): """ user management serializer """ name = serializers.SerializerMethodField() phone_number = serializers.SerializerMethodField() user_type = serializers.SerializerMethodField() is_active = serializers.SerializerMethodField() class Meta: """ meta class """ model = USER fields = ('id', 'name', 'email', 'phone_number', 'user_type', 'is_active') @staticmethod def get_name(obj): """ :param obj: user object :return: full name """ return f"{obj.first_name} {obj.last_name}" if obj.last_name else obj.first_name @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 profile.is_active elif profile := obj.junior_profile.all().first(): return profile.is_active else: return obj.is_active class GuardianSerializer(serializers.ModelSerializer): name = serializers.SerializerMethodField() class Meta: model = Guardian fields = ('id', 'name', 'dob', 'gender', 'country_code', 'phone', 'is_active', 'country_name', 'image') def update(self, instance, validated_data): instance.user.email = self.context.get('email', instance.user.email) instance.user.save() instance.country_code = validated_data.get('country_code', instance.country_code) instance.phone = validated_data.get('phone', instance.phone) instance.save() return instance @staticmethod def get_name(obj): """ :param obj: user object :return: full name """ return f"{obj.user.first_name} {obj.user.last_name}" if obj.user.last_name else obj.user.first_name class JuniorSerializer(serializers.ModelSerializer): name = serializers.SerializerMethodField() class Meta: model = Junior fields = ('id', 'name', 'dob', 'gender', 'country_code', 'phone', 'is_active', 'country_name', 'image') def update(self, instance, validated_data): instance.auth.email = self.context.get('email', instance.auth.email) instance.auth.save() instance.country_code = validated_data.get('country_code', instance.country_code) instance.phone = validated_data.get('phone', instance.phone) instance.save() return instance @staticmethod def get_name(obj): """ :param obj: user object :return: full name """ return f"{obj.auth.first_name} {obj.auth.last_name}" if obj.auth.last_name else obj.auth.first_name class UserManagementDetailSerializer(serializers.ModelSerializer): user_type = serializers.SerializerMethodField() guardian_profile = GuardianSerializer(many=True) junior_profile = JuniorSerializer(many=True) associated_users = serializers.SerializerMethodField() class Meta: """ meta class """ model = USER fields = ('id', 'user_type', 'email', 'guardian_profile', 'junior_profile', 'associated_users') @staticmethod def get_user_type(obj): """ :param obj: user object :return: user type """ profile = obj.guardian_profile.all().first() 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_associated_users(obj): if profile := obj.guardian_profile.all().first(): junior = Junior.objects.filter(guardian_code__contains=[profile.guardian_code], is_verified=True) serializer = JuniorSerializer(junior, many=True) return serializer.data elif profile := obj.junior_profile.all().first(): guardian = Guardian.objects.filter(guardian_code__in=profile.guardian_code, is_verified=True) serializer = GuardianSerializer(guardian, many=True) return serializer.data