mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-07-15 10:05:21 +00:00
299 lines
9.2 KiB
Python
299 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 base.constants import USER_TYPE
|
|
# 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 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):
|
|
"""
|
|
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')
|
|
|
|
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 f"{obj.user.first_name} {obj.user.last_name}" if obj.user.last_name else obj.user.first_name
|
|
|
|
@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')
|
|
|
|
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()
|
|
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: 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
|
|
|
|
@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')
|
|
|
|
@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_associated_users(obj):
|
|
"""
|
|
:param obj: user object
|
|
:return: associated user
|
|
"""
|
|
if profile := obj.guardian_profile.all().first():
|
|
if profile.guardian_code:
|
|
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():
|
|
if profile.guardian_code:
|
|
guardian = Guardian.objects.filter(guardian_code__in=profile.guardian_code, is_verified=True)
|
|
serializer = GuardianSerializer(guardian, many=True)
|
|
return serializer.data
|
|
else:
|
|
return None
|