from rest_framework import viewsets, status from rest_framework.decorators import action from rest_framework.response import Response 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 from django.views.decorators.csrf import csrf_exempt from rest_framework_simplejwt.serializers import TokenObtainPairSerializer from rest_framework_simplejwt.views import TokenObtainPairView from rest_framework_simplejwt.tokens import RefreshToken from base.messages import ERROR_CODE, SUCCESS_CODE from guardian.tasks import generate_otp from account.utils import custom_response, custom_error_response class SendPhoneOtp(viewsets.ModelViewSet): """Send otp on phone""" def create(self, request, *args, **kwargs): otp = generate_otp() UserPhoneOtp.objects.create(country_code=self.request.data['country_code'], phone=self.request.data['phone'], otp=otp) return custom_response(None, {'phone_otp':otp}, response_status=status.HTTP_200_OK) class UserPhoneVerification(viewsets.ModelViewSet): """Send otp on phone""" def list(self, request, *args, **kwargs): try: phone_data = UserPhoneOtp.objects.filter(phone=request.data['phone'], otp=request.data['otp']).last() if phone_data: phone_data.is_verified = True phone_data.save() return custom_response(SUCCESS_CODE['3027'], 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: 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) if user.is_superuser: serializer = SuperUserSerializer(user) return custom_response(None, 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: 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_400_BAD_REQUEST) data.update({"is_email_verified": is_verified}) 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: email_data = UserEmailOtp.objects.filter(email=request.data['email'], otp=request.data['otp']).last() if email_data: email_data.is_verified = True email_data.save() return custom_response(SUCCESS_CODE['3011'], 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: 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']): UserEmailOtp.objects.create(email=request.data['email'], otp=otp) return custom_response(None, {'email_otp': otp}, response_status=status.HTTP_200_OK) else: return custom_error_response(ERROR_CODE["2023"], response_status=status.HTTP_400_BAD_REQUEST)