Files
zod-backend/web_admin/serializers/user_management_serializer.py

287 lines
9.2 KiB
Python

"""
web_admin user_management serializers file
"""
# django imports
from rest_framework import serializers
from django.contrib.auth import get_user_model
from account.utils import get_user_full_name
from base.constants import USER_TYPE, GUARDIAN, JUNIOR
# local imports
from base.messages import ERROR_CODE, SUCCESS_CODE
from guardian.models import Guardian
from junior.models import Junior
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 get_user_full_name(obj)
@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
@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
@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')
@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
class GuardianSerializer(serializers.ModelSerializer):
"""
guardian serializer
"""
name = serializers.SerializerMethodField()
first_name = serializers.SerializerMethodField()
last_name = serializers.SerializerMethodField()
username = serializers.SerializerMethodField()
email = serializers.EmailField(required=False)
class Meta:
"""
meta class
"""
model = Guardian
fields = ('id', 'name', 'first_name', 'last_name', 'username', 'dob', 'gender', 'country_code', 'phone',
'is_active', 'country_name', 'image', 'email', 'is_deleted')
def validate(self, attrs):
"""
to validate request data
:return: validated attrs
"""
email = attrs.get('email')
phone = attrs.get('phone')
if USER.objects.filter(email=email).exclude(id=self.context.get('user_id')).exists():
raise serializers.ValidationError({'details': ERROR_CODE['2003']})
if Guardian.objects.filter(phone=phone).exclude(user__id=self.context.get('user_id')).exists():
raise serializers.ValidationError({'details': ERROR_CODE['2012']})
return attrs
def update(self, instance, validated_data):
"""
to update user and its related profile
:param instance: user's guardian object
:param validated_data:
:return: guardian object
"""
instance.user.email = self.validated_data.get('email', instance.user.email)
instance.user.username = self.validated_data.get('email', instance.user.username)
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: guardian object
:return: full name
"""
return get_user_full_name(obj.user)
@staticmethod
def get_first_name(obj):
"""
:param obj: guardian object
:return: first name
"""
return obj.user.first_name
@staticmethod
def get_last_name(obj):
"""
:param obj: guardian object
:return: last name
"""
return obj.user.last_name
@staticmethod
def get_username(obj):
"""
:param obj: guardian object
:return: email
"""
return obj.user.username
class JuniorSerializer(serializers.ModelSerializer):
"""
junior serializer
"""
name = serializers.SerializerMethodField()
first_name = serializers.SerializerMethodField()
last_name = serializers.SerializerMethodField()
username = serializers.SerializerMethodField()
email = serializers.EmailField(required=False)
class Meta:
"""
meta class
"""
model = Junior
fields = ('id', 'name', 'first_name', 'last_name', 'username', 'dob', 'gender', 'country_code', 'phone',
'is_active', 'country_name', 'image', 'email', 'is_deleted')
def validate(self, attrs):
"""
to validate request data
:return: validated attrs
"""
email = attrs.get('email')
phone = attrs.get('phone')
if email and USER.objects.filter(email=email).exclude(id=self.context.get('user_id')).exists():
raise serializers.ValidationError({'details': ERROR_CODE['2003']})
if phone and Junior.objects.filter(phone=phone).exclude(auth__id=self.context.get('user_id')).exists():
raise serializers.ValidationError({'details': ERROR_CODE['2012']})
return attrs
def update(self, instance, validated_data):
"""
:param instance: user's junior object
:param validated_data: validated data
:return: instance
"""
instance.auth.email = self.validated_data.get('email', instance.auth.email)
instance.auth.username = self.validated_data.get('email', instance.auth.username)
instance.auth.save(update_fields=['email', 'username'])
instance.country_code = validated_data.get('country_code', instance.country_code)
instance.phone = validated_data.get('phone', instance.phone)
instance.save(update_fields=['country_code', 'phone'])
return instance
@staticmethod
def get_name(obj):
"""
:param obj: junior object
:return: full name
"""
return get_user_full_name(obj.auth)
@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
@staticmethod
def get_username(obj):
"""
:param obj: junior object
:return: email
"""
return obj.auth.username
class UserManagementDetailSerializer(serializers.ModelSerializer):
"""
user management details serializer
"""
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')
def get_user_type(self, obj):
"""
:param obj: user object
:return: user type
"""
return GUARDIAN if self.context['user_type'] == GUARDIAN else JUNIOR
def get_associated_users(self, obj):
"""
:param obj: user object
:return: associated user
"""
if self.context['user_type'] == GUARDIAN:
profile = obj.guardian_profile.all().only('user_id', 'guardian_code').first()
if profile.guardian_code:
junior = Junior.objects.filter(guardian_code__contains=[profile.guardian_code],
is_verified=True).select_related('auth')
serializer = JuniorSerializer(junior, many=True)
return serializer.data
elif self.context['user_type'] == JUNIOR:
profile = obj.junior_profile.all().only('auth_id', 'guardian_code').first()
if profile.guardian_code:
guardian = Guardian.objects.filter(guardian_code__in=profile.guardian_code,
is_verified=True).select_related('user')
serializer = GuardianSerializer(guardian, many=True)
return serializer.data