Code

Ticket #19349: 19349.diff

File 19349.diff, 1.5 KB (added by foonicorn, 17 months ago)

Override ReadOnlyPasswordHashField.bound_data to always return initial value

Line 
1diff --git a/django/contrib/auth/forms.py b/django/contrib/auth/forms.py
2index 9279c52..e011d47 100644
3--- a/django/contrib/auth/forms.py
4+++ b/django/contrib/auth/forms.py
5@@ -52,6 +52,11 @@ class ReadOnlyPasswordHashField(forms.Field):
6         kwargs.setdefault("required", False)
7         super(ReadOnlyPasswordHashField, self).__init__(*args, **kwargs)
8 
9+    def bound_data(self, data, initial):
10+        # Always return initial because the widget doesn't
11+        # render an input field.
12+        return initial
13+
14 
15 class UserCreationForm(forms.ModelForm):
16     """
17diff --git a/django/contrib/auth/tests/forms.py b/django/contrib/auth/tests/forms.py
18index f3eb242..87fcfc8 100644
19--- a/django/contrib/auth/tests/forms.py
20+++ b/django/contrib/auth/tests/forms.py
21@@ -282,6 +282,14 @@ class UserChangeFormTest(TestCase):
22         self.assertTrue(form.is_valid())
23         self.assertEqual(form.cleaned_data['password'], 'sha1$6efc0$f93efe9fd7542f25a7be94871ea45aa95de57161')
24 
25+    def test_bug_19349_bound_password_field(self):
26+        user = User.objects.get(username='testclient')
27+        form = UserChangeForm(data={}, instance=user)
28+        # When rendering the bound password field,
29+        # ReadOnlyPasswordHashWidget needs the initial
30+        # value to render correctly
31+        self.assertEqual(form.initial['password'], form['password'].value())
32+
33 
34 @skipIfCustomUser
35 @override_settings(USE_TZ=False, PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',))