Skip to content

Commit cfeb1fd

Browse files
committed
Fix BetterReadOnlyPasswordHashWidget for django 6.0
1 parent cceb54a commit cfeb1fd

6 files changed

Lines changed: 46 additions & 17 deletions

File tree

.github/workflows/ci.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,16 @@ jobs:
8787
python-version: "3.12"
8888
- django-version: "5.2"
8989
python-version: "3.13"
90+
- django-version: "5.2"
91+
python-version: "3.14"
9092

93+
# Django 6.0
94+
- django-version: "6.0"
95+
python-version: "3.12"
96+
- django-version: "6.0"
97+
python-version: "3.13"
98+
- django-version: "6.0"
99+
python-version: "3.14"
91100
steps:
92101
- uses: actions/checkout@v4
93102

authtools/forms.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,7 @@ class BetterReadOnlyPasswordHashWidget(ReadOnlyPasswordHashWidget):
3131
"""
3232
A ReadOnlyPasswordHashWidget that has a less intimidating output.
3333
"""
34-
35-
def get_context(self, name, value, attrs):
36-
context = super().get_context(name, value, attrs)
37-
if any(item.get('value') for item in context['summary']):
38-
context['summary'] = [{'label': gettext('*************')}]
39-
return context
34+
template_name = 'authtools/widgets/better_read_only_password_hash.html'
4035

4136

4237
class UserChangeForm(DjangoUserChangeForm):
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{% load authtools %}
2+
<div{% include 'django/forms/widgets/attrs.html' %}>
3+
{% render_better_read_only_password_hash widget.value %}
4+
<p><a href="{{ password_url|default:"../password/" }}">{{ button_label }}</a></p>
5+
</div>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from django.contrib.auth.hashers import UNUSABLE_PASSWORD_PREFIX, identify_hasher
2+
from django.template import Library
3+
from django.utils.html import format_html
4+
from django.utils.translation import gettext
5+
6+
register = Library()
7+
8+
9+
@register.simple_tag
10+
def render_better_read_only_password_hash(value):
11+
if not value or value.startswith(UNUSABLE_PASSWORD_PREFIX):
12+
return format_html("<p><strong>{}</strong></p>", gettext("No password set."))
13+
try:
14+
hasher = identify_hasher(value)
15+
hasher.safe_summary(value)
16+
except ValueError:
17+
return format_html(
18+
"<p><strong>{}</strong></p>",
19+
gettext("Invalid password format or unknown hashing algorithm."),
20+
)
21+
22+
return format_html(
23+
"<p>{}</p>",
24+
gettext("*************"),
25+
)

tests/tests/tests.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -258,15 +258,7 @@ def test_better_readonly_password_widget(self):
258258
form = UserChangeForm(instance=user)
259259

260260
self.assertIn(_('*************'), form.as_table())
261-
262-
version = django.VERSION[0]
263-
264-
if version < 4:
265-
self.assertIn('<a href="../password/">', form.as_table())
266-
elif version < 5:
267-
self.assertIn('<a href="../../{0}/password/">'.format(user.id), form.as_table())
268-
else:
269-
self.assertIn('<a class="button" href="../password/">', form.as_table())
261+
self.assertIn('<a href="../password/">', form.as_table())
270262

271263

272264
class UserAdminTest(TestCase):

tox.ini

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ envlist=
33
py37-dj{22,30,32,32}
44
py{38,39}-dj{22,30,31,32,40,41,42}
55
py{10}-dj{32,40,41,42,50,51,52}
6-
py{11,12}-dj{42,50,51,52}
7-
py313-dj{51,52}
6+
py{11,12}-dj{42,50,51,52,60}
7+
py313-dj{51,52,60}
8+
py314-dj60
89
[testenv]
910
python=
1011
py37: python3.7
@@ -14,6 +15,7 @@ python=
1415
py311: python3.11
1516
py312: python3.12
1617
py313: python3.13
18+
py314: python3.14
1719
commands=
1820
/usr/bin/env
1921
make test
@@ -28,6 +30,7 @@ deps=
2830
dj50: Django>=5.0,<5.1
2931
dj51: Django>=5.1,<5.2
3032
dj52: Django>=5.2,<5.3
33+
dj60: Django>=6.0,<6.1
3134
whitelist_externals=
3235
env
3336
make

0 commit comments

Comments
 (0)