mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2026-03-11 02:31:45 +00:00
Compare commits
24 Commits
GoogleLogi
...
login_code
| Author | SHA1 | Date | |
|---|---|---|---|
| 22f1d01b94 | |||
| a27c486c43 | |||
| 28cbdf9cfa | |||
| 014b5fe183 | |||
| d2498f82ad | |||
| 79ac140ddd | |||
| 4a34dab570 | |||
| 245162b913 | |||
| 7376fc555b | |||
| ed2d7895ab | |||
| c57968b7c0 | |||
| e42d8af65b | |||
| d254c5e5fa | |||
| 118d73776b | |||
| c2713b8214 | |||
| 4dd95e74fb | |||
| 70c136dde4 | |||
| 88a9e925ed | |||
| 3d3ccfb146 | |||
| be6725c66d | |||
| f3c41043e0 | |||
| ea2bd635ca | |||
| eb4cde397f | |||
| ef1f7ebeac |
18
account/migrations/0002_useremailotp_user_type.py
Normal file
18
account/migrations/0002_useremailotp_user_type.py
Normal file
@ -0,0 +1,18 @@
|
||||
# Generated by Django 4.2.2 on 2023-06-29 12:10
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('account', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='useremailotp',
|
||||
name='user_type',
|
||||
field=models.CharField(blank=True, choices=[('1', 'junior'), ('2', 'guardian'), ('3', 'superuser')], default=None, max_length=15, null=True),
|
||||
),
|
||||
]
|
||||
@ -59,6 +59,7 @@ class UserEmailOtp(models.Model):
|
||||
"""otp details"""
|
||||
otp = models.CharField(max_length=10)
|
||||
is_verified = models.BooleanField(default=False)
|
||||
user_type = models.CharField(max_length=15, choices=USER_TYPE, null=True, blank=True, default=None)
|
||||
|
||||
# OTP validity
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
|
||||
@ -13,21 +13,15 @@ from rest_framework.decorators import action
|
||||
from django.contrib.auth import authenticate, login
|
||||
from rest_framework_simplejwt.tokens import RefreshToken
|
||||
from guardian.utils import upload_image_to_alibaba
|
||||
from .utils import get_token
|
||||
|
||||
class GoogleSignInSerializer(serializers.Serializer):
|
||||
"""Google login Serializer"""
|
||||
email = serializers.EmailField()
|
||||
|
||||
def create(self, validated_data):
|
||||
"""Create or update user model"""
|
||||
with transaction.atomic():
|
||||
if User.objects.filter(email__iexact=self.validated_data['email']).exists():
|
||||
return User.objects.get(email__iexact=self.validated_data['email'])
|
||||
class GoogleLoginSerializer(serializers.Serializer):
|
||||
access_token = serializers.CharField(max_length=5000, required=True)
|
||||
|
||||
if not User.objects.filter(email__iexact=self.validated_data['email']).exists():
|
||||
instance = User.objects.create(username=self.validated_data['email'],
|
||||
email=self.validated_data['email'])
|
||||
return instance
|
||||
class Meta:
|
||||
"""meta class"""
|
||||
fields = ('access_token',)
|
||||
|
||||
class UpdateGuardianImageSerializer(serializers.ModelSerializer):
|
||||
"""Reset Password after verification"""
|
||||
@ -72,6 +66,8 @@ class ResetPasswordSerializer(serializers.Serializer):
|
||||
if user_opt_details:
|
||||
user_details = User.objects.filter(email=user_opt_details.email).last()
|
||||
if user_details:
|
||||
if user_details.check_password(password):
|
||||
raise serializers.ValidationError({"details":ERROR_CODE['2001'],"code":"400", "status":"failed"})
|
||||
user_details.set_password(password)
|
||||
user_details.save()
|
||||
return {'password':password}
|
||||
@ -130,11 +126,13 @@ class GuardianSerializer(serializers.ModelSerializer):
|
||||
first_name = serializers.SerializerMethodField('get_first_name')
|
||||
last_name = serializers.SerializerMethodField('get_last_name')
|
||||
auth_token = serializers.SerializerMethodField('get_auth_token')
|
||||
refresh_token = serializers.SerializerMethodField('get_refresh_token')
|
||||
|
||||
def get_auth_token(self, obj):
|
||||
refresh = RefreshToken.for_user(obj.user)
|
||||
access_token = str(refresh.access_token)
|
||||
return access_token
|
||||
return get_token()['access']
|
||||
def get_refresh_token(self, obj):
|
||||
return get_token()['refresh']
|
||||
|
||||
|
||||
def get_user_type(self, obj):
|
||||
"""user type"""
|
||||
@ -155,8 +153,8 @@ class GuardianSerializer(serializers.ModelSerializer):
|
||||
class Meta(object):
|
||||
"""Meta info"""
|
||||
model = Guardian
|
||||
fields = ['auth_token', 'email', 'first_name', 'last_name', 'country_code', 'phone', 'family_name', 'gender', 'dob',
|
||||
'referral_code', 'is_active', 'is_complete_profile', 'passcode',
|
||||
fields = ['auth_token', 'refresh_token', 'email', 'first_name', 'last_name', 'country_code', 'phone', 'family_name', 'gender', 'dob',
|
||||
'referral_code', 'is_active', 'is_complete_profile', 'passcode', 'image',
|
||||
'created_at', 'updated_at', 'user_type']
|
||||
|
||||
|
||||
@ -167,11 +165,12 @@ class JuniorSerializer(serializers.ModelSerializer):
|
||||
first_name = serializers.SerializerMethodField('get_first_name')
|
||||
last_name = serializers.SerializerMethodField('get_last_name')
|
||||
auth_token = serializers.SerializerMethodField('get_auth_token')
|
||||
refresh_token = serializers.SerializerMethodField('get_refresh_token')
|
||||
|
||||
def get_auth_token(self, obj):
|
||||
refresh = RefreshToken.for_user(obj.auth)
|
||||
access_token = str(refresh.access_token)
|
||||
return access_token
|
||||
return get_token()['access']
|
||||
def get_refresh_token(self, obj):
|
||||
return get_token()['refresh']
|
||||
|
||||
def get_user_type(self, obj):
|
||||
return JUNIOR
|
||||
@ -188,8 +187,8 @@ class JuniorSerializer(serializers.ModelSerializer):
|
||||
class Meta(object):
|
||||
"""Meta info"""
|
||||
model = Junior
|
||||
fields = ['auth_token', 'email', 'first_name', 'last_name', 'country_code', 'phone', 'gender', 'dob',
|
||||
'guardian_code', 'referral_code','is_active', 'is_complete_profile', 'created_at',
|
||||
fields = ['auth_token', 'refresh_token', 'email', 'first_name', 'last_name', 'country_code', 'phone', 'gender', 'dob',
|
||||
'guardian_code', 'referral_code','is_active', 'is_complete_profile', 'created_at', 'image',
|
||||
'updated_at', 'user_type']
|
||||
|
||||
class EmailVerificationSerializer(serializers.ModelSerializer):
|
||||
|
||||
@ -5,14 +5,15 @@ from rest_framework.decorators import api_view
|
||||
"""Third party import"""
|
||||
from rest_framework import routers
|
||||
from .views import (UserLogin, SendPhoneOtp, UserPhoneVerification, UserEmailVerification, ReSendEmailOtp,
|
||||
ForgotPasswordAPIView, ResetPasswordAPIView, ChangePasswordAPIView, UpdateProfileImage)
|
||||
ForgotPasswordAPIView, ResetPasswordAPIView, ChangePasswordAPIView, UpdateProfileImage,
|
||||
GoogleLoginViewSet, SigninWithApple)
|
||||
"""Router"""
|
||||
router = routers.SimpleRouter()
|
||||
|
||||
"""API End points with router"""
|
||||
router.register('user', UserLogin, basename='user')
|
||||
router.register('admin', UserLogin, basename='admin')
|
||||
# router.register('google-login', GoogleLoginAPIViewset, basename='admin')
|
||||
router.register('google-login', GoogleLoginViewSet, basename='admin')
|
||||
router.register('send-phone-otp', SendPhoneOtp, basename='send-phone-otp')
|
||||
router.register('user-phone-verification', UserPhoneVerification, basename='user-phone-verification')
|
||||
router.register('user-email-verification', UserEmailVerification, basename='user-email-verification')
|
||||
@ -22,5 +23,6 @@ urlpatterns = [
|
||||
path('api/v1/forgot-password/', ForgotPasswordAPIView.as_view()),
|
||||
path('api/v1/reset-password/', ResetPasswordAPIView.as_view()),
|
||||
path('api/v1/change-password/', ChangePasswordAPIView.as_view()),
|
||||
path('api/v1/update-profile-image/', UpdateProfileImage.as_view())
|
||||
path('api/v1/update-profile-image/', UpdateProfileImage.as_view()),
|
||||
path('api/v1/apple-login/', SigninWithApple.as_view(), name='signup_with_apple'),
|
||||
]
|
||||
|
||||
@ -3,8 +3,13 @@
|
||||
from django.conf import settings
|
||||
from rest_framework import viewsets, status
|
||||
from rest_framework.response import Response
|
||||
|
||||
from templated_email import send_templated_mail
|
||||
import jwt
|
||||
from datetime import datetime
|
||||
from calendar import timegm
|
||||
from uuid import uuid4
|
||||
import secrets
|
||||
|
||||
def send_otp_email(recipient_email, otp):
|
||||
from_email = settings.EMAIL_FROM_ADDRESS
|
||||
recipient_list = [recipient_email]
|
||||
@ -36,3 +41,53 @@ def custom_error_response(detail, response_status):
|
||||
if not detail:
|
||||
detail = {}
|
||||
return Response({"error": detail, "status": "failed", "code": response_status})
|
||||
|
||||
|
||||
def get_user_data(attrs):
|
||||
"""
|
||||
used to decode token
|
||||
"""
|
||||
user_data = jwt.decode(jwt=attrs['token'], options={'verify_signature': False},
|
||||
algorithms=['RS256'])
|
||||
return user_data
|
||||
|
||||
|
||||
def generate_jwt_token(token_type: str, now_time: int, data: dict = dict):
|
||||
"""
|
||||
used to generate jwt token
|
||||
"""
|
||||
if type(data) == type:
|
||||
data = {}
|
||||
data.update({
|
||||
'token_type': token_type,
|
||||
'iss': 'your_site_url',
|
||||
'iat': timegm(datetime.utcnow().utctimetuple()),
|
||||
'jti': uuid4().hex
|
||||
})
|
||||
TOKEN_TYPE = ["access", "refresh"]
|
||||
if token_type == TOKEN_TYPE[1]:
|
||||
exp = now_time + settings.SIMPLE_JWT['REFRESH_TOKEN_LIFETIME']
|
||||
else:
|
||||
exp = now_time + settings.SIMPLE_JWT['ACCESS_TOKEN_LIFETIME']
|
||||
|
||||
data.update({
|
||||
"exp": timegm(exp.utctimetuple())
|
||||
})
|
||||
|
||||
|
||||
signing_key = secrets.token_hex(32)
|
||||
|
||||
return jwt.encode(payload=data, key=signing_key,
|
||||
algorithm='HS256')
|
||||
|
||||
|
||||
def get_token(data: dict = dict):
|
||||
""" create access and refresh token """
|
||||
now_time = datetime.utcnow()
|
||||
access = generate_jwt_token('access', now_time, data)
|
||||
refresh = generate_jwt_token('refresh', now_time, data)
|
||||
|
||||
return {
|
||||
'access': access,
|
||||
'refresh': refresh
|
||||
}
|
||||
164
account/views.py
164
account/views.py
@ -2,6 +2,7 @@ from rest_framework import viewsets, status, views
|
||||
from rest_framework.decorators import action
|
||||
import random
|
||||
import logging
|
||||
import jwt
|
||||
from django.contrib.auth import authenticate, login
|
||||
from guardian.models import Guardian
|
||||
from junior.models import Junior
|
||||
@ -9,40 +10,125 @@ from account.models import UserProfile, UserPhoneOtp, UserEmailOtp
|
||||
from django.contrib.auth.models import User
|
||||
from .serializers import (SuperUserSerializer, GuardianSerializer, JuniorSerializer, EmailVerificationSerializer,
|
||||
ForgotPasswordSerializer, ResetPasswordSerializer, ChangePasswordSerializer,
|
||||
GoogleSignInSerializer, UpdateGuardianImageSerializer, UpdateJuniorProfileImageSerializer)
|
||||
GoogleLoginSerializer, UpdateGuardianImageSerializer, UpdateJuniorProfileImageSerializer)
|
||||
from rest_framework_simplejwt.tokens import RefreshToken
|
||||
from base.messages import ERROR_CODE, SUCCESS_CODE
|
||||
from guardian.tasks import generate_otp
|
||||
from django.conf import settings
|
||||
from account.utils import send_otp_email
|
||||
from account.utils import custom_response, custom_error_response
|
||||
from django.core.mail import EmailMessage
|
||||
from django.core.mail import send_mail
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
from templated_email import send_templated_mail
|
||||
import google.oauth2.credentials
|
||||
import google.auth.transport.requests
|
||||
from rest_framework import status
|
||||
from rest_framework.response import Response
|
||||
import requests
|
||||
from django.conf import settings
|
||||
from .utils import get_token
|
||||
|
||||
|
||||
class GoogleLoginMixin:
|
||||
def google_login(self, request):
|
||||
access_token = request.data.get('access_token')
|
||||
user_type = request.data.get('user_type')
|
||||
if not access_token:
|
||||
return Response({'error': 'Access token is required.'}, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
try:
|
||||
# Validate the access token and obtain the user's email and name
|
||||
credentials = google.oauth2.credentials.Credentials.from_authorized_user_info(
|
||||
info={
|
||||
'access_token': access_token,
|
||||
'token_uri': 'https://oauth2.googleapis.com/token',
|
||||
'client_id': settings.GOOGLE_CLIENT_ID,
|
||||
'client_secret': settings.GOOGLE_CLIENT_SECRET,
|
||||
'refresh_token': None,
|
||||
}
|
||||
)
|
||||
user_info_endpoint = f'https://www.googleapis.com/oauth2/v3/userinfo?access_token={access_token}'
|
||||
headers = {'Authorization': f'Bearer {credentials.token}'}
|
||||
response = requests.get(user_info_endpoint, headers=headers)
|
||||
response.raise_for_status()
|
||||
user_info = response.json()
|
||||
email = user_info['email']
|
||||
first_name = user_info['given_name']
|
||||
last_name = user_info['family_name']
|
||||
profile_picture = user_info['picture']
|
||||
except Exception as e:
|
||||
return custom_error_response(str(e), response_status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
# Check if the user exists in your database or create a new user
|
||||
# ...
|
||||
user_data = User.objects.filter(email__iexact=email)
|
||||
if user_data.exists():
|
||||
if str(user_type) == '1':
|
||||
junior_query = Junior.objects.filter(auth=user_data.last()).last()
|
||||
serializer = JuniorSerializer(junior_query)
|
||||
if str(user_type) == '2':
|
||||
guardian_query = Guardian.objects.filter(user=user_data.last()).last()
|
||||
serializer = GuardianSerializer(guardian_query)
|
||||
return custom_response(SUCCESS_CODE['3003'], serializer.data,
|
||||
response_status=status.HTTP_200_OK)
|
||||
|
||||
if not User.objects.filter(email__iexact=email).exists():
|
||||
user_obj = User.objects.create(username=email, email=email, first_name=first_name, last_name=last_name)
|
||||
if str(user_type) == '1':
|
||||
junior_query = Junior.objects.create(auth=user_obj, is_verified=True, is_active=True,
|
||||
image=profile_picture)
|
||||
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)
|
||||
serializer = GuardianSerializer(guardian_query)
|
||||
# Return a JSON response with the user's email and name
|
||||
return custom_response(SUCCESS_CODE['3003'], serializer.data,
|
||||
response_status=status.HTTP_200_OK)
|
||||
|
||||
|
||||
class GoogleLoginViewSet(GoogleLoginMixin, viewsets.GenericViewSet):
|
||||
serializer_class = GoogleLoginSerializer
|
||||
|
||||
def create(self, request):
|
||||
serializer = self.get_serializer(data=request.data)
|
||||
serializer.is_valid(raise_exception=True)
|
||||
return self.google_login(request)
|
||||
|
||||
class SigninWithApple(views.APIView):
|
||||
"""This API is for sign in with Apple for app."""
|
||||
def post(self, request):
|
||||
token = request.data.get("access_token")
|
||||
user_type = request.data.get("user_type")
|
||||
if not token:
|
||||
return custom_error_response(ERROR_CODE['2027'], response_status=status.HTTP_400_BAD_REQUEST)
|
||||
try:
|
||||
decoded_data = jwt.decode(token, options={"verify_signature": False})
|
||||
user_data = {"email": decoded_data.get('email'), "username": decoded_data.get('email'), "is_active": True}
|
||||
if decoded_data.get("email"):
|
||||
try:
|
||||
user = User.objects.get(email=decoded_data.get("email"))
|
||||
if str(user_type) == '1':
|
||||
junior_query = Junior.objects.filter(auth=user).last()
|
||||
serializer = JuniorSerializer(junior_query)
|
||||
if str(user_type) == '2':
|
||||
guardian_query = Guardian.objects.filter(user=user).last()
|
||||
serializer = GuardianSerializer(guardian_query)
|
||||
return custom_response(SUCCESS_CODE['3003'], serializer.data,
|
||||
response_status=status.HTTP_200_OK)
|
||||
|
||||
except User.DoesNotExist:
|
||||
user = User.objects.create(**user_data)
|
||||
if str(user_type) == '1':
|
||||
junior_query = Junior.objects.create(auth=user, is_verified=True, is_active=True)
|
||||
serializer = JuniorSerializer(junior_query)
|
||||
if str(user_type) == '2':
|
||||
guardian_query = Guardian.objects.create(user=user, is_verified=True, is_active=True)
|
||||
serializer = GuardianSerializer(guardian_query)
|
||||
return custom_response(SUCCESS_CODE['3003'], serializer.data,
|
||||
response_status=status.HTTP_200_OK)
|
||||
except Exception as e:
|
||||
logging.error(e)
|
||||
return custom_error_response(str(e), response_status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
# class GoogleLoginAPIViewset(viewsets.ModelViewSet):
|
||||
# """Google Login"""
|
||||
# serializer_class = GoogleSignInSerializer
|
||||
#
|
||||
# def create(self, request, *args, **kwargs):
|
||||
# """
|
||||
# Override default behaviour of create method
|
||||
# """
|
||||
# provider_type = []
|
||||
# serializer = self.get_serializer(data=request.data)
|
||||
# if serializer.is_valid(raise_exception=True):
|
||||
# # provider = self.get_provider_view(request.data.get('provider'))
|
||||
# # if User is not authenticated then send error message
|
||||
# # if not provider.is_authenticated(request):
|
||||
# # return custom_error_response({}, status.HTTP_400_BAD_REQUEST)
|
||||
#
|
||||
# user = serializer.save()
|
||||
# if User.objects.filter(email__iexact=user.email).exists():
|
||||
# print("ppppppppppppp")
|
||||
# return custom_response(SUCCESS_CODE["3003"], response_status=status.HTTP_200_OK)
|
||||
# return custom_response(ERROR_CODE["2002"], response_status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
class UpdateProfileImage(views.APIView):
|
||||
permission_classes = [IsAuthenticated]
|
||||
@ -86,7 +172,7 @@ class ForgotPasswordAPIView(views.APIView):
|
||||
return custom_error_response(ERROR_CODE['2004'], response_status=status.HTTP_404_NOT_FOUND)
|
||||
verification_code = ''.join([str(random.randrange(9)) for _ in range(6)])
|
||||
# Send the verification code to the user's email
|
||||
from_email = settings.EMAIL_HOST_USER
|
||||
from_email = settings.EMAIL_FROM_ADDRESS
|
||||
recipient_list = [email]
|
||||
send_templated_mail(
|
||||
template_name='email_reset_verification.email',
|
||||
@ -142,26 +228,26 @@ class UserLogin(viewsets.ViewSet):
|
||||
username = request.data.get('username')
|
||||
password = request.data.get('password')
|
||||
user = authenticate(request, username=username, password=password)
|
||||
|
||||
try:
|
||||
if user is not None:
|
||||
login(request, user)
|
||||
guardian_data = Guardian.objects.filter(user__username=username, is_complete_profile=True).last()
|
||||
guardian_data = Guardian.objects.filter(user__username=username, is_verified=True).last()
|
||||
if guardian_data:
|
||||
serializer = GuardianSerializer(guardian_data)
|
||||
junior_data = Junior.objects.filter(auth__username=username, is_complete_profile=True).last()
|
||||
serializer = GuardianSerializer(guardian_data).data
|
||||
junior_data = Junior.objects.filter(auth__username=username, is_verified=True).last()
|
||||
if junior_data:
|
||||
serializer = JuniorSerializer(junior_data)
|
||||
return custom_response(SUCCESS_CODE['3003'], serializer.data, response_status=status.HTTP_200_OK)
|
||||
serializer = JuniorSerializer(junior_data).data
|
||||
return custom_response(SUCCESS_CODE['3003'], serializer, response_status=status.HTTP_200_OK)
|
||||
else:
|
||||
return custom_error_response(ERROR_CODE["2002"], response_status=status.HTTP_401_UNAUTHORIZED)
|
||||
except Exception as e:
|
||||
logging.error(e)
|
||||
user_profile_data = UserProfile.objects.filter(user__username=username).last()
|
||||
email_verified = UserEmailOtp.objects.filter(email=username).last()
|
||||
refresh = RefreshToken.for_user(user)
|
||||
access_token = str(refresh.access_token)
|
||||
data = {"auth_token":access_token, "is_profile_complete": False,
|
||||
"user_role": user_profile_data.user_type,
|
||||
"user_type": email_verified.user_type,
|
||||
}
|
||||
is_verified = False
|
||||
if email_verified:
|
||||
@ -208,6 +294,16 @@ class UserEmailVerification(viewsets.ModelViewSet):
|
||||
if email_data:
|
||||
email_data.is_verified = True
|
||||
email_data.save()
|
||||
if email_data.user_type == '1':
|
||||
junior_data = Junior.objects.filter(auth__email=self.request.GET.get('email')).last()
|
||||
if junior_data:
|
||||
junior_data.is_verified = True
|
||||
junior_data.save()
|
||||
else:
|
||||
guardian_data = Guardian.objects.filter(user__email=self.request.GET.get('email')).last()
|
||||
if guardian_data:
|
||||
guardian_data.is_verified = True
|
||||
guardian_data.save()
|
||||
refresh = RefreshToken.for_user(user_obj)
|
||||
access_token = str(refresh.access_token)
|
||||
return custom_response(SUCCESS_CODE['3011'], {"auth_token":access_token}, response_status=status.HTTP_200_OK)
|
||||
|
||||
@ -24,7 +24,7 @@ ERROR_CODE_REQUIRED = {
|
||||
# Error code
|
||||
ERROR_CODE = {
|
||||
"2000": "Email not found.",
|
||||
"2001": "Your account has not been verified. Please check your email and verify it.",
|
||||
"2001": "This is your existing password. Please choose other one",
|
||||
"2002": "Invalid login credentials.",
|
||||
"2003": "An account already exists with this email address.",
|
||||
"2004": "User not found.",
|
||||
@ -45,11 +45,12 @@ ERROR_CODE = {
|
||||
"2019": "Either File extension or File size doesn't meet the requirements",
|
||||
"2020": "Enter valid mobile number",
|
||||
"2021": "Already register",
|
||||
"2022":"Invalid Guardian code",
|
||||
"2023":"Invalid user",
|
||||
"2024":"Email not verified",
|
||||
"2025":"Invalid input. Expected a list of strings.",
|
||||
"2026" : "New password should not same as old password"
|
||||
"2022": "Invalid Guardian code",
|
||||
"2023": "Invalid user",
|
||||
"2024": "Email not verified",
|
||||
"2025": "Invalid input. Expected a list of strings.",
|
||||
"2026": "New password should not same as old password",
|
||||
"2027": "data should contain `identityToken`"
|
||||
}
|
||||
SUCCESS_CODE = {
|
||||
# Success code for password
|
||||
|
||||
18
guardian/migrations/0006_guardian_is_verified.py
Normal file
18
guardian/migrations/0006_guardian_is_verified.py
Normal file
@ -0,0 +1,18 @@
|
||||
# Generated by Django 4.2.2 on 2023-06-29 12:10
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('guardian', '0005_alter_guardian_image'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='guardian',
|
||||
name='is_verified',
|
||||
field=models.BooleanField(default=False),
|
||||
),
|
||||
]
|
||||
@ -20,14 +20,15 @@ class Guardian(models.Model):
|
||||
family_name = models.CharField(max_length=50, null=True, blank=True, default=None)
|
||||
gender = models.CharField(choices=GENDERS, max_length=15, null=True, blank=True, default=None)
|
||||
dob = models.DateField(max_length=15, null=True, blank=True, default=None)
|
||||
"""Profile activity"""
|
||||
is_active = models.BooleanField(default=True)
|
||||
is_verified = models.BooleanField(default=False)
|
||||
is_complete_profile = models.BooleanField(default=False)
|
||||
passcode = models.IntegerField(null=True, blank=True, default=None)
|
||||
"""Codes"""
|
||||
guardian_code = models.CharField(max_length=10, null=True, blank=True, default=None)
|
||||
referral_code = models.CharField(max_length=10, null=True, blank=True, default=None)
|
||||
referral_code_used = models.CharField(max_length=10, null=True, blank=True, default=None)
|
||||
"""Profile activity"""
|
||||
is_active = models.BooleanField(default=True)
|
||||
is_complete_profile = models.BooleanField(default=False)
|
||||
passcode = models.IntegerField(null=True, blank=True, default=None)
|
||||
"""Profile created and updated time"""
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
@ -8,7 +8,7 @@ from django.db import transaction
|
||||
from django.contrib.auth.models import User
|
||||
"""Import Django app"""
|
||||
from .models import Guardian
|
||||
from account.models import UserProfile
|
||||
from account.models import UserProfile, UserEmailOtp
|
||||
from base.messages import ERROR_CODE, SUCCESS_CODE
|
||||
from .utils import upload_image_to_alibaba
|
||||
from junior.models import Junior
|
||||
@ -35,17 +35,21 @@ class UserSerializer(serializers.ModelSerializer):
|
||||
"""Create user profile"""
|
||||
user = User.objects.create_user(username=email, email=email, password=password)
|
||||
UserProfile.objects.create(user=user, user_type=user_type)
|
||||
if user_type == '1':
|
||||
Junior.objects.create(auth=user)
|
||||
if user_type == '2':
|
||||
Guardian.objects.create(user=user)
|
||||
return user
|
||||
except Exception as e:
|
||||
"""Error handling"""
|
||||
logging.error(e)
|
||||
raise serializers.ValidationError({"details":ERROR_CODE['2021']})
|
||||
|
||||
def save(self, **kwargs):
|
||||
"""save the data"""
|
||||
with transaction.atomic():
|
||||
instance = super().save(**kwargs)
|
||||
return instance
|
||||
otp = UserEmailOtp.objects.filter(email=email).last()
|
||||
otp_verified = False
|
||||
if otp and otp.is_verified:
|
||||
otp_verified = True
|
||||
raise serializers.ValidationError({"details":ERROR_CODE['2021'], "otp_verified":bool(otp_verified),
|
||||
"code": 400, "status":"failed",
|
||||
})
|
||||
|
||||
class CreateGuardianSerializer(serializers.ModelSerializer):
|
||||
"""Create guardian serializer"""
|
||||
@ -82,11 +86,11 @@ class CreateGuardianSerializer(serializers.ModelSerializer):
|
||||
|
||||
def create(self, validated_data):
|
||||
"""Create guardian profile"""
|
||||
phone_number = validated_data.pop('phone', None)
|
||||
guardian_data = Guardian.objects.filter(phone=phone_number)
|
||||
junior_data = Junior.objects.filter(phone=phone_number)
|
||||
if phone_number and (guardian_data or junior_data):
|
||||
raise serializers.ValidationError({"details": ERROR_CODE['2012']})
|
||||
# phone_number = validated_data.get('phone', None)
|
||||
# guardian_data = Guardian.objects.filter(phone=phone_number)
|
||||
# junior_data = Junior.objects.filter(phone=phone_number)
|
||||
# if phone_number and (guardian_data or junior_data):
|
||||
# raise serializers.ValidationError({"details": ERROR_CODE['2012']})
|
||||
user = User.objects.filter(username=self.context['user']).last()
|
||||
if user:
|
||||
"""Save first and last name of guardian"""
|
||||
|
||||
@ -3,8 +3,9 @@
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
from rest_framework import viewsets, status
|
||||
"""Import Django app"""
|
||||
from .serializers import UserSerializer
|
||||
from .serializers import CreateGuardianSerializer
|
||||
from .serializers import UserSerializer, CreateGuardianSerializer
|
||||
from .models import Guardian
|
||||
from junior.models import Junior
|
||||
from account.models import UserEmailOtp
|
||||
from .tasks import generate_otp
|
||||
from account.utils import send_otp_email
|
||||
@ -22,7 +23,7 @@ class SignupViewset(viewsets.ModelViewSet):
|
||||
serializer.save()
|
||||
"""Generate otp"""
|
||||
otp = generate_otp()
|
||||
UserEmailOtp.objects.create(email=request.data['email'], otp=otp)
|
||||
UserEmailOtp.objects.create(email=request.data['email'], otp=otp, user_type=str(request.data['user_type']))
|
||||
"""Send email to the register user"""
|
||||
send_otp_email(request.data['email'], otp)
|
||||
return custom_response(SUCCESS_CODE['3001'], {"email_otp": otp},
|
||||
|
||||
18
junior/migrations/0005_junior_is_verified.py
Normal file
18
junior/migrations/0005_junior_is_verified.py
Normal file
@ -0,0 +1,18 @@
|
||||
# Generated by Django 4.2.2 on 2023-06-29 12:10
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('junior', '0004_alter_junior_image'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='junior',
|
||||
name='is_verified',
|
||||
field=models.BooleanField(default=False),
|
||||
),
|
||||
]
|
||||
@ -28,6 +28,7 @@ class Junior(models.Model):
|
||||
is_active = models.BooleanField(default=True)
|
||||
is_complete_profile = models.BooleanField(default=False)
|
||||
passcode = models.IntegerField(null=True, blank=True, default=None)
|
||||
is_verified = models.BooleanField(default=False)
|
||||
"""Profile created and updated time"""
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
@ -58,13 +58,12 @@ class CreateJuniorSerializer(serializers.ModelSerializer):
|
||||
|
||||
def create(self, validated_data):
|
||||
"""Create junior profile"""
|
||||
image = validated_data.pop('image', None)
|
||||
phone_number = validated_data.pop('phone', None)
|
||||
print("phone_number====>",phone_number,'==>',type(phone_number))
|
||||
guardian_data = Guardian.objects.filter(phone=phone_number)
|
||||
junior_data = Junior.objects.filter(phone=phone_number)
|
||||
if phone_number and (junior_data or guardian_data):
|
||||
raise serializers.ValidationError({"details":ERROR_CODE['2012']})
|
||||
image = validated_data.get('image', None)
|
||||
# phone_number = validated_data.get('phone', None)
|
||||
# guardian_data = Guardian.objects.filter(phone=phone_number)
|
||||
# junior_data = Junior.objects.filter(phone=phone_number)
|
||||
# if phone_number and (junior_data or guardian_data):
|
||||
# raise serializers.ValidationError({"details":ERROR_CODE['2012']})
|
||||
user = User.objects.filter(username=self.context['user']).last()
|
||||
if user:
|
||||
"""Save first and last name of junior"""
|
||||
|
||||
@ -19,6 +19,6 @@ upstream web {
|
||||
|
||||
location /static {
|
||||
autoindex on;
|
||||
alias /usr/src/app/zod_bank/static/;
|
||||
alias /usr/src/app/static/;
|
||||
}
|
||||
}
|
||||
|
||||
@ -8,6 +8,7 @@ async-timeout==4.0.2
|
||||
billiard==4.1.0
|
||||
boto3==1.26.157
|
||||
botocore==1.29.157
|
||||
cachetools==5.3.1
|
||||
celery==5.3.1
|
||||
certifi==2023.5.7
|
||||
cffi==1.15.1
|
||||
@ -38,6 +39,7 @@ django-timezone-field==5.1
|
||||
djangorestframework==3.14.0
|
||||
djangorestframework-simplejwt==5.2.2
|
||||
drf-yasg==1.21.6
|
||||
google-auth==2.21.0
|
||||
gunicorn==20.1.0
|
||||
idna==3.4
|
||||
inflection==0.5.1
|
||||
@ -50,6 +52,8 @@ phonenumbers==8.13.15
|
||||
Pillow==9.5.0
|
||||
prompt-toolkit==3.0.38
|
||||
psycopg==3.1.9
|
||||
pyasn1==0.5.0
|
||||
pyasn1-modules==0.3.0
|
||||
pycparser==2.21
|
||||
pycryptodome==3.18.0
|
||||
PyJWT==2.7.0
|
||||
@ -60,6 +64,7 @@ pytz==2023.3
|
||||
PyYAML==6.0
|
||||
redis==4.5.5
|
||||
requests==2.31.0
|
||||
rsa==4.9
|
||||
s3transfer==0.6.1
|
||||
six==1.16.0
|
||||
sqlparse==0.4.4
|
||||
|
||||
@ -56,6 +56,7 @@ INSTALLED_APPS = [
|
||||
'account',
|
||||
'junior',
|
||||
'guardian',
|
||||
# 'social_django'
|
||||
]
|
||||
|
||||
MIDDLEWARE = [
|
||||
@ -95,7 +96,7 @@ REST_FRAMEWORK = {
|
||||
]
|
||||
}
|
||||
SIMPLE_JWT = {
|
||||
'ACCESS_TOKEN_LIFETIME': timedelta(minutes=15),
|
||||
'ACCESS_TOKEN_LIFETIME': timedelta(minutes=50),
|
||||
'REFRESH_TOKEN_LIFETIME': timedelta(days=7),
|
||||
}
|
||||
# Database
|
||||
@ -171,30 +172,49 @@ CORS_ALLOW_HEADERS = (
|
||||
"""Static files (CSS, JavaScript, Images)
|
||||
https://docs.djangoproject.com/en/3.0/howto/static-files/"""
|
||||
|
||||
# AUTHENTICATION_BACKENDS = [
|
||||
# 'social_core.backends.google.GoogleOAuth2',
|
||||
# 'django.contrib.auth.backends.ModelBackend',
|
||||
# ]
|
||||
#
|
||||
# LOGIN_URL = 'login'
|
||||
# LOGIN_REDIRECT_URL = 'home'
|
||||
# LOGOUT_URL = 'logout'
|
||||
# LOGOUT_REDIRECT_URL = 'login'
|
||||
|
||||
# Email settings For temporary use
|
||||
# EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
|
||||
# EMAIL_HOST = 'smtp.gmail.com'
|
||||
# EMAIL_PORT = 587
|
||||
# EMAIL_USE_TLS = True
|
||||
# EMAIL_HOST_USER = 'infozodbank@gmail.com'
|
||||
# # Replace with your Gmail email password or App password
|
||||
# EMAIL_HOST_PASSWORD = 'ghwdmznwwslvchga'
|
||||
# SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = ''
|
||||
# SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = ''
|
||||
|
||||
EMAIL_BACKEND = os.getenv('EMAIL_BACKEND')
|
||||
EMAIL_HOST = os.getenv('EMAIL_HOST')
|
||||
EMAIL_PORT = os.getenv('EMAIL_PORT')
|
||||
EMAIL_USE_TLS = os.getenv('EMAIL_USE_TLS')
|
||||
EMAIL_HOST_USER = os.getenv('EMAIL_HOST_USER') # Replace with your Gmail email address
|
||||
EMAIL_HOST_PASSWORD = os.getenv('EMAIL_HOST_PASSWORD') # Replace with your Gmail email password or App password
|
||||
EMAIL_FROM_ADDRESS = os.getenv('EMAIL_FROM_ADDRESS')
|
||||
GOOGLE_CLIENT_ID = "182276566528-hlbjncs19fo502jposod6kft2p9k4grk.apps.googleusercontent.com"
|
||||
GOOGLE_CLIENT_SECRET = "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"
|
||||
|
||||
ALIYUN_OSS_ACCESS_KEY_ID = os.getenv('ALIYUN_OSS_ACCESS_KEY_ID')
|
||||
ALIYUN_OSS_ACCESS_KEY_SECRET = os.getenv('ALIYUN_OSS_ACCESS_KEY_SECRET')
|
||||
ALIYUN_OSS_BUCKET_NAME = os.getenv('ALIYUN_OSS_BUCKET_NAME')
|
||||
ALIYUN_OSS_ENDPOINT = os.getenv('ALIYUN_OSS_ENDPOINT')
|
||||
ALIYUN_OSS_REGION = os.getenv('ALIYUN_OSS_REGION')
|
||||
# EMAIL_BACKEND = os.getenv('EMAIL_BACKEND')
|
||||
# EMAIL_HOST = os.getenv('EMAIL_HOST')
|
||||
# EMAIL_PORT = os.getenv('EMAIL_PORT')
|
||||
# EMAIL_USE_TLS = os.getenv('EMAIL_USE_TLS')
|
||||
# EMAIL_HOST_USER = os.getenv('EMAIL_HOST_USER') # Replace with your Gmail email address
|
||||
# EMAIL_HOST_PASSWORD = os.getenv('EMAIL_HOST_PASSWORD') # Replace with your Gmail email password or App password
|
||||
# EMAIL_FROM_ADDRESS = os.getenv('EMAIL_FROM_ADDRESS')
|
||||
|
||||
EMAIL_BACKEND="django.core.mail.backends.smtp.EmailBackend"
|
||||
EMAIL_HOST="smtp.sendgrid.net"
|
||||
EMAIL_PORT="587"
|
||||
EMAIL_USE_TLS="True"
|
||||
EMAIL_HOST_USER="apikey" # Replace with your Gmail email address
|
||||
EMAIL_HOST_PASSWORD="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"
|
||||
EMAIL_FROM_ADDRESS="zodbank@yopmail.com"
|
||||
|
||||
# ALIYUN_OSS_ACCESS_KEY_ID = os.getenv('ALIYUN_OSS_ACCESS_KEY_ID')
|
||||
# ALIYUN_OSS_ACCESS_KEY_SECRET = os.getenv('ALIYUN_OSS_ACCESS_KEY_SECRET')
|
||||
# ALIYUN_OSS_BUCKET_NAME = os.getenv('ALIYUN_OSS_BUCKET_NAME')
|
||||
# ALIYUN_OSS_ENDPOINT = os.getenv('ALIYUN_OSS_ENDPOINT')
|
||||
# ALIYUN_OSS_REGION = os.getenv('ALIYUN_OSS_REGION')
|
||||
|
||||
ALIYUN_OSS_ACCESS_KEY_ID="LTAI5t7w1gq1CswJtvxtEZTd"
|
||||
ALIYUN_OSS_ACCESS_KEY_SECRET="6yknAFpP2gVMhCWAJwbAjCEw2eehpf"
|
||||
ALIYUN_OSS_BUCKET_NAME="zod-dev"
|
||||
ALIYUN_OSS_ENDPOINT="oss-me-central-1.aliyuncs.com"
|
||||
ALIYUN_OSS_REGION="Global"
|
||||
|
||||
STATIC_URL = 'static/'
|
||||
STATIC_ROOT = 'static'
|
||||
|
||||
Reference in New Issue
Block a user