Ticket #19349: 19349_2.diff

File 19349_2.diff, 2.9 KB (added by foonicorn, 12 years ago)
  • django/contrib/auth/forms.py

    diff --git a/django/contrib/auth/forms.py b/django/contrib/auth/forms.py
    index 9279c52..673c044 100644
    a b class ReadOnlyPasswordHashWidget(forms.Widget):  
    2727        encoded = value
    2828        final_attrs = self.build_attrs(attrs)
    2929
    30         if encoded == '' or encoded == UNUSABLE_PASSWORD:
     30        if not encoded or encoded == UNUSABLE_PASSWORD:
    3131            summary = mark_safe("<strong>%s</strong>" % ugettext("No password set."))
    3232        else:
    3333            try:
    class ReadOnlyPasswordHashField(forms.Field):  
    5252        kwargs.setdefault("required", False)
    5353        super(ReadOnlyPasswordHashField, self).__init__(*args, **kwargs)
    5454
     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
    5560
    5661class UserCreationForm(forms.ModelForm):
    5762    """
  • django/contrib/auth/tests/forms.py

    diff --git a/django/contrib/auth/tests/forms.py b/django/contrib/auth/tests/forms.py
    index f3eb242..1306619 100644
    a b from __future__ import unicode_literals  
    33import os
    44from django.contrib.auth.models import User
    55from django.contrib.auth.forms import (UserCreationForm, AuthenticationForm,
    6     PasswordChangeForm, SetPasswordForm, UserChangeForm, PasswordResetForm)
     6    PasswordChangeForm, SetPasswordForm, UserChangeForm, PasswordResetForm,
     7    ReadOnlyPasswordHashWidget)
    78from django.contrib.auth.tests.utils import skipIfCustomUser
    89from django.core import mail
    910from django.forms.fields import Field, EmailField
    class UserChangeFormTest(TestCase):  
    282283        self.assertTrue(form.is_valid())
    283284        self.assertEqual(form.cleaned_data['password'], 'sha1$6efc0$f93efe9fd7542f25a7be94871ea45aa95de57161')
    284285
     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
    285294
    286295@skipIfCustomUser
    287296@override_settings(USE_TZ=False, PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',))
    class PasswordResetFormTest(TestCase):  
    362371        self.assertFalse(form.is_valid())
    363372        self.assertEqual(form["email"].errors,
    364373                         [_("The user account associated with this email address cannot reset the password.")])
     374
     375
     376class 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.assertTrue(html)
Back to Top