ft_transcendence/accounts/views/register.py

18 lines
683 B
Python
Raw Normal View History

2023-11-11 13:50:14 -05:00
from rest_framework import permissions, status
from ..serializers.register import RegisterSerialiser
from rest_framework.views import APIView
from rest_framework.response import Response
from django.http import HttpRequest
2023-12-04 06:32:15 -05:00
from django.contrib.auth import login
2023-11-11 13:50:14 -05:00
class RegisterView(APIView):
permission_classes = (permissions.AllowAny,)
def post(self, request: HttpRequest):
2023-11-13 07:22:10 -05:00
data = request.data
serializer = RegisterSerialiser(data=data)
2023-11-11 13:50:14 -05:00
if serializer.is_valid(raise_exception=True):
2023-11-13 07:22:10 -05:00
user = serializer.create(data)
2023-11-11 13:50:14 -05:00
if user:
2023-12-04 06:32:15 -05:00
login(request, user)
2023-11-11 13:50:14 -05:00
return Response("user created", status=status.HTTP_201_CREATED)
return Response(status=status.HTTP_400_BAD_REQUEST)