From 939bb82df4a993502d39bfb852b348547746cafa Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Fri, 7 Jul 2023 17:45:51 +0200 Subject: ✨ enable password recovering by email MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../registration/password_reset_confirm.html | 49 ++++++++++++++++------ .../registration/password_reset_email.html | 2 +- ishtar_common/urls_registration.py | 6 +-- ishtar_common/views.py | 42 +++++++++++++++---- 4 files changed, 71 insertions(+), 28 deletions(-) (limited to 'ishtar_common') diff --git a/ishtar_common/templates/registration/password_reset_confirm.html b/ishtar_common/templates/registration/password_reset_confirm.html index b0e2cc142..6129d259b 100644 --- a/ishtar_common/templates/registration/password_reset_confirm.html +++ b/ishtar_common/templates/registration/password_reset_confirm.html @@ -3,19 +3,42 @@ {% block content %} -{% if validlink %} -
-
{% csrf_token %} - - {{ form.as_table }} - -
-
-
-{% else %} -
-

{% trans "Password reset failed" %}

+
+
+
+

{{page_title}}

+ {% if validlink %} +
{% csrf_token %} + {% if form.non_field_errors %} +
+ {% for error in form.non_field_errors %} +
+ {{error}} +
+ {% endfor %} +
+ {% endif %} + {% with force_large_col=True %} + {% for field in form %} + {% include "blocks/bs_field_snippet.html" %} + {% endfor %} + {% endwith %} + +
+
+ +
+
+
+ {% else %} +
+

{% trans "Password reset failed" %}

+
+ {% endif %} +
+
-{% endif %} + + {% endblock %} diff --git a/ishtar_common/templates/registration/password_reset_email.html b/ishtar_common/templates/registration/password_reset_email.html index 05612cf58..ad81d141b 100644 --- a/ishtar_common/templates/registration/password_reset_email.html +++ b/ishtar_common/templates/registration/password_reset_email.html @@ -1,5 +1,5 @@ {% load i18n %} {% blocktrans %}Reset password at {{ site_name }}{% endblocktrans %}: {% block reset_link %} -{{ protocol }}://{{ domain }}{% url 'password_reset_confirm' uidb36=uid, token=token %} +{{ protocol }}://{{ domain }}{% url 'password_reset_confirm' uidb64=uid token=token %} {% endblock %} diff --git a/ishtar_common/urls_registration.py b/ishtar_common/urls_registration.py index fc8d2c53c..556bf6f95 100644 --- a/ishtar_common/urls_registration.py +++ b/ishtar_common/urls_registration.py @@ -42,11 +42,7 @@ urlpatterns = [ path('accounts/logout/', views.LogoutView.as_view(), name='logout'), path('accounts/password_change/', views.PasswordChangeView.as_view(), name='password_change'), - path('accounts/password_reset/', auth_views.PasswordResetView.as_view(), name='password_reset'), - path('accounts/password_reset/done/', auth_views.PasswordResetDoneView.as_view(), - name='password_reset_done'), + path('accounts/password_reset/', views.PasswordResetView.as_view(), name='password_reset'), path('accounts/reset///', views.PasswordResetConfirmView.as_view(), name='password_reset_confirm'), - path('accounts/reset/done/', auth_views.PasswordResetCompleteView.as_view(), - name='password_reset_complete'), ] \ No newline at end of file diff --git a/ishtar_common/views.py b/ishtar_common/views.py index 38fd80689..2a60bfd6a 100644 --- a/ishtar_common/views.py +++ b/ishtar_common/views.py @@ -38,10 +38,7 @@ from django.conf import settings from django.contrib import messages from django.contrib.auth import logout from django.contrib.auth.decorators import login_required -from django.contrib.auth.views import redirect_to_login, LoginView as AuthLoginView, \ - PasswordChangeView as AuthPasswordChangeView, \ - PasswordResetConfirmView as AuthPasswordResetConfirmView, \ - LogoutView as AuthLogoutView +from django.contrib.auth import views as auth_view from django.contrib.contenttypes.models import ContentType from django.core.exceptions import ObjectDoesNotExist from django.core.cache import cache @@ -179,7 +176,7 @@ def index(request): return render(request, "index.html", dct) -class LoginView(AuthLoginView): +class LoginView(auth_view.LoginView): form_class = forms.AuthenticationForm def get_context_data(self, **kwargs): @@ -188,7 +185,7 @@ class LoginView(AuthLoginView): return context -class LogoutView(AuthLogoutView): +class LogoutView(auth_view.LogoutView): def get(self, request, *args, **kwargs): # clear cache keys = [] @@ -210,7 +207,7 @@ def update_password_last_update(user): cache.set(key, False, settings.CACHE_TIMEOUT) -class PasswordChangeView(AuthPasswordChangeView): +class PasswordChangeView(auth_view.PasswordChangeView): form_class = forms.PasswordChangeForm success_url = reverse_lazy('start') template_name = 'registration/form.html' @@ -227,10 +224,19 @@ class PasswordChangeView(AuthPasswordChangeView): return context -class PasswordResetConfirmView(AuthPasswordResetConfirmView): +class PasswordResetConfirmView(auth_view.PasswordResetConfirmView): + form_class = forms.SetPasswordForm + success_url = reverse_lazy('login') + + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + context["page_title"] = _("Password reset") + return context + def form_valid(self, form): returned = super().form_valid(form) update_password_last_update(form.user) + messages.add_message(self.request, messages.INFO, _("Password changed")) return returned @@ -247,6 +253,24 @@ class RegistrationView(registration_views.RegistrationView): raise NotImplementedError +class PasswordResetView(auth_view.PasswordResetView): + template_name = 'registration/form.html' + success_url = reverse_lazy('start') + + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + context["page_title"] = _("Reset password") + return context + + def form_valid(self, form): + returned = super().form_valid(form) + messages.add_message( + self.request, messages.INFO, + _("Email with password reset instructions has been sent.") + ) + return returned + + person_search_wizard = wizards.PersonSearch.as_view( [("general-person_search", forms.PersonFormSelection)], label=_("Person search"), @@ -2753,7 +2777,7 @@ class DisplayItemView(IshtarMixin, TemplateView): def dispatch(self, request, *args, **kwargs): if not self.request.user.is_authenticated: - return redirect_to_login(reverse("display-item", kwargs=kwargs)) + return auth_view.redirect_to_login(reverse("display-item", kwargs=kwargs)) return super(DisplayItemView, self).dispatch(request, *args, **kwargs) def get_context_data(self, *args, **kwargs): -- cgit v1.2.3