2023-12-13 02:11:29 +01:00
|
|
|
from django.shortcuts import render, redirect
|
|
|
|
from django.contrib.auth.forms import AuthenticationForm, UserCreationForm
|
|
|
|
from django.contrib.auth import authenticate, login, logout
|
2024-01-06 14:14:15 +01:00
|
|
|
from django.urls import reverse_lazy
|
2023-12-13 02:11:29 +01:00
|
|
|
from django.views import View
|
|
|
|
|
2024-01-06 14:14:15 +01:00
|
|
|
|
2023-12-13 02:11:29 +01:00
|
|
|
class LoginView(View):
|
|
|
|
form_class = AuthenticationForm
|
|
|
|
template_name = "login_signup.html"
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
form = self.form_class()
|
|
|
|
return render(request, self.template_name, {"form": form})
|
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
2024-01-06 14:14:15 +01:00
|
|
|
redirection_path = request.GET.get("next")
|
2023-12-13 02:11:29 +01:00
|
|
|
form = self.form_class(data=request.POST)
|
|
|
|
if form.is_valid():
|
|
|
|
user = authenticate(
|
|
|
|
username=form.cleaned_data["username"],
|
|
|
|
password=form.cleaned_data["password"],
|
|
|
|
)
|
|
|
|
if user is not None:
|
|
|
|
login(request, user)
|
2024-01-06 14:14:15 +01:00
|
|
|
if redirection_path is not None:
|
|
|
|
return redirect(redirection_path)
|
|
|
|
else:
|
|
|
|
return redirect("detect/")
|
2023-12-13 02:11:29 +01:00
|
|
|
return render(request, self.template_name, {"form": form})
|
|
|
|
|
|
|
|
|
|
|
|
class SignupView(View):
|
|
|
|
form_class = UserCreationForm
|
|
|
|
template_name = "login_signup.html"
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
form = self.form_class()
|
|
|
|
return render(request, self.template_name, {"form": form, "signup": True})
|
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
form = self.form_class(data=request.POST)
|
|
|
|
if form.is_valid():
|
|
|
|
user = form.save()
|
|
|
|
login(request, user)
|
2024-01-06 14:14:15 +01:00
|
|
|
return redirect("detect/")
|
2023-12-13 02:11:29 +01:00
|
|
|
return render(request, self.template_name, {"form": form, "signup": True})
|
|
|
|
|
|
|
|
|
|
|
|
def logout_view(request):
|
2024-01-06 14:14:15 +01:00
|
|
|
redirection_path = request.GET.get("next")
|
2023-12-13 02:11:29 +01:00
|
|
|
logout(request)
|
2024-01-06 14:14:15 +01:00
|
|
|
if redirection_path is not None:
|
|
|
|
return redirect(redirection_path)
|
|
|
|
else:
|
|
|
|
return redirect("detect/")
|