1
0
Fork 0
mirror of https://github.com/seanmorley15/AdventureLog.git synced 2025-07-20 13:29:37 +02:00
AdventureLog/backend/server/users/views.py

44 lines
1.8 KiB
Python
Raw Normal View History

2024-07-08 11:44:39 -04:00
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from rest_framework.permissions import IsAuthenticated
from .serializers import ChangeEmailSerializer
from drf_yasg.utils import swagger_auto_schema
from drf_yasg import openapi
2024-08-16 12:21:43 -04:00
from django.conf import settings
2024-07-08 11:44:39 -04:00
class ChangeEmailView(APIView):
permission_classes = [IsAuthenticated]
@swagger_auto_schema(
request_body=ChangeEmailSerializer,
responses={
200: openapi.Response('Email successfully changed'),
400: 'Bad Request'
},
operation_description="Change the email address for the authenticated user."
)
def post(self, request):
serializer = ChangeEmailSerializer(data=request.data, context={'request': request})
if serializer.is_valid():
user = request.user
new_email = serializer.validated_data['new_email']
user.email = new_email
2024-08-16 11:47:53 -04:00
# remove all other email addresses for the user
user.emailaddress_set.exclude(email=new_email).delete()
user.emailaddress_set.create(email=new_email, primary=True, verified=False)
2024-07-08 11:44:39 -04:00
user.save()
return Response({"detail": "Email successfully changed."}, status=status.HTTP_200_OK)
2024-08-16 12:21:43 -04:00
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
class IsRegistrationDisabled(APIView):
@swagger_auto_schema(
responses={
200: openapi.Response('Registration is disabled'),
400: 'Bad Request'
},
operation_description="Check if registration is disabled."
)
def get(self, request):
return Response({"is_disabled": settings.DISABLE_REGISTRATION}, status=status.HTTP_200_OK)