mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-07-15 18:07:02 +00:00
changes
This commit is contained in:
196
account/views.py
Normal file
196
account/views.py
Normal file
@ -0,0 +1,196 @@
|
||||
from rest_framework import viewsets, status, views
|
||||
from rest_framework.decorators import action
|
||||
import random
|
||||
import logging
|
||||
from django.contrib.auth import authenticate, login
|
||||
from guardian.models import Guardian
|
||||
from junior.models import Junior
|
||||
from account.models import UserProfile, UserPhoneOtp, UserEmailOtp
|
||||
from django.contrib.auth.models import User
|
||||
from .serializers import (SuperUserSerializer, GuardianSerializer, JuniorSerializer, EmailVerificationSerializer,
|
||||
ForgotPasswordSerializer, ResetPasswordSerializer, ChangePasswordSerializer)
|
||||
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
|
||||
|
||||
|
||||
class ChangePasswordAPIView(views.APIView):
|
||||
permission_classes = [IsAuthenticated]
|
||||
def post(self, request):
|
||||
serializer = ChangePasswordSerializer(context=request.user, data=request.data)
|
||||
if serializer.is_valid():
|
||||
serializer.save()
|
||||
return custom_response(SUCCESS_CODE['3007'], response_status=status.HTTP_200_OK)
|
||||
return custom_error_response(serializer.errors, response_status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
class ResetPasswordAPIView(views.APIView):
|
||||
def post(self, request):
|
||||
serializer = ResetPasswordSerializer(data=request.data)
|
||||
if serializer.is_valid():
|
||||
serializer.save()
|
||||
return custom_response(SUCCESS_CODE['3006'], response_status=status.HTTP_200_OK)
|
||||
return custom_error_response(serializer.errors, response_status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
class ForgotPasswordAPIView(views.APIView):
|
||||
def post(self, request):
|
||||
serializer = ForgotPasswordSerializer(data=request.data)
|
||||
if serializer.is_valid():
|
||||
email = serializer.validated_data['email']
|
||||
try:
|
||||
User.objects.get(email=email)
|
||||
except User.DoesNotExist:
|
||||
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
|
||||
recipient_list = [email]
|
||||
send_templated_mail(
|
||||
template_name='email_reset_verification.email',
|
||||
from_email=from_email,
|
||||
recipient_list=recipient_list,
|
||||
context={
|
||||
'verification_code': verification_code
|
||||
}
|
||||
)
|
||||
user_data, created = UserEmailOtp.objects.get_or_create(email=email)
|
||||
if user_data:
|
||||
user_data.otp = verification_code
|
||||
user_data.save()
|
||||
return custom_response(SUCCESS_CODE['3015'],
|
||||
response_status=status.HTTP_200_OK)
|
||||
return custom_error_response(serializer.errors, response_status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
class SendPhoneOtp(viewsets.ModelViewSet):
|
||||
"""Send otp on phone"""
|
||||
def create(self, request, *args, **kwargs):
|
||||
otp = generate_otp()
|
||||
phone_number = self.request.data['phone']
|
||||
if phone_number.isdigit() and len(phone_number) == 10:
|
||||
phone_otp, created = UserPhoneOtp.objects.get_or_create(country_code=self.request.data['country_code'],
|
||||
phone=self.request.data['phone'])
|
||||
if phone_otp:
|
||||
phone_otp.otp = otp
|
||||
phone_otp.save()
|
||||
return custom_response(None, {'phone_otp':otp}, response_status=status.HTTP_200_OK)
|
||||
return custom_error_response(ERROR_CODE['2020'], response_status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
|
||||
class UserPhoneVerification(viewsets.ModelViewSet):
|
||||
"""Send otp on phone"""
|
||||
def list(self, request, *args, **kwargs):
|
||||
try:
|
||||
phone_data = UserPhoneOtp.objects.filter(phone=self.request.GET.get('phone'),
|
||||
otp=self.request.GET.get('otp')).last()
|
||||
if phone_data:
|
||||
phone_data.is_verified = True
|
||||
phone_data.save()
|
||||
return custom_response(SUCCESS_CODE['3012'], response_status=status.HTTP_200_OK)
|
||||
else:
|
||||
return custom_error_response(ERROR_CODE["2008"], response_status=status.HTTP_400_BAD_REQUEST)
|
||||
except Exception:
|
||||
return custom_error_response(ERROR_CODE["2008"], response_status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
|
||||
|
||||
class UserLogin(viewsets.ViewSet):
|
||||
@action(methods=['post'], detail=False)
|
||||
def login(self, request):
|
||||
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()
|
||||
if guardian_data:
|
||||
serializer = GuardianSerializer(guardian_data)
|
||||
junior_data = Junior.objects.filter(auth__username=username, is_complete_profile=True).last()
|
||||
if junior_data:
|
||||
serializer = JuniorSerializer(junior_data)
|
||||
return custom_response(SUCCESS_CODE['3003'], serializer.data, 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,
|
||||
}
|
||||
is_verified = False
|
||||
if email_verified:
|
||||
is_verified = email_verified.is_verified
|
||||
if not is_verified:
|
||||
otp = generate_otp()
|
||||
email_verified.otp = otp
|
||||
email_verified.save()
|
||||
data.update({"email_otp":otp})
|
||||
return custom_response(ERROR_CODE['2024'], {"email_otp": otp, "is_email_verified": is_verified},
|
||||
response_status=status.HTTP_200_OK)
|
||||
data.update({"is_email_verified": is_verified})
|
||||
return custom_response(None, data, response_status=status.HTTP_200_OK)
|
||||
|
||||
@action(methods=['post'], detail=False)
|
||||
def admin_login(self, request):
|
||||
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)
|
||||
if user.is_superuser:
|
||||
serializer = SuperUserSerializer(user)
|
||||
return custom_response(SUCCESS_CODE['3003'], serializer.data, 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)
|
||||
refresh = RefreshToken.for_user(user)
|
||||
access_token = str(refresh.access_token)
|
||||
data = {"auth_token": access_token, "user_role": '3'}
|
||||
return custom_response(None, data, response_status=status.HTTP_200_OK)
|
||||
|
||||
class UserEmailVerification(viewsets.ModelViewSet):
|
||||
"""User Email verification"""
|
||||
serializer_class = EmailVerificationSerializer
|
||||
|
||||
def list(self, request, *args, **kwargs):
|
||||
try:
|
||||
user_obj = User.objects.filter(username=self.request.GET.get('email')).last()
|
||||
email_data = UserEmailOtp.objects.filter(email=self.request.GET.get('email'),
|
||||
otp=self.request.GET.get('otp')).last()
|
||||
if email_data:
|
||||
email_data.is_verified = True
|
||||
email_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)
|
||||
else:
|
||||
return custom_error_response(ERROR_CODE["2008"], response_status=status.HTTP_400_BAD_REQUEST)
|
||||
except Exception as e:
|
||||
logging.error(e)
|
||||
return custom_error_response(ERROR_CODE["2008"], response_status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
class ReSendEmailOtp(viewsets.ModelViewSet):
|
||||
"""Send otp on phone"""
|
||||
def create(self, request, *args, **kwargs):
|
||||
otp = generate_otp()
|
||||
if User.objects.filter(email=request.data['email']):
|
||||
email_data, created = UserEmailOtp.objects.get_or_create(email=request.data['email'])
|
||||
if email_data:
|
||||
email_data.otp = otp
|
||||
email_data.save()
|
||||
send_otp_email(request.data['email'], otp)
|
||||
return custom_response(SUCCESS_CODE['3016'], response_status=status.HTTP_200_OK)
|
||||
else:
|
||||
return custom_error_response(ERROR_CODE["2023"], response_status=status.HTTP_400_BAD_REQUEST)
|
Reference in New Issue
Block a user