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