Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #19349 -- Fixed re-rendering of ReadOnlyPasswordHashWidget

Thanks tim.bowden at mapforge.com.au for the report, Andreas Hug
for the patch and Anton Baklanov for the review.
  • Loading branch information...
commit a0cd6dd11e81cd0e229d6d109524c8a7e52bc9e1 1 parent 84a5294
Claude Paroz authored December 01, 2012
7  django/contrib/auth/forms.py
@@ -27,7 +27,7 @@ def render(self, name, value, attrs):
27 27
         encoded = value
28 28
         final_attrs = self.build_attrs(attrs)
29 29
 
30  
-        if encoded == '' or encoded == UNUSABLE_PASSWORD:
  30
+        if not encoded or encoded == UNUSABLE_PASSWORD:
31 31
             summary = mark_safe("<strong>%s</strong>" % ugettext("No password set."))
32 32
         else:
33 33
             try:
@@ -52,6 +52,11 @@ def __init__(self, *args, **kwargs):
52 52
         kwargs.setdefault("required", False)
53 53
         super(ReadOnlyPasswordHashField, self).__init__(*args, **kwargs)
54 54
 
  55
+    def bound_data(self, data, initial):
  56
+        # Always return initial because the widget doesn't
  57
+        # render an input field.
  58
+        return initial
  59
+
55 60
 
56 61
 class UserCreationForm(forms.ModelForm):
57 62
     """
21  django/contrib/auth/tests/forms.py
@@ -3,7 +3,8 @@
3 3
 import os
4 4
 from django.contrib.auth.models import User
5 5
 from django.contrib.auth.forms import (UserCreationForm, AuthenticationForm,
6  
-    PasswordChangeForm, SetPasswordForm, UserChangeForm, PasswordResetForm)
  6
+    PasswordChangeForm, SetPasswordForm, UserChangeForm, PasswordResetForm,
  7
+    ReadOnlyPasswordHashWidget)
7 8
 from django.contrib.auth.tests.utils import skipIfCustomUser
8 9
 from django.core import mail
9 10
 from django.forms.fields import Field, EmailField
@@ -282,6 +283,14 @@ def test_bug_19133(self):
282 283
         self.assertTrue(form.is_valid())
283 284
         self.assertEqual(form.cleaned_data['password'], 'sha1$6efc0$f93efe9fd7542f25a7be94871ea45aa95de57161')
284 285
 
  286
+    def test_bug_19349_bound_password_field(self):
  287
+        user = User.objects.get(username='testclient')
  288
+        form = UserChangeForm(data={}, instance=user)
  289
+        # When rendering the bound password field,
  290
+        # ReadOnlyPasswordHashWidget needs the initial
  291
+        # value to render correctly
  292
+        self.assertEqual(form.initial['password'], form['password'].value())
  293
+
285 294
 
286 295
 @skipIfCustomUser
287 296
 @override_settings(USE_TZ=False, PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',))
@@ -362,3 +371,13 @@ def test_unusable_password(self):
362 371
         self.assertFalse(form.is_valid())
363 372
         self.assertEqual(form["email"].errors,
364 373
                          [_("The user account associated with this email address cannot reset the password.")])
  374
+
  375
+
  376
+class ReadOnlyPasswordHashWidgetTest(TestCase):
  377
+
  378
+    def test_bug_19349_render_with_none_value(self):
  379
+        # Rendering the widget with value set to None
  380
+        # mustn't raise an exception.
  381
+        widget = ReadOnlyPasswordHashWidget()
  382
+        html = widget.render(name='password', value=None, attrs={})
  383
+        self.assertIn(_("No password set."), html)

0 notes on commit a0cd6dd

Please sign in to comment.
Something went wrong with that request. Please try again.