From 2ca623bf3360a10e9e3dfd5bce9bb815bff75465 Mon Sep 17 00:00:00 2001 From: ben8 Date: Thu, 24 Apr 2025 12:41:36 +0200 Subject: [PATCH] =?UTF-8?q?Passwort-zur=C3=BCcksetzen,=20Passwort-=C3=A4nd?= =?UTF-8?q?ern,=20Account-l=C3=B6schen!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- django/accounts/forms.py | 7 +++- django/accounts/urls.py | 23 +++++++++- django/accounts/views.py | 37 +++++++++++++++- django/core/settings.py | 1 + django/templates/accounts/home.html | 4 ++ django/templates/accounts/login.html | 1 + django/templates/accounts/register.html | 5 +++ .../registration/delete_account_confirm.html | 35 ++++++++++++++++ .../registration/password_change_form.html | 42 +++++++++++++++++++ .../registration/password_reset_complete.html | 19 +++++++++ .../registration/password_reset_confirm.html | 37 ++++++++++++++++ .../registration/password_reset_done.html | 18 ++++++++ .../registration/password_reset_email.html | 11 +++++ .../registration/password_reset_form.html | 35 ++++++++++++++++ 14 files changed, 269 insertions(+), 6 deletions(-) create mode 100644 django/templates/registration/delete_account_confirm.html create mode 100644 django/templates/registration/password_change_form.html create mode 100644 django/templates/registration/password_reset_complete.html create mode 100644 django/templates/registration/password_reset_confirm.html create mode 100644 django/templates/registration/password_reset_done.html create mode 100644 django/templates/registration/password_reset_email.html create mode 100644 django/templates/registration/password_reset_form.html diff --git a/django/accounts/forms.py b/django/accounts/forms.py index 6da316b..4aec893 100644 --- a/django/accounts/forms.py +++ b/django/accounts/forms.py @@ -6,11 +6,15 @@ from django import forms class RegisterForm(UserCreationForm): class Meta: model=User - fields = ['username','password1','password2'] + fields = ['username','password1','password2', "email"] username = forms.CharField( widget=forms.TextInput(attrs={'placeholder': 'BENUTZERNAME'}) ) + email = forms.EmailField( + widget=forms.TextInput(attrs={'placeholder': 'E-MAIL'}) + ) + password1 = forms.CharField( widget=forms.PasswordInput(attrs={'placeholder': 'PASSWORT'}) ) @@ -19,4 +23,3 @@ class RegisterForm(UserCreationForm): ) - diff --git a/django/accounts/urls.py b/django/accounts/urls.py index 61cfe31..efdc865 100644 --- a/django/accounts/urls.py +++ b/django/accounts/urls.py @@ -1,7 +1,8 @@ -from django.urls import path # type: ignore +from django.urls import path, reverse_lazy # type: ignore from . import views from django.contrib.auth.views import LogoutView from django.contrib.auth.views import LoginView +from django.contrib.auth import views as auth_views app_name = 'accounts' # Namensraum zum verhindern von Konflikten zwischen Apps # @@ -13,4 +14,22 @@ urlpatterns = [ path('logout/', LogoutView.as_view(next_page='accounts:login'), name='logout'), path('login/', LoginView.as_view(template_name='accounts/login.html', next_page='accounts:home'), name='login'), path('register/', views.register, name='register'), -] \ No newline at end of file + path('password_reset/', auth_views.PasswordResetView.as_view(success_url=reverse_lazy('accounts:password_reset_done')), name='password_reset'), + path('password_reset/done/', auth_views.PasswordResetDoneView.as_view(), name='password_reset_done'), + path('reset/done/', auth_views.PasswordResetCompleteView.as_view(), name='password_reset_complete'), + + + + path('password_change/', views.password_changed, name='password_change'), + path('password_change/done/', auth_views.PasswordChangeDoneView.as_view(), name='password_change_done'), + path('delete_account/', views.deleteAccount, name="delete_account"), + path( + 'reset///', + auth_views.PasswordResetConfirmView.as_view( + template_name='registration/password_reset_confirm.html', + success_url=reverse_lazy('accounts:password_reset_complete') + ), + name='password_reset_confirm' +), + +] diff --git a/django/accounts/views.py b/django/accounts/views.py index c2d2a73..46afbcf 100644 --- a/django/accounts/views.py +++ b/django/accounts/views.py @@ -2,7 +2,10 @@ from django.shortcuts import render, redirect from django.contrib.auth.decorators import login_required from django.contrib.auth import login, authenticate from .forms import RegisterForm - +from django.contrib.auth.forms import PasswordChangeForm +from django.contrib.auth import update_session_auth_hash +from django.contrib import messages +from django.contrib.auth import logout # Create your views here. @login_required def home(request): @@ -18,4 +21,34 @@ def register(response): else: form = RegisterForm() - return render(response, "accounts/register.html", {"form":form}) \ No newline at end of file + return render(response, "accounts/register.html", {"form":form}) + +from django.contrib.auth import views as auth_views + +@login_required +def password_changed(request): + if request.method == 'POST': + form = PasswordChangeForm(user=request.user, data=request.POST) + if form.is_valid(): + #user = form.save() + #update_session_auth_hash(request, user) #Hier wäre der User nach Passwortänderung noch angemeldet! + logout(request) + messages.success(request, "Das Passwort wurde erfolgreich geändert!") + return redirect('accounts:home') + + else: + form = PasswordChangeForm(user=request.user) + + return render(request, 'registration/password_change_form.html', {'form': form}) + + +@login_required +def deleteAccount(request): + if request.method == 'POST': + user=request.user + logout(request) + user.delete() + messages.success(request, "Der Account wurde erfolgreich gelöscht!") + return redirect('homepage:home') + return render(request, 'registration/delete_account_confirm.html') + diff --git a/django/core/settings.py b/django/core/settings.py index e2c442d..8db3ef9 100644 --- a/django/core/settings.py +++ b/django/core/settings.py @@ -16,6 +16,7 @@ from pathlib import Path # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent +EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' #TODO: NUR FÜR ENTWICKLUNG # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/5.1/howto/deployment/checklist/ diff --git a/django/templates/accounts/home.html b/django/templates/accounts/home.html index f1a5421..8a4ebe8 100644 --- a/django/templates/accounts/home.html +++ b/django/templates/accounts/home.html @@ -9,11 +9,15 @@

Willkommen, {{ request.user.username }}!

+
{% csrf_token %}
+ + +
qivip diff --git a/django/templates/accounts/login.html b/django/templates/accounts/login.html index 76c0225..3ec348b 100644 --- a/django/templates/accounts/login.html +++ b/django/templates/accounts/login.html @@ -27,6 +27,7 @@ +
{% endblock %} \ No newline at end of file diff --git a/django/templates/accounts/register.html b/django/templates/accounts/register.html index 57a8f4c..b3d2ea6 100644 --- a/django/templates/accounts/register.html +++ b/django/templates/accounts/register.html @@ -37,6 +37,11 @@ {{ form.password2 }} +
+ + {{ form.email }} + +
diff --git a/django/templates/registration/delete_account_confirm.html b/django/templates/registration/delete_account_confirm.html new file mode 100644 index 0000000..e5ce50f --- /dev/null +++ b/django/templates/registration/delete_account_confirm.html @@ -0,0 +1,35 @@ +{% extends 'base.html' %} + +{% block content %} +
+
+
+
+ + + + +

Löschen bestätigen

+ +

+ Möchten Sie ihren Account wirklich löschen? Diese Aktion kann nicht rückgängig gemacht werden. +

+
+ +
+ {% csrf_token %} +
+ + Abbrechen + + +
+
+
+
+
+{% endblock %} \ No newline at end of file diff --git a/django/templates/registration/password_change_form.html b/django/templates/registration/password_change_form.html new file mode 100644 index 0000000..5ceda79 --- /dev/null +++ b/django/templates/registration/password_change_form.html @@ -0,0 +1,42 @@ +{% extends 'base.html' %} +{% load static %} +{% block title %}Passwort ändern{% endblock %} +{% block content %} +
+
+

Passwort ändern

+
+ {% if form.errors %} +
+
    + {% for field, errors in form.errors.items %} + {% for error in errors %} +
  • {{ error }}
  • + {% endfor %} + {% endfor %} +
+
+ {% endif %} + {% csrf_token %} +
+ + {{ form.old_password.label_tag }} + + +
+ +
+ {{ form.new_password1.label_tag }} + +
+
+ {{ form.new_password2.label_tag }} + +
+ + +
+ +
+
+{% endblock %} \ No newline at end of file diff --git a/django/templates/registration/password_reset_complete.html b/django/templates/registration/password_reset_complete.html new file mode 100644 index 0000000..3825fbf --- /dev/null +++ b/django/templates/registration/password_reset_complete.html @@ -0,0 +1,19 @@ +{% extends 'base.html' %} +{% load static %} +{% block title %}Passwort zurückgesetzt{% endblock %} +{% block content %} +
+
+

Passwort zurückgesetzt

+
+ Das Passwort wurde erfolgreich zurückgesetzt! Sie können sich nun mit dem neuen Passwort anmelden! + {% csrf_token %} +
+ +
+ +
+ +
+
+{% endblock %} \ No newline at end of file diff --git a/django/templates/registration/password_reset_confirm.html b/django/templates/registration/password_reset_confirm.html new file mode 100644 index 0000000..8f383ce --- /dev/null +++ b/django/templates/registration/password_reset_confirm.html @@ -0,0 +1,37 @@ +{% extends 'base.html' %} +{% load static %} +{% block title %}Passwort zurücksetzen{% endblock %} +{% block content %} +
+
+

Passwort zurücksetzen

+
+ {% if form.errors %} +
+
    + {% for field, errors in form.errors.items %} + {% for error in errors %} +
  • {{ error }}
  • + {% endfor %} + {% endfor %} +
+
+ {% endif %} + {% csrf_token %} + +
+ {{ form.new_password1.label_tag }} + +
+
+ {{ form.new_password2.label_tag }} + + +
+ + +
+ +
+
+{% endblock %} \ No newline at end of file diff --git a/django/templates/registration/password_reset_done.html b/django/templates/registration/password_reset_done.html new file mode 100644 index 0000000..406ce74 --- /dev/null +++ b/django/templates/registration/password_reset_done.html @@ -0,0 +1,18 @@ +{% extends 'base.html' %} +{% load static %} +{% block title %}E-Mail gesendet{% endblock %} + +{% block content %} +
+
+

E-Mail wurde gesendet

+

+ Wenn ein Konto mit dieser E-Mail-Adresse existiert, haben wir dir eine Nachricht mit einem Link zum Zurücksetzen des Passworts gesendet. + +

  Tipp: Gegebenenfalls Spam überprüfen.

+ + + Zur Anmeldung +
+
+{% endblock %} diff --git a/django/templates/registration/password_reset_email.html b/django/templates/registration/password_reset_email.html new file mode 100644 index 0000000..df397fb --- /dev/null +++ b/django/templates/registration/password_reset_email.html @@ -0,0 +1,11 @@ +{% load i18n %} +{% autoescape off %} +{% blocktranslate %}Du erhältst diese E-Mail, weil du einen Passwort-Zurücksetzungsversuch bei {{ site_name }} angefragt hast.{% endblocktranslate %} + +{% translate "Bitte gehe zu folgender Seite und wähle ein neues Passwort:" %} +{{ protocol }}://{{ domain }}{% url 'accounts:password_reset_confirm' uidb64=uid token=token %} + +{% translate "Dein Benutzername ist:" %} {{ user.get_username }} + +{% blocktranslate %}Danke, dass du {{ site_name }} benutzt!{% endblocktranslate %} +{% endautoescape %} diff --git a/django/templates/registration/password_reset_form.html b/django/templates/registration/password_reset_form.html new file mode 100644 index 0000000..10ea009 --- /dev/null +++ b/django/templates/registration/password_reset_form.html @@ -0,0 +1,35 @@ +{% extends 'base.html' %} +{% load static %} +{% block title %}Passwort ändern{% endblock %} +{% block content %} +
+
+

Passwort zurücksetzen

+
+ Gib hier deine E-Mail an und erhalte eine E-Mail, um das Passwort zurückzusetzen. + {% if form.errors %} +
+
    + {% for field, errors in form.errors.items %} + {% for error in errors %} +
  • {{ error }}
  • + {% endfor %} + {% endfor %} +
+
+ {% endif %} + {% csrf_token %} + + +
+ {{ form.email.label_tag }} + +
+ + + +
+ +
+
+{% endblock %} \ No newline at end of file