From b4677ac8f5b942223766f1ee0525c96d3a5816d9 Mon Sep 17 00:00:00 2001 From: tiborhari Date: Sun, 6 Aug 2023 20:32:39 +0200 Subject: [PATCH] Fix the displayed token expiry values, and use `PASSWORD_RESET_TIMEOUT_DAYS` if it's set. #10 --- README.rst | 2 +- django_admin_reset/admin.py | 23 ++++++++++++------- .../locale/hu/LC_MESSAGES/django.po | 4 ++-- .../templates/admin/password_reset_url.html | 2 +- .../tests/password_reset_test.py | 6 ++--- tox.ini | 2 +- 6 files changed, 23 insertions(+), 16 deletions(-) diff --git a/README.rst b/README.rst index 1a59635..e5d78c6 100644 --- a/README.rst +++ b/README.rst @@ -43,7 +43,7 @@ Validation time The password reset links/tokens, generated by this package, are using the built-in Django password reset functionality, and so respect the -``PASSWORD_RESET_TIMEOUT_DAYS`` setting. +``PASSWORD_RESET_TIMEOUT`` setting. Compatibility ------------- diff --git a/django_admin_reset/admin.py b/django_admin_reset/admin.py index 3f10e3c..2fb94c7 100644 --- a/django_admin_reset/admin.py +++ b/django_admin_reset/admin.py @@ -1,4 +1,4 @@ -from django import forms +from django import forms, VERSION from django.conf import settings from django.contrib import admin from django.contrib.admin.utils import unquote @@ -23,13 +23,17 @@ UserModel = get_user_model() -def _get_password_reset_token_expiry(): - if hasattr(settings, 'PASSWORD_RESET_TIMEOUT'): +def _get_password_reset_token_expiry_seconds(): + if ( + settings.is_overridden('PASSWORD_RESET_TIMEOUT_DAYS') + and not settings.is_overridden('PASSWORD_RESET_TIMEOUT') + and VERSION < (4,) + ): + # Django 3.0- + return settings.PASSWORD_RESET_TIMEOUT_DAYS * 60*60*24 + else: # Django 3.1+ return settings.PASSWORD_RESET_TIMEOUT - else: - # Django 3.0- - return settings.PASSWORD_RESET_TIMEOUT_DAYS class UserCreationForm(forms.ModelForm): @@ -141,8 +145,11 @@ def password_reset_url(self, request, id, form_url=''): return TemplateResponse( request, 'admin/password_reset_url.html', - context={'user': user, 'url': url, 'title': _('Password reset'), - 'timeout_days': _get_password_reset_token_expiry()}) + context={ + 'user': user, 'url': url, 'title': _('Password reset'), + 'timeout_seconds': _get_password_reset_token_expiry_seconds(), + }, + ) if admin.site.is_registered(UserModel): diff --git a/django_admin_reset/locale/hu/LC_MESSAGES/django.po b/django_admin_reset/locale/hu/LC_MESSAGES/django.po index 42a2239..e5f3612 100644 --- a/django_admin_reset/locale/hu/LC_MESSAGES/django.po +++ b/django_admin_reset/locale/hu/LC_MESSAGES/django.po @@ -51,7 +51,7 @@ msgstr "" #, python-format msgid "" "This link can be sent directly to the user (e.g. by email). It's only usable " -"once, and it expires in %(timeout_days)s days." +"once, and it expires in %(timeout_seconds)s seconds." msgstr "" "Ez a link közvetlenül elküldhető a felhasználónak (pl. email-en). Csak " -"egyszer használható, és %(timeout_days)s nap múlva lejár." +"egyszer használható, és %(timeout_seconds)s másodperc múlva lejár." diff --git a/django_admin_reset/templates/admin/password_reset_url.html b/django_admin_reset/templates/admin/password_reset_url.html index d7c95c4..2760a43 100644 --- a/django_admin_reset/templates/admin/password_reset_url.html +++ b/django_admin_reset/templates/admin/password_reset_url.html @@ -12,6 +12,6 @@ {% blocktrans trimmed with username=user.username %} This link can be sent directly to the user (e.g. by email). It's only usable once, and it expires in - {{ timeout_days }} days. + {{ timeout_seconds }} seconds. {% endblocktrans %} {% endblock %} diff --git a/django_admin_reset/tests/password_reset_test.py b/django_admin_reset/tests/password_reset_test.py index c3bbe8c..f76345e 100644 --- a/django_admin_reset/tests/password_reset_test.py +++ b/django_admin_reset/tests/password_reset_test.py @@ -14,7 +14,7 @@ from django.utils.http import urlsafe_base64_encode from pytest import fixture, mark -from django_admin_reset.admin import _get_password_reset_token_expiry +from django_admin_reset.admin import _get_password_reset_token_expiry_seconds pytestmark = [mark.django_db] @@ -250,13 +250,13 @@ def test_expired_token(user_idx, logout, if django.VERSION < (3, 1): future_date = date.today() + timedelta( - days=_get_password_reset_token_expiry() + 1) + days=_get_password_reset_token_expiry_seconds() + 60) with patch('django.contrib.auth.tokens.PasswordResetTokenGenerator.' '_today', return_value=future_date): assert_invalid_url(client, url, token, [user.pk]) else: future_date = datetime.now() + timedelta( - days=_get_password_reset_token_expiry() + 1) + days=_get_password_reset_token_expiry_seconds() + 60) with patch('django.contrib.auth.tokens.PasswordResetTokenGenerator.' '_now', return_value=future_date): assert_invalid_url(client, url, token, [user.pk]) diff --git a/tox.ini b/tox.ini index ed8ea0f..78fe870 100644 --- a/tox.ini +++ b/tox.ini @@ -28,7 +28,7 @@ basepython = python3.11 commands = pip install -e .[babel] pybabel compile --domain django --directory django_admin_reset/locale - python setup.py bdist_wheel --universal + python setup.py bdist_wheel [gh-actions] python =