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 * from ..forms.register import RegisterForm class RegisterView(View): def get(self, request: HttpRequest): if request.user.is_authenticated: logout(request) return render(request, 'register.html', {'form': RegisterForm}) def post(self, request: HttpRequest): if request.user.is_authenticated: logout(request) 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)