From 7fb89158a9f4bc295f9c44e16aa7a5ce1891e115 Mon Sep 17 00:00:00 2001 From: Patrick Mayr Date: Fri, 6 Jan 2023 13:20:35 +0000 Subject: [PATCH] change loginPage to LoginView --- fet2020/authentications/forms.py | 30 +++++++++++-- fet2020/authentications/urls.py | 2 +- fet2020/authentications/views.py | 47 ++++---------------- fet2020/fet2020/settings.py | 1 + fet2020/templates/authentications/login.html | 16 +++---- fet2020/templates/baseform/password.html | 12 +++++ fet2020/templates/baseform/text.html | 2 +- 7 files changed, 57 insertions(+), 53 deletions(-) create mode 100644 fet2020/templates/baseform/password.html diff --git a/fet2020/authentications/forms.py b/fet2020/authentications/forms.py index 40a53f7c..2e87e7c4 100644 --- a/fet2020/authentications/forms.py +++ b/fet2020/authentications/forms.py @@ -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 diff --git a/fet2020/authentications/urls.py b/fet2020/authentications/urls.py index f4c51d8b..723dd2d8 100644 --- a/fet2020/authentications/urls.py +++ b/fet2020/authentications/urls.py @@ -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"), ] diff --git a/fet2020/authentications/views.py b/fet2020/authentications/views.py index 31c7f5bf..719e1c1a 100644 --- a/fet2020/authentications/views.py +++ b/fet2020/authentications/views.py @@ -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 diff --git a/fet2020/fet2020/settings.py b/fet2020/fet2020/settings.py index c59f244d..215e8e14 100644 --- a/fet2020/fet2020/settings.py +++ b/fet2020/fet2020/settings.py @@ -66,6 +66,7 @@ AUTHENTICATION_BACKENDS = [ "django.contrib.auth.backends.ModelBackend", ] +LOGIN_REDIRECT_URL = 'home' LOGIN_URL = "/auth/login" diff --git a/fet2020/templates/authentications/login.html b/fet2020/templates/authentications/login.html index 25e99264..1a13f436 100644 --- a/fet2020/templates/authentications/login.html +++ b/fet2020/templates/authentications/login.html @@ -7,9 +7,11 @@

Anmeldung für FET-Mitarbeiter

-
+ {% csrf_token %} + {% include "baseform/non_field_errors.html" %} + {% for message in messages %}
@@ -18,15 +20,11 @@
{% endfor %} - - + {% include "baseform/text.html" with field=form.username %} + {% include "baseform/password.html" with field=form.password %} + +
diff --git a/fet2020/templates/baseform/password.html b/fet2020/templates/baseform/password.html new file mode 100644 index 00000000..729a7aa6 --- /dev/null +++ b/fet2020/templates/baseform/password.html @@ -0,0 +1,12 @@ + diff --git a/fet2020/templates/baseform/text.html b/fet2020/templates/baseform/text.html index cbfac2ef..37842bc8 100644 --- a/fet2020/templates/baseform/text.html +++ b/fet2020/templates/baseform/text.html @@ -5,7 +5,7 @@
{{ field.errors }}
{% endif %} - + {% if field.help_text %} {{ field.help_text }} {% endif %}