Jira-13 sonar fixes

This commit is contained in:
jain
2023-06-27 11:44:00 +05:30
parent 9bd31f9e86
commit 44b25dde3e
19 changed files with 188 additions and 89 deletions

View File

@ -1,28 +1,29 @@
"""Serializer file for junior"""
"""Import Django 3rd party app"""
from rest_framework import serializers
from django.contrib.auth.models import User
from .models import Guardian
from django.db import transaction
import random
from account.models import UserProfile
"""Import django app"""
from junior.models import Junior
from base.constants import GENDERS, GUARDIAN, JUNIOR, SUPERUSER
from rest_framework_simplejwt.tokens import RefreshToken
from base.messages import ERROR_CODE, SUCCESS_CODE
from django.contrib.postgres.fields import ArrayField
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')
@ -33,41 +34,59 @@ class CreateJuniorSerializer(serializers.ModelSerializer):
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:
junior.phone = validated_data.get('phone', junior.phone)
"""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)
junior.is_complete_profile = validated_data.get('is_complete_profile', junior.is_complete_profile)
"""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