change loginPage to LoginView

This commit is contained in:
2023-01-06 13:20:35 +00:00
parent b48a817e8c
commit 7fb89158a9
7 changed files with 57 additions and 53 deletions

View File

@@ -1,6 +1,28 @@
from django import forms
from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth.models import User
from .authentications import authentication
class LoginForm(forms.Form):
username = forms.CharField()
password = forms.CharField(label="Passwort", widget=forms.PasswordInput())
class LoginForm(AuthenticationForm):
def clean(self):
username = self.cleaned_data.get("username")
password = self.cleaned_data.get("password")
if username is not None and password:
auth_user = authentication(username, password)
if auth_user:
try:
self.user_cache = User.objects.get(username=auth_user.lower())
except User.DoesNotExist:
self.user_cache = User.objects.create_user(auth_user.lower())
else:
raise self.get_invalid_login_error()
if self.user_cache is None:
raise self.get_invalid_login_error()
else:
self.confirm_login_allowed(self.user_cache)
return self.cleaned_data

View File

@@ -6,6 +6,6 @@ from . import views
app_name = apps.AuthenticationsConfig.name
urlpatterns = [
path("login/", views.loginPage, name="login"),
path("login/", views.AuthLoginView.as_view(), name="login"),
path("logout/", views.logoutUser, name="logout"),
]

View File

@@ -1,46 +1,17 @@
from django.contrib import messages
from django.contrib.auth import login, logout
from django.contrib.auth.models import User
from django.shortcuts import render, redirect
from django.contrib.auth import logout
from django.contrib.auth.views import LoginView
from django.shortcuts import redirect
from documents.etherpadlib import del_ep_cookie
from .authentications import authentication
from .decorators import unauthenticated_user, authenticated_user
from .decorators import authenticated_user
from .forms import LoginForm
@unauthenticated_user
def loginPage(request):
if request.method == "POST":
username = request.POST.get("username").lower()
password = request.POST.get("password")
auth_user = authentication(username, password)
if auth_user:
try:
user = User.objects.get(username=auth_user.lower())
except User.DoesNotExist:
user = User.objects.create_user(auth_user.lower())
login(request, user)
try:
return redirect(request.GET.get("next"))
except:
return redirect("home")
else:
messages.error(
request,
"Anmeldung nicht erfolgreich. Bitte überprüfe Benutzername und Passwort.",
)
form = LoginForm()
context = {
"form": form,
}
return render(request, "authentications/login.html", context)
class AuthLoginView(LoginView):
authentication_form = LoginForm
redirect_authenticated_user = True
template_name = "authentications/login.html"
@authenticated_user

View File

@@ -66,6 +66,7 @@ AUTHENTICATION_BACKENDS = [
"django.contrib.auth.backends.ModelBackend",
]
LOGIN_REDIRECT_URL = 'home'
LOGIN_URL = "/auth/login"

View File

@@ -7,9 +7,11 @@
<main class="container mx-auto w-full px-4 my-8 flex-grow flex flex-col">
<h1 class="page-title">Anmeldung für FET-Mitarbeiter</h1>
<div class="w-full h-full flex-1 flex justify-center items-center">
<form action="" method="POST" class="sm:p-4 sm:w-3/5 md:w-1/2 lg:w-2/5 xl:w-1/3 2xl:w-1/4 grid grid-cols-1 gap-3 sm:gap-6">
<form action="{% url 'authentications:login' %}" method="POST" class="sm:p-4 sm:w-3/5 md:w-1/2 lg:w-2/5 xl:w-1/3 2xl:w-1/4 grid grid-cols-1 gap-3 sm:gap-6">
{% csrf_token %}
{% include "baseform/non_field_errors.html" %}
{% for message in messages %}
<div class="alert alert-danger">
<i class="alert-icon fa-solid fa-check-circle"></i>
@@ -18,15 +20,11 @@
</div>
{% endfor %}
<label class="block">
<span class="text-gray-700 dark:text-gray-200">Benutzername</span>
<input type="text" name="username" class="mt-1 block w-full rounded-md border-gray-300 dark:border-none shadow-sm focus:border-none focus:ring focus:ring-blue-200 dark:focus:ring-sky-700 focus:ring-opacity-50" required="required">
</label>
<label class="block">
<span class="text-gray-700 dark:text-gray-200">Passwort</span>
<input type="password" name="password" class="mt-1 block w-full rounded-md border-gray-300 dark:border-none shadow-sm focus:border-none focus:ring focus:ring-blue-200 dark:focus:ring-sky-700 focus:ring-opacity-50" required="required">
</label>
{% include "baseform/text.html" with field=form.username %}
{% include "baseform/password.html" with field=form.password %}
<input type="submit" class="block btn btn-primary" value="Anmelden">
<input type="hidden" name="next" value="{{ next }}">
</form>
</div>
</main>

View File

@@ -0,0 +1,12 @@
<label class="block">
<span class="text-gray-700 dark:text-gray-200">{{ field.label }}</span>
{% if field.errors %}
<div class="alert alert-danger">
<div class="alert-body">{{ field.errors }}</div>
</div>
{% endif %}
<input type="password" id="id_{{ field.name }}" name="{{ field.name }}" {% if field.required %}required{% endif %} class="mt-1 block w-full rounded-md border-gray-300 dark:border-none shadow-sm focus:border-none focus:ring focus:ring-blue-200 dark:focus:ring-sky-700 focus:ring-opacity-50">
{% if field.help_text %}
<span class="text-gray-700 dark:text-gray-200">{{ field.help_text }}</span>
{% endif %}
</label>

View File

@@ -5,7 +5,7 @@
<div class="alert-body">{{ field.errors }}</div>
</div>
{% endif %}
<input type="text" id="id_{{ field.name }}" name="{{ field.name }}" value="{{ field.value }}" {% if field.required %}required{% endif %} class="mt-1 block w-full rounded-md border-gray-300 dark:border-none shadow-sm focus:border-none focus:ring focus:ring-blue-200 dark:focus:ring-sky-700 focus:ring-opacity-50">
<input type="text" id="id_{{ field.name }}" name="{{ field.name }}" {% if field.value %}value="{{ field.value }}"{% endif %} {% if field.required %}required{% endif %} class="mt-1 block w-full rounded-md border-gray-300 dark:border-none shadow-sm focus:border-none focus:ring focus:ring-blue-200 dark:focus:ring-sky-700 focus:ring-opacity-50">
{% if field.help_text %}
<span class="text-gray-700 dark:text-gray-200">{{ field.help_text }}</span>
{% endif %}