ft_transcendence/accounts/views/register.py

29 lines
1.0 KiB
Python

from django.shortcuts import render
from django.views import View
from django.http import HttpResponse, HttpRequest
from django.contrib.auth.models import User
from django.db.models.query import QuerySet
from ..status_code import *
from ..settings import *
from ..forms.register import RegisterForm
class RegisterView(View):
def get(self, request: HttpRequest):
return render(request, "register.html", {"form": RegisterForm})
def post(self, request: HttpRequest):
form: RegisterForm = RegisterForm(request.POST)
if not form.is_valid():
return HttpResponse(INVALID_USERNAME_PASSWORD)
username: str = form.cleaned_data["username"]
password: str = form.cleaned_data["password"]
if User.objects.filter(username=username).exists():
return HttpResponse(USERNAME_ALREADY_USED)
user: User = User.objects.create_user(username, password=password)
user.save()
return HttpResponse(USER_ADDED)