Spaces:
Sleeping
Sleeping
Download authService/views.py from mohitlahoti2002/djangoServer: direct link, hf CLI and curl.
- Browser
- Download file 5.81 kB
-
https://huggingface.co/spaces/mohitlahoti2002/djangoServer/resolve/main/authService/views.py
- Command line
-
hf download hf://spaces/mohitlahoti2002/djangoServer/authService/views.py
-
curl -L -o views.py https://huggingface.co/spaces/mohitlahoti2002/djangoServer/resolve/main/authService/views.py
5.81 kB
| from rest_framework.views import APIView | |
| from rest_framework.response import Response | |
| from rest_framework import status | |
| from .serializers import ( | |
| UserRegistrationSerializer, | |
| VerifyOTPSerializer, | |
| LoginSerializer, | |
| ProfileSerializer, | |
| ForgotPasswordSerializer, | |
| ResetPasswordSerializer | |
| ) | |
| from .models import User | |
| from django.contrib.auth import authenticate | |
| from rest_framework_simplejwt.tokens import RefreshToken | |
| from rest_framework.permissions import IsAuthenticated | |
| from . import constants | |
| import random | |
| from django.core.mail import send_mail | |
| from django.utils import timezone | |
| from datetime import timedelta | |
| def send_otp_email(user): | |
| otp = str(random.randint(100000, 999999)) | |
| user.otp = otp | |
| user.otp_created_at = timezone.now() | |
| user.save() | |
| send_mail( | |
| 'Your OTP for verification', | |
| f'Your OTP is: {otp}', | |
| 'from@example.com', | |
| [user.email], | |
| fail_silently=False, | |
| ) | |
| class UserRegistrationView(APIView): | |
| def post(self, request): | |
| print(request.data) | |
| serializer = UserRegistrationSerializer(data=request.data) | |
| if serializer.is_valid(): | |
| user = serializer.save() | |
| send_otp_email(user) | |
| return Response({"message": constants.SIGNUP_SUCCESS}, status=status.HTTP_201_CREATED) | |
| print(serializer.errors) | |
| return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) | |
| class VerifyOTPView(APIView): | |
| def post(self, request): | |
| serializer = VerifyOTPSerializer(data=request.data) | |
| if serializer.is_valid(): | |
| email = serializer.validated_data['email'] | |
| otp = serializer.validated_data['otp'] | |
| try: | |
| user = User.objects.get(email=email) | |
| if user.otp == otp and (timezone.now() - user.otp_created_at) < timedelta(minutes=10): | |
| user.is_verified = True | |
| user.save() | |
| return Response({"message": constants.OTP_VERIFIED}, status=status.HTTP_200_OK) | |
| return Response({"message": constants.INVALID_OTP}, status=status.HTTP_400_BAD_REQUEST) | |
| except User.DoesNotExist: | |
| return Response({"message": constants.USER_NOT_FOUND}, status=status.HTTP_404_NOT_FOUND) | |
| return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) | |
| class LoginView(APIView): | |
| def post(self, request): | |
| serializer = LoginSerializer(data=request.data) | |
| if serializer.is_valid(): | |
| email = serializer.validated_data['email'] | |
| password = serializer.validated_data['password'] | |
| user = authenticate(email=email, password=password) | |
| if user: | |
| if not user.is_verified: | |
| return Response({"message": "Please verify your OTP first."}, status=status.HTTP_400_BAD_REQUEST) | |
| refresh = RefreshToken.for_user(user) | |
| return Response({ | |
| "message": constants.LOGIN_SUCCESS, | |
| "refresh": str(refresh), | |
| "access": str(refresh.access_token), | |
| }) | |
| return Response({"message": constants.INVALID_CREDENTIALS}, status=status.HTTP_400_BAD_REQUEST) | |
| return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) | |
| class ProfileView(APIView): | |
| permission_classes = [IsAuthenticated] | |
| def get(self, request): | |
| serializer = ProfileSerializer(request.user) | |
| return Response(serializer.data, status=status.HTTP_200_OK) | |
| def put(self, request): | |
| serializer = ProfileSerializer(request.user, data=request.data, partial=True) | |
| if serializer.is_valid(): | |
| # Don't allow email or role changes | |
| if 'email' in serializer.validated_data: | |
| del serializer.validated_data['email'] | |
| if 'role' in serializer.validated_data: | |
| del serializer.validated_data['role'] | |
| serializer.save() | |
| return Response(serializer.data, status=status.HTTP_200_OK) | |
| return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) | |
| class ForgotPasswordView(APIView): | |
| def post(self, request): | |
| serializer = ForgotPasswordSerializer(data=request.data) | |
| if serializer.is_valid(): | |
| email = serializer.validated_data['email'] | |
| try: | |
| user = User.objects.get(email=email) | |
| send_otp_email(user) | |
| return Response({"message": constants.PASSWORD_RESET_EMAIL_SENT}, status=status.HTTP_200_OK) | |
| except User.DoesNotExist: | |
| return Response({"message": constants.USER_NOT_FOUND}, status=status.HTTP_404_NOT_FOUND) | |
| return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) | |
| class ResetPasswordView(APIView): | |
| def post(self, request): | |
| serializer = ResetPasswordSerializer(data=request.data) | |
| if serializer.is_valid(): | |
| email = serializer.validated_data['email'] | |
| otp = serializer.validated_data['otp'] | |
| password = serializer.validated_data['password'] | |
| try: | |
| user = User.objects.get(email=email) | |
| if user.otp == otp and (timezone.now() - user.otp_created_at) < timedelta(minutes=10): | |
| user.set_password(password) | |
| user.save() | |
| return Response({"message": constants.PASSWORD_RESET_SUCCESS}, status=status.HTTP_200_OK) | |
| return Response({"message": constants.INVALID_OTP}, status=status.HTTP_400_BAD_REQUEST) | |
| except User.DoesNotExist: | |
| return Response({"message": constants.USER_NOT_FOUND}, status=status.HTTP_404_NOT_FOUND) | |
| return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) |