mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-07-15 18:07:02 +00:00
Merge branch 'login_api_reduced_queries' of github.com:KiwiTechLLC/ZODBank-Backend into login_api_reduced_queries
This commit is contained in:
@ -216,10 +216,17 @@ class GuardianSerializer(serializers.ModelSerializer):
|
|||||||
|
|
||||||
def get_user_type(self, obj):
|
def get_user_type(self, obj):
|
||||||
"""user type"""
|
"""user type"""
|
||||||
email_verified = UserEmailOtp.objects.filter(email=obj.user.username).last()
|
if self.context.get('user_type', ''):
|
||||||
if email_verified and email_verified.user_type is not None:
|
return self.context.get('user_type')
|
||||||
return email_verified.user_type
|
# remove the below code once user_type can be passed
|
||||||
return str(NUMBER['two'])
|
# from everywhere from where this serializer is being called
|
||||||
|
else:
|
||||||
|
email_verified = UserEmailOtp.objects.filter(
|
||||||
|
email=obj.user.username
|
||||||
|
).last()
|
||||||
|
if email_verified and email_verified.user_type is not None:
|
||||||
|
return email_verified.user_type
|
||||||
|
return str(NUMBER['two'])
|
||||||
|
|
||||||
def get_auth(self, obj):
|
def get_auth(self, obj):
|
||||||
"""user email address"""
|
"""user email address"""
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
"""Account utils"""
|
"""Account utils"""
|
||||||
|
from celery import shared_task
|
||||||
|
|
||||||
"""Import django"""
|
"""Import django"""
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from rest_framework import viewsets, status
|
from rest_framework import viewsets, status
|
||||||
@ -20,7 +22,7 @@ from rest_framework import serializers
|
|||||||
# Import messages from base package"""
|
# Import messages from base package"""
|
||||||
from junior.models import Junior
|
from junior.models import Junior
|
||||||
from guardian.models import Guardian
|
from guardian.models import Guardian
|
||||||
from account.models import UserDelete
|
from account.models import UserDelete, UserDeviceDetails
|
||||||
from base.messages import ERROR_CODE
|
from base.messages import ERROR_CODE
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from base.constants import NUMBER
|
from base.constants import NUMBER
|
||||||
@ -109,6 +111,7 @@ def guardian_account_update(user_tb):
|
|||||||
for data in jun_data:
|
for data in jun_data:
|
||||||
data.guardian_code.remove(guardian_data.guardian_code)
|
data.guardian_code.remove(guardian_data.guardian_code)
|
||||||
data.save()
|
data.save()
|
||||||
|
@shared_task()
|
||||||
def send_otp_email(recipient_email, otp):
|
def send_otp_email(recipient_email, otp):
|
||||||
"""Send otp on email with template"""
|
"""Send otp on email with template"""
|
||||||
from_email = settings.EMAIL_FROM_ADDRESS
|
from_email = settings.EMAIL_FROM_ADDRESS
|
||||||
@ -124,6 +127,20 @@ def send_otp_email(recipient_email, otp):
|
|||||||
)
|
)
|
||||||
return otp
|
return otp
|
||||||
|
|
||||||
|
@shared_task
|
||||||
|
def user_device_details(user, device_id):
|
||||||
|
"""
|
||||||
|
Used to store the device id of the user
|
||||||
|
user: user object
|
||||||
|
device_id: string
|
||||||
|
return
|
||||||
|
"""
|
||||||
|
device_details, created = UserDeviceDetails.objects.get_or_create(user=user)
|
||||||
|
if device_details:
|
||||||
|
device_details.device_id = device_id
|
||||||
|
device_details.save()
|
||||||
|
|
||||||
|
|
||||||
def send_support_email(name, sender, subject, message):
|
def send_support_email(name, sender, subject, message):
|
||||||
"""Send otp on email with template"""
|
"""Send otp on email with template"""
|
||||||
to_email = [settings.EMAIL_FROM_ADDRESS]
|
to_email = [settings.EMAIL_FROM_ADDRESS]
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
"""Account view """
|
"""Account view """
|
||||||
|
import threading
|
||||||
|
|
||||||
from notifications.utils import remove_fcm_token
|
from notifications.utils import remove_fcm_token
|
||||||
|
|
||||||
# django imports
|
# django imports
|
||||||
@ -35,10 +37,10 @@ from .serializers import (SuperUserSerializer, GuardianSerializer, JuniorSeriali
|
|||||||
AdminLoginSerializer)
|
AdminLoginSerializer)
|
||||||
from rest_framework_simplejwt.tokens import RefreshToken
|
from rest_framework_simplejwt.tokens import RefreshToken
|
||||||
from base.messages import ERROR_CODE, SUCCESS_CODE
|
from base.messages import ERROR_CODE, SUCCESS_CODE
|
||||||
from base.constants import NUMBER, ZOD, JUN, GRD
|
from base.constants import NUMBER, ZOD, JUN, GRD, USER_TYPE_FLAG
|
||||||
from guardian.tasks import generate_otp
|
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_code, OTP_EXPIRY)
|
generate_code, OTP_EXPIRY, user_device_details)
|
||||||
from junior.serializers import JuniorProfileSerializer
|
from junior.serializers import JuniorProfileSerializer
|
||||||
from guardian.serializers import GuardianProfileSerializer
|
from guardian.serializers import GuardianProfileSerializer
|
||||||
|
|
||||||
@ -280,29 +282,38 @@ class UserPhoneVerification(viewsets.ModelViewSet):
|
|||||||
return custom_error_response(ERROR_CODE["2008"], response_status=status.HTTP_400_BAD_REQUEST)
|
return custom_error_response(ERROR_CODE["2008"], response_status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class UserLogin(viewsets.ViewSet):
|
class UserLogin(viewsets.ViewSet):
|
||||||
"""User login"""
|
"""User login"""
|
||||||
@action(methods=['post'], detail=False)
|
@action(methods=['post'], detail=False)
|
||||||
def login(self, request):
|
def login(self, request):
|
||||||
username = request.data.get('username')
|
username = request.data.get('username')
|
||||||
password = request.data.get('password')
|
password = request.data.get('password')
|
||||||
|
user_type = request.data.get('user_type')
|
||||||
device_id = request.META.get('HTTP_DEVICE_ID')
|
device_id = request.META.get('HTTP_DEVICE_ID')
|
||||||
user = authenticate(request, username=username, password=password)
|
user = authenticate(request, username=username, password=password)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if user is not None:
|
if user is not None:
|
||||||
login(request, user)
|
login(request, user)
|
||||||
guardian_data = Guardian.objects.filter(user__username=username, is_verified=True).last()
|
if user_type == USER_TYPE_FLAG["FIRST"]:
|
||||||
if guardian_data:
|
guardian_data = Guardian.objects.filter(user__username=username, is_verified=True).last()
|
||||||
serializer = GuardianSerializer(guardian_data).data
|
if guardian_data:
|
||||||
junior_data = Junior.objects.filter(auth__username=username, is_verified=True).last()
|
serializer = GuardianSerializer(
|
||||||
if junior_data:
|
guardian_data, context={'user_type': user_type}
|
||||||
serializer = JuniorSerializer(junior_data).data
|
).data
|
||||||
device_details, created = UserDeviceDetails.objects.get_or_create(user=user)
|
elif user_type == USER_TYPE_FLAG["TWO"]:
|
||||||
if device_details:
|
junior_data = Junior.objects.filter(auth__username=username, is_verified=True).last()
|
||||||
device_details.device_id = device_id
|
if junior_data:
|
||||||
device_details.save()
|
serializer = JuniorSerializer(
|
||||||
|
junior_data, context={'user_type': user_type}
|
||||||
|
).data
|
||||||
|
else:
|
||||||
|
return custom_error_response(
|
||||||
|
ERROR_CODE["2069"],
|
||||||
|
response_status=status.HTTP_401_UNAUTHORIZED
|
||||||
|
)
|
||||||
|
# storing device id in using thread so the time would be reduced
|
||||||
|
threading.Thread(target=user_device_details, args=(user, device_id))
|
||||||
return custom_response(SUCCESS_CODE['3003'], serializer, response_status=status.HTTP_200_OK)
|
return custom_response(SUCCESS_CODE['3003'], serializer, response_status=status.HTTP_200_OK)
|
||||||
else:
|
else:
|
||||||
return custom_error_response(ERROR_CODE["2002"], response_status=status.HTTP_401_UNAUTHORIZED)
|
return custom_error_response(ERROR_CODE["2002"], response_status=status.HTTP_401_UNAUTHORIZED)
|
||||||
@ -312,9 +323,12 @@ class UserLogin(viewsets.ViewSet):
|
|||||||
refresh = RefreshToken.for_user(user)
|
refresh = RefreshToken.for_user(user)
|
||||||
access_token = str(refresh.access_token)
|
access_token = str(refresh.access_token)
|
||||||
refresh_token = str(refresh)
|
refresh_token = str(refresh)
|
||||||
data = {"auth_token":access_token, "refresh_token":refresh_token, "is_profile_complete": False,
|
data = {
|
||||||
"user_type": email_verified.user_type,
|
"auth_token":access_token,
|
||||||
}
|
"refresh_token":refresh_token,
|
||||||
|
"is_profile_complete": False,
|
||||||
|
"user_type": user_type,
|
||||||
|
}
|
||||||
is_verified = False
|
is_verified = False
|
||||||
if email_verified:
|
if email_verified:
|
||||||
is_verified = email_verified.is_verified
|
is_verified = email_verified.is_verified
|
||||||
@ -323,11 +337,18 @@ class UserLogin(viewsets.ViewSet):
|
|||||||
email_verified.otp = otp
|
email_verified.otp = otp
|
||||||
email_verified.save()
|
email_verified.save()
|
||||||
data.update({"email_otp":otp})
|
data.update({"email_otp":otp})
|
||||||
send_otp_email(username, otp)
|
send_otp_email.delay(username, otp)
|
||||||
return custom_response(ERROR_CODE['2024'], {"email_otp": otp, "is_email_verified": is_verified},
|
return custom_response(
|
||||||
response_status=status.HTTP_200_OK)
|
ERROR_CODE['2024'],
|
||||||
|
{"email_otp": otp, "is_email_verified": is_verified},
|
||||||
|
response_status=status.HTTP_200_OK
|
||||||
|
)
|
||||||
data.update({"is_email_verified": is_verified})
|
data.update({"is_email_verified": is_verified})
|
||||||
return custom_response(SUCCESS_CODE['3003'], data, response_status=status.HTTP_200_OK)
|
return custom_response(
|
||||||
|
SUCCESS_CODE['3003'],
|
||||||
|
data,
|
||||||
|
response_status=status.HTTP_200_OK
|
||||||
|
)
|
||||||
|
|
||||||
@action(methods=['post'], detail=False)
|
@action(methods=['post'], detail=False)
|
||||||
def admin_login(self, request):
|
def admin_login(self, request):
|
||||||
|
@ -50,6 +50,13 @@ USER_TYPE = (
|
|||||||
('2', 'guardian'),
|
('2', 'guardian'),
|
||||||
('3', 'superuser')
|
('3', 'superuser')
|
||||||
)
|
)
|
||||||
|
|
||||||
|
USER_TYPE_FLAG = {
|
||||||
|
"FIRST" : "1",
|
||||||
|
"TWO" : "2",
|
||||||
|
"THREE": "3"
|
||||||
|
}
|
||||||
|
|
||||||
"""gender"""
|
"""gender"""
|
||||||
GENDERS = (
|
GENDERS = (
|
||||||
('1', 'Male'),
|
('1', 'Male'),
|
||||||
|
@ -94,7 +94,8 @@ ERROR_CODE = {
|
|||||||
"2065": "Passwords do not match. Please try again.",
|
"2065": "Passwords do not match. Please try again.",
|
||||||
"2066": "Task does not exist or not in expired state",
|
"2066": "Task does not exist or not in expired state",
|
||||||
"2067": "Action not allowed. User type missing.",
|
"2067": "Action not allowed. User type missing.",
|
||||||
"2068": "No guardian associated with this junior"
|
"2068": "No guardian associated with this junior",
|
||||||
|
"2069": "Invalid user type"
|
||||||
|
|
||||||
}
|
}
|
||||||
"""Success message code"""
|
"""Success message code"""
|
||||||
|
Reference in New Issue
Block a user