ft_transcendence/django/trancendence/accounts/views/register.py

29 lines
1018 B
Python
Raw Normal View History

from django.shortcuts import render
from django.views import View
2023-10-25 10:10:23 -04:00
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 *
2023-10-25 10:10:23 -04:00
from ..forms.register import RegisterForm
2023-10-25 10:10:23 -04:00
class RegisterView(View):
def get(self, request: HttpRequest):
return render(request, "register.html", {"form": RegisterForm})
2023-10-25 10:10:23 -04:00
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.objects.create_user(username, password=password)
user.save()
return HttpResponse(USER_ADDED)