# Path: server/core/serializers/user.py
# class LoggedUserSerializer(BaseUserSerializer):
#     pass
# 
# Path: server/authentication/serializers/signup.py
# class SignupSerializer(serializers.Serializer):
#     username = serializers.CharField(max_length=30, validators=[validators.UniqueValidator(queryset=User.objects.all())])
#     email = serializers.EmailField(validators=[validators.UniqueValidator(queryset=User.objects.all())])
#     password = serializers.CharField(max_length=50)
# 
#     def save(self):
#         User.objects.create_user(**self.data)
#         a_user = authenticate(**self.data)
#         login(self.context['request'], a_user)
# 
#         user = authenticate(**self.data)
#         if user is None:
#             return
#         login(self.context['request'], user)
#         assert user.is_authenticated()
#         return user


import json
from django.shortcuts import render, redirect
from rest_framework import status, views
from rest_framework.response import Response
from server.core.serializers import LoggedUserSerializer
from ..serializers import SignupSerializer




class SignupView(views.APIView):
    def post(self, request):
        if request.user.is_authenticated():
            return redirect('index')

        serializer = SignupSerializer(data=request.data, context={'request': request})
        if not serializer.is_valid():

# Path: server/core/serializers/user.py
# class LoggedUserSerializer(BaseUserSerializer):
#     pass
# 
# Path: server/authentication/serializers/signup.py
# class SignupSerializer(serializers.Serializer):
#     username = serializers.CharField(max_length=30, validators=[validators.UniqueValidator(queryset=User.objects.all())])
#     email = serializers.EmailField(validators=[validators.UniqueValidator(queryset=User.objects.all())])
#     password = serializers.CharField(max_length=50)
# 
#     def save(self):
#         User.objects.create_user(**self.data)
#         a_user = authenticate(**self.data)
#         login(self.context['request'], a_user)
# 
#         user = authenticate(**self.data)
#         if user is None:
#             return
#         login(self.context['request'], user)
#         assert user.is_authenticated()
#         return user


# Path: server/authentication/views/signup.py
import json
from django.shortcuts import render, redirect
from rest_framework import status, views
from rest_framework.response import Response
from server.core.serializers import LoggedUserSerializer
from ..serializers import SignupSerializer





class SignupView(views.APIView):
    def post(self, request):
        if request.user.is_authenticated():
            return redirect('index')

        serializer = SignupSerializer(data=request.data, context={'request': request})
        if not serializer.is_valid():

            return Response(status=status.HTTP_400_BAD_REQUEST, data=serializer.error_messages)