referral code

This commit is contained in:
jain
2023-07-13 17:31:05 +05:30
parent a16fecb1c9
commit 52a0f4b590
5 changed files with 29 additions and 42 deletions

View File

@ -27,7 +27,8 @@ from rest_framework_simplejwt.tokens import RefreshToken
from base.messages import ERROR_CODE, SUCCESS_CODE
from base.constants import NUMBER
from guardian.tasks import generate_otp
from account.utils import send_otp_email, send_support_email, custom_response, custom_error_response
from account.utils import (send_otp_email, send_support_email, custom_response, custom_error_response,
generate_alphanumeric_code)
from rest_framework.permissions import IsAuthenticated
from templated_email import send_templated_mail
import google.oauth2.credentials
@ -89,19 +90,15 @@ class GoogleLoginMixin:
if str(user_type) == '1':
junior_query = Junior.objects.create(auth=user_obj, is_verified=True, is_active=True,
image=profile_picture, signup_method='2',
junior_code=''.join(
[str(random.randrange(9)) for _ in range(6)]),
referral_code=''.join(
[str(random.randrange(9)) for _ in range(6)])
junior_code=generate_alphanumeric_code(6),
referral_code=generate_alphanumeric_code(6)
)
serializer = JuniorSerializer(junior_query)
if str(user_type) == '2':
guardian_query = Guardian.objects.create(user=user_obj, is_verified=True, is_active=True,
image=profile_picture,signup_method='2',
guardian_code=''.join(
[str(random.randrange(9)) for _ in range(6)]),
referral_code=''.join(
[str(random.randrange(9)) for _ in range(6)])
guardian_code=generate_alphanumeric_code(6),
referral_code=generate_alphanumeric_code(6)
)
serializer = GuardianSerializer(guardian_query)
# Return a JSON response with the user's email and name
@ -143,18 +140,15 @@ class SigninWithApple(views.APIView):
user = User.objects.create(**user_data)
if str(user_type) == '1':
junior_query = Junior.objects.create(auth=user, is_verified=True, is_active=True,
signup_method='3',junior_code=''.join(
[str(random.randrange(9)) for _ in range(6)]),
referral_code=''.join(
[str(random.randrange(9)) for _ in range(6)]))
signup_method='3',
junior_code=generate_alphanumeric_code(6),
referral_code=generate_alphanumeric_code(6))
serializer = JuniorSerializer(junior_query)
if str(user_type) == '2':
guardian_query = Guardian.objects.create(user=user, is_verified=True, is_active=True,
signup_method='3',
guardian_code=''.join(
[str(random.randrange(9)) for _ in range(6)]),
referral_code=''.join(
[str(random.randrange(9)) for _ in range(6)]))
guardian_code=generate_alphanumeric_code(6),
referral_code=generate_alphanumeric_code(6))
serializer = GuardianSerializer(guardian_query)
return custom_response(SUCCESS_CODE['3003'], serializer.data,
response_status=status.HTTP_200_OK)
@ -169,7 +163,7 @@ class UpdateProfileImage(views.APIView):
def put(self, request, format=None):
try:
image = request.data['image']
if image.size == NUMBER['zero']:
if image and image.size == NUMBER['zero']:
return custom_error_response(ERROR_CODE['2035'], response_status=status.HTTP_400_BAD_REQUEST)
filename = f"images/{image.name}"
image_url = upload_image_to_alibaba(image, filename)

View File

@ -9,6 +9,7 @@ from django.contrib.auth.models import User
"""Import Django app"""
from .models import Guardian, JuniorTask
from account.models import UserProfile, UserEmailOtp, UserNotification
from account.utils import generate_alphanumeric_code
from account.serializers import JuniorSerializer
from junior.serializers import JuniorDetailSerializer
from base.messages import ERROR_CODE, SUCCESS_CODE
@ -38,11 +39,11 @@ class UserSerializer(serializers.ModelSerializer):
user = User.objects.create_user(username=email, email=email, password=password)
UserNotification.objects.create(user=user)
if user_type == '1':
Junior.objects.create(auth=user, junior_code=''.join([str(random.randrange(9)) for _ in range(6)]),
referral_code=''.join([str(random.randrange(9)) for _ in range(6)]))
Junior.objects.create(auth=user, junior_code=generate_alphanumeric_code(6),
referral_code=generate_alphanumeric_code(6))
if user_type == '2':
Guardian.objects.create(user=user, guardian_code=''.join([str(random.randrange(9)) for _ in range(6)]),
referral_code=''.join([str(random.randrange(9)) for _ in range(6)]))
Guardian.objects.create(user=user, guardian_code=generate_alphanumeric_code(6),
referral_code=generate_alphanumeric_code(6))
return user
except Exception as e:
"""Error handling"""
@ -99,11 +100,7 @@ class CreateGuardianSerializer(serializers.ModelSerializer):
user.last_name = self.context.get('last_name')
user.save()
"""Create guardian data"""
guardian, created = Guardian.objects.get_or_create(user=self.context['user'])
if created:
"""Create referral code and guardian code"""
guardian.referral_code = ''.join([str(random.randrange(9)) for _ in range(4)])
guardian.guardian_code = ''.join([str(random.randrange(9)) for _ in range(4)])
guardian = Guardian.objects.filter(user=self.context['user']).last()
if guardian:
"""update details according to the data get from request"""
guardian.gender = validated_data.get('gender',guardian.gender)

View File

@ -56,9 +56,9 @@ class UpdateGuardianProfile(viewsets.ViewSet):
data = request.data
image = request.data.get('image')
image_url = ''
if image.size == NUMBER['zero']:
return custom_error_response(ERROR_CODE['2035'], response_status=status.HTTP_400_BAD_REQUEST)
if image:
if image and image.size == NUMBER['zero']:
return custom_error_response(ERROR_CODE['2035'], response_status=status.HTTP_400_BAD_REQUEST)
filename = f"images/{image.name}"
image_url = upload_image_to_alibaba(image, filename)
data = {"image":image_url}
@ -118,7 +118,7 @@ class CreateTaskAPIView(viewsets.ModelViewSet):
image_data = image
else:
filename = f"images/{image}"
if image.size == NUMBER['zero']:
if image and image.size == NUMBER['zero']:
return custom_error_response(ERROR_CODE['2035'], response_status=status.HTTP_400_BAD_REQUEST)
image_url = upload_image_to_alibaba(image, filename)
image_data = image_url

View File

@ -8,7 +8,7 @@ from django.utils import timezone
from rest_framework_simplejwt.tokens import RefreshToken
"""Import django app"""
from account.utils import send_otp_email
from account.utils import send_otp_email, generate_alphanumeric_code
from junior.models import Junior, JuniorPoints
from guardian.tasks import generate_otp
from base.messages import ERROR_CODE, SUCCESS_CODE
@ -76,11 +76,7 @@ class CreateJuniorSerializer(serializers.ModelSerializer):
user.last_name = self.context.get('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)])
junior = Junior.objects.filter(auth=self.context['user']).last()
if junior:
"""update details according to the data get from request"""
junior.gender = validated_data.get('gender',junior.gender)
@ -134,8 +130,8 @@ class JuniorDetailSerializer(serializers.ModelSerializer):
"""Meta info"""
model = Junior
fields = ['id', 'email', 'first_name', 'last_name', 'country_code', 'phone', 'gender', 'dob',
'guardian_code', 'is_invited', 'referral_code','is_active', 'is_complete_profile', 'created_at', 'image',
'updated_at']
'guardian_code', 'is_invited', 'referral_code','is_active', 'is_complete_profile', 'created_at',
'image', 'updated_at']
class JuniorDetailListSerializer(serializers.ModelSerializer):
"""junior serializer"""
@ -279,8 +275,8 @@ class AddJuniorSerializer(serializers.ModelSerializer):
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)]),
junior_code=generate_alphanumeric_code(6),
referral_code=generate_alphanumeric_code(6),
referral_code_used=guardian_data.referral_code)
"""Generate otp"""
otp_value = generate_otp()

View File

@ -25,9 +25,9 @@ class UpdateJuniorProfile(viewsets.ViewSet):
request_data = request.data
image = request.data.get('image')
image_url = ''
if image.size == NUMBER['zero']:
return custom_error_response(ERROR_CODE['2035'], response_status=status.HTTP_400_BAD_REQUEST)
if image:
if image.size == NUMBER['zero']:
return custom_error_response(ERROR_CODE['2035'], response_status=status.HTTP_400_BAD_REQUEST)
filename = f"images/{image.name}"
image_url = upload_image_to_alibaba(image, filename)
request_data = {"image": image_url}