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): class LoginForm(AuthenticationForm):
username = forms.CharField() def clean(self):
password = forms.CharField(label="Passwort", widget=forms.PasswordInput()) 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 app_name = apps.AuthenticationsConfig.name
urlpatterns = [ urlpatterns = [
path("login/", views.loginPage, name="login"), path("login/", views.AuthLoginView.as_view(), name="login"),
path("logout/", views.logoutUser, name="logout"), path("logout/", views.logoutUser, name="logout"),
] ]

View File

@@ -1,46 +1,17 @@
from django.contrib import messages from django.contrib.auth import logout
from django.contrib.auth import login, logout from django.contrib.auth.views import LoginView
from django.contrib.auth.models import User from django.shortcuts import redirect
from django.shortcuts import render, redirect
from documents.etherpadlib import del_ep_cookie 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 from .forms import LoginForm
@unauthenticated_user class AuthLoginView(LoginView):
def loginPage(request): authentication_form = LoginForm
if request.method == "POST": redirect_authenticated_user = True
username = request.POST.get("username").lower() template_name = "authentications/login.html"
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)
@authenticated_user @authenticated_user

View File

@@ -66,6 +66,7 @@ AUTHENTICATION_BACKENDS = [
"django.contrib.auth.backends.ModelBackend", "django.contrib.auth.backends.ModelBackend",
] ]
LOGIN_REDIRECT_URL = 'home'
LOGIN_URL = "/auth/login" 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"> <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> <h1 class="page-title">Anmeldung für FET-Mitarbeiter</h1>
<div class="w-full h-full flex-1 flex justify-center items-center"> <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 %} {% csrf_token %}
{% include "baseform/non_field_errors.html" %}
{% for message in messages %} {% for message in messages %}
<div class="alert alert-danger"> <div class="alert alert-danger">
<i class="alert-icon fa-solid fa-check-circle"></i> <i class="alert-icon fa-solid fa-check-circle"></i>
@@ -18,15 +20,11 @@
</div> </div>
{% endfor %} {% endfor %}
<label class="block"> {% include "baseform/text.html" with field=form.username %}
<span class="text-gray-700 dark:text-gray-200">Benutzername</span> {% include "baseform/password.html" with field=form.password %}
<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>
<input type="submit" class="block btn btn-primary" value="Anmelden"> <input type="submit" class="block btn btn-primary" value="Anmelden">
<input type="hidden" name="next" value="{{ next }}">
</form> </form>
</div> </div>
</main> </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 class="alert-body">{{ field.errors }}</div>
</div> </div>
{% endif %} {% 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 %} {% if field.help_text %}
<span class="text-gray-700 dark:text-gray-200">{{ field.help_text }}</span> <span class="text-gray-700 dark:text-gray-200">{{ field.help_text }}</span>
{% endif %} {% endif %}