jira-17 add junior and send invite mail

This commit is contained in:
jain
2023-07-12 16:24:30 +05:30
parent 33101811ff
commit a041028840
13 changed files with 235 additions and 8 deletions

View File

@ -4,12 +4,19 @@ from rest_framework import serializers
from django.contrib.auth.models import User
from django.db import transaction
import random
from django.utils import timezone
from rest_framework_simplejwt.tokens import RefreshToken
"""Import django app"""
from account.utils import send_otp_email
from junior.models import Junior, JuniorPoints
from guardian.utils import upload_image_to_alibaba
from guardian.tasks import generate_otp
from base.messages import ERROR_CODE, SUCCESS_CODE
from guardian.models import Guardian, JuniorTask
from base.constants import PENDING, IN_PROGRESS, REJECTED, REQUESTED, COMPLETED
from guardian.models import Guardian, JuniorTask
from account.models import UserEmailOtp
from junior.utils import junior_notification_email, junior_approval_mail
class ListCharField(serializers.ListField):
"""Serializer for Array field"""
@ -238,3 +245,54 @@ class JuniorProfileSerializer(serializers.ModelSerializer):
fields = ['id', 'email', 'first_name', 'last_name', 'country_name', 'country_code', 'phone', 'gender', 'dob',
'guardian_code', 'referral_code','is_active', 'is_complete_profile', 'created_at', 'image',
'updated_at', 'notification_count', 'total_count', 'complete_field_count', 'signup_method']
class AddJuniorSerializer(serializers.ModelSerializer):
"""Add junior serializer"""
auth_token = serializers.SerializerMethodField('get_auth_token')
def get_auth_token(self, obj):
"""auth token"""
refresh = RefreshToken.for_user(obj)
access_token = str(refresh.access_token)
return access_token
class Meta(object):
"""Meta info"""
model = Junior
fields = ['gender','dob', 'relationship', 'auth_token']
def create(self, validated_data):
""" create junior"""
with transaction.atomic():
email = self.context['email']
guardian = self.context['user']
full_name = self.context['first_name'] + ' ' + self.context['last_name']
guardian_data = Guardian.objects.filter(user__username=guardian).last()
user_data = User.objects.create(username=email, email=email,
first_name=self.context['first_name'],
last_name=self.context['last_name'])
password = User.objects.make_random_password()
user_data.set_password(password)
user_data.save()
junior_data = Junior.objects.create(auth=user_data, gender=validated_data.get('gender'),
dob=validated_data.get('dob'), is_invited=True,
relationship=validated_data.get('relationship'),
junior_code=''.join([str(random.randrange(9)) for _ in range(4)]),
referral_code=''.join([str(random.randrange(9)) for _ in range(4)]),
referral_code_used=guardian_data.referral_code)
"""Generate otp"""
otp_value = generate_otp()
expiry_time = timezone.now() + timezone.timedelta(days=1)
UserEmailOtp.objects.create(email=email, otp=otp_value,
user_type='1', expired_at=expiry_time)
"""Send email to the register user"""
send_otp_email(email, otp_value)
"""Notification email"""
junior_notification_email(email, full_name, email, password)
junior_approval_mail(guardian, full_name)
return junior_data