mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-07-15 10:05:21 +00:00
edit user validations, active-inactive api
This commit is contained in:
@ -5,15 +5,11 @@ web_admin user_management serializers file
|
||||
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)
|
||||
from base.constants import USER_TYPE
|
||||
# local imports
|
||||
from base.messages import ERROR_CODE
|
||||
from base.messages import ERROR_CODE, SUCCESS_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()
|
||||
|
||||
@ -97,14 +93,41 @@ class UserManagementListSerializer(serializers.ModelSerializer):
|
||||
|
||||
|
||||
class GuardianSerializer(serializers.ModelSerializer):
|
||||
"""
|
||||
guardian serializer
|
||||
"""
|
||||
name = serializers.SerializerMethodField()
|
||||
email = serializers.CharField(required=False)
|
||||
|
||||
class Meta:
|
||||
"""
|
||||
meta class
|
||||
"""
|
||||
model = Guardian
|
||||
fields = ('id', 'name', 'dob', 'gender', 'country_code', 'phone', 'is_active', 'country_name', 'image')
|
||||
fields = ('id', 'name', '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):
|
||||
instance.user.email = self.context.get('email', instance.user.email)
|
||||
"""
|
||||
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)
|
||||
@ -121,14 +144,40 @@ class GuardianSerializer(serializers.ModelSerializer):
|
||||
|
||||
|
||||
class JuniorSerializer(serializers.ModelSerializer):
|
||||
"""
|
||||
junior serializer
|
||||
"""
|
||||
name = serializers.SerializerMethodField()
|
||||
email = serializers.CharField(required=False)
|
||||
|
||||
class Meta:
|
||||
"""
|
||||
meta class
|
||||
"""
|
||||
model = Junior
|
||||
fields = ('id', 'name', 'dob', 'gender', 'country_code', 'phone', 'is_active', 'country_name', 'image')
|
||||
fields = ('id', 'name', '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):
|
||||
instance.auth.email = self.context.get('email', instance.auth.email)
|
||||
"""
|
||||
: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)
|
||||
@ -145,6 +194,9 @@ class JuniorSerializer(serializers.ModelSerializer):
|
||||
|
||||
|
||||
class UserManagementDetailSerializer(serializers.ModelSerializer):
|
||||
"""
|
||||
user management details serializer
|
||||
"""
|
||||
user_type = serializers.SerializerMethodField()
|
||||
guardian_profile = GuardianSerializer(many=True)
|
||||
junior_profile = JuniorSerializer(many=True)
|
||||
@ -163,7 +215,6 @@ class UserManagementDetailSerializer(serializers.ModelSerializer):
|
||||
: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():
|
||||
@ -173,6 +224,10 @@ class UserManagementDetailSerializer(serializers.ModelSerializer):
|
||||
|
||||
@staticmethod
|
||||
def get_associated_users(obj):
|
||||
"""
|
||||
:param obj: user object
|
||||
:return: associated user
|
||||
"""
|
||||
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)
|
||||
|
Reference in New Issue
Block a user