Skip to content

Commit ddb85da

Browse files
authored
Fix the new Django admin user edit form's password change button (#129)
* Fix the new Django admin user edit form's password change button At some point in the recent past (https://code.djangoproject.com/ticket/34977), Django changed the user edit form away from showing a link to the password change page, into a button. This relied on a call to `get_context()`, which this custom password widget wasn't invoking. * Wrap the hidden password in gettext * Update the widget test to ensure the button is there * Check for the admin password change view url in the test This should work both for pre-5.0 and post-5.0 versions of Django.
1 parent 1db8f2f commit ddb85da

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

authtools/forms.py

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,12 @@ class BetterReadOnlyPasswordHashWidget(ReadOnlyPasswordHashWidget):
3131
"""
3232
A ReadOnlyPasswordHashWidget that has a less intimidating output.
3333
"""
34-
def render(self, name, value, attrs=None, renderer=None):
35-
final_attrs = flatatt(self.build_attrs(attrs or {}))
3634

37-
if not value or not is_password_usable(value):
38-
summary = gettext("No password set.")
39-
else:
40-
try:
41-
identify_hasher(value)
42-
except ValueError:
43-
summary = gettext("Invalid password format or unknown"
44-
" hashing algorithm.")
45-
else:
46-
summary = gettext('*************')
47-
48-
return format_html('<div{attrs}><strong>{summary}</strong></div>',
49-
attrs=final_attrs, summary=summary)
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
5040

5141

5242
class UserChangeForm(DjangoUserChangeForm):

tests/tests/tests.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from unittest import skipIf, skipUnless
44

5+
import django
56
from django.core import mail
67

78
from django.test import TestCase
@@ -258,6 +259,15 @@ def test_better_readonly_password_widget(self):
258259

259260
self.assertIn(_('*************'), form.as_table())
260261

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())
270+
261271

262272
class UserAdminTest(TestCase):
263273
def test_generated_fieldsets(self):

0 commit comments

Comments
 (0)