ft_transcendence/accounts/views/register.py

32 lines
1.1 KiB
Python
Raw Normal View History

from django.shortcuts import render
from django.views import View
from django.http import HttpResponse, HttpRequest, JsonResponse
from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login, logout
from django.db.models.query import QuerySet
from django.contrib.auth.decorators import user_passes_test
from ..status_code import *
2023-10-25 10:10:23 -04:00
from ..forms.register import RegisterForm
2023-10-25 10:10:23 -04:00
class RegisterView(View):
2023-10-25 10:10:23 -04:00
def get(self, request: HttpRequest):
if request.user.is_authenticated:
logout(request)
return render(request, 'register.html', {'form': RegisterForm})
2023-10-25 10:10:23 -04:00
def post(self, request: HttpRequest):
if request.user.is_authenticated:
logout(request)
2023-10-25 10:10:23 -04:00
form: RegisterForm = RegisterForm(request.POST)
if not form.is_valid():
return JsonResponse(form.errors)
user: User = User.objects.create_user(username=form.cleaned_data['username'], password=form.cleaned_data['password'])
login(request, user)
return HttpResponse(USER_ADDED)