Files
zod-backend/junior/serializers.py
2023-06-27 17:27:19 +05:30

93 lines
3.9 KiB
Python

"""Serializer file for junior"""
"""Import Django 3rd party app"""
from rest_framework import serializers
from django.contrib.auth.models import User
from django.db import transaction
import random
"""Import django app"""
from junior.models import Junior
class ListCharField(serializers.ListField):
"""Serializer for Array field"""
child = serializers.CharField()
def to_representation(self, data):
"""to represent the data"""
return data
def to_internal_value(self, data):
"""internal value"""
if isinstance(data, list):
return data
raise serializers.ValidationError({"details":"Invalid input. Expected a list of strings."})
class CreateJuniorSerializer(serializers.ModelSerializer):
"""Create junior serializer"""
first_name = serializers.SerializerMethodField('get_first_name')
last_name = serializers.SerializerMethodField('get_last_name')
email = serializers.SerializerMethodField('get_email')
phone = serializers.CharField(max_length=20, required=False)
country_code = serializers.IntegerField(required=False)
dob = serializers.DateField(required=False)
referral_code = serializers.CharField(max_length=100, required=False)
guardian_code = ListCharField(required=False)
class Meta(object):
"""Meta info"""
model = Junior
fields = ['first_name', 'last_name', 'email', 'phone', 'gender', 'country_code', 'dob', 'referral_code',
'passcode', 'is_complete_profile', 'guardian_code']
def get_first_name(self,obj):
"""first name of junior"""
return obj.auth.first_name
def get_last_name(self,obj):
"""last name of junior"""
return obj.auth.last_name
def get_email(self,obj):
"""email of junior"""
return obj.auth.email
def create(self, validated_data):
"""Create junior profile"""
user = User.objects.filter(username=self.context['user']).last()
if user:
"""Save first and last name of junior"""
user.first_name = self.context.get('first_name', user.first_name)
user.last_name = self.context.get('last_name', user.last_name)
user.save()
"""Create junior data"""
junior, created = Junior.objects.get_or_create(auth=self.context['user'])
if created:
"""Create referral code and junior code"""
junior.referral_code = ''.join([str(random.randrange(9)) for _ in range(4)])
junior.junior_code = ''.join([str(random.randrange(9)) for _ in range(4)])
if junior:
"""update details according to the data get from request"""
junior.gender = validated_data.get('gender',junior.gender)
"""Update guardian code"""
junior.guardian_code = validated_data.get('guardian_code', junior.guardian_code)
junior.dob = validated_data.get('dob',junior.dob)
junior.passcode = validated_data.get('passcode', junior.passcode)
"""Update country code and phone number"""
junior.phone = validated_data.get('phone', junior.phone)
junior.country_code = validated_data.get('country_code', junior.country_code)
junior.referral_code_used = validated_data.get('referral_code_used', junior.referral_code_used)
"""Complete profile of the junior if below all data are filled"""
complete_profile_field = all([junior.phone, junior.gender, junior.family_name,
junior.dob, junior.country_code, user.first_name, user.last_name])
junior.is_complete_profile = False
if complete_profile_field:
junior.is_complete_profile = True
junior.save()
return junior
def save(self, **kwargs):
"""Save the data into junior table"""
with transaction.atomic():
instance = super().save(**kwargs)
return instance