mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-07-15 18:07:02 +00:00
184 lines
6.0 KiB
Python
184 lines
6.0 KiB
Python
"""
|
|
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()
|
|
country_code = serializers.SerializerMethodField()
|
|
phone = serializers.SerializerMethodField()
|
|
user_type = serializers.SerializerMethodField()
|
|
is_active = serializers.SerializerMethodField()
|
|
|
|
class Meta:
|
|
"""
|
|
meta class
|
|
"""
|
|
model = USER
|
|
fields = ('id', 'name', 'email', 'country_code', 'phone', '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_country_code(obj):
|
|
"""
|
|
:param obj: user object
|
|
:return: user phone number
|
|
"""
|
|
if profile := obj.guardian_profile.all().first():
|
|
return profile.country_code if profile.country_code else None
|
|
elif profile := obj.junior_profile.all().first():
|
|
return profile.country_code if profile.country_code else None
|
|
else:
|
|
return None
|
|
|
|
@staticmethod
|
|
def get_phone(obj):
|
|
"""
|
|
:param obj: user object
|
|
:return: user phone number
|
|
"""
|
|
if profile := obj.guardian_profile.all().first():
|
|
return profile.phone if profile.phone else None
|
|
elif profile := obj.junior_profile.all().first():
|
|
return profile.phone if profile.phone else None
|
|
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
|