1
0
Fork 0
mirror of https://github.com/seanmorley15/AdventureLog.git synced 2025-07-19 04:49:37 +02:00
AdventureLog/backend/server/users/views.py
2024-07-08 11:44:39 -04:00

28 lines
No EOL
1.1 KiB
Python

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
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
user.save()
return Response({"detail": "Email successfully changed."}, status=status.HTTP_200_OK)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)