Ticket #6209: ticket-6209-r8231.diff

File ticket-6209-r8231.diff, 4.9 KB (added by mcroydon, 16 years ago)

Updated rajeshdhawan's patch to apply cleanly against r8231

  • django/contrib/formtools/preview.py

    diff --git a/django/contrib/formtools/preview.py b/django/contrib/formtools/preview.py
    index c56fdff..75bc8e2 100644
    a b import cPickle as pickle  
    66
    77from django.conf import settings
    88from django.http import Http404
     9from django.forms import BooleanField
    910from django.shortcuts import render_to_response
    1011from django.template.context import RequestContext
    1112from django.utils.hashcompat import md5_constructor
    class FormPreview(object):  
    106107        Subclasses may want to take into account request-specific information
    107108        such as the IP address.
    108109        """
    109         data = [(bf.name, bf.data or '') for bf in form] + [settings.SECRET_KEY]
     110        # Ensure that the hash does not change when a BooleanField's bound
     111        # data is a string `False' or a boolean False.
     112        # DRY: Rather than re-coding this special behaviour here, we
     113        # create a dummy BooleanField and call its clean method to get a
     114        # boolean True or False verdict that is consistent with
     115        # BooleanField.clean()
     116        dummy_bool = BooleanField(required=False)
     117        def _cleaned_data(bf):
     118            if isinstance(bf.field, BooleanField):
     119                return dummy_bool.clean(bf.data)
     120            return bf.data
     121        data = [(bf.name, _cleaned_data(bf) or '') for bf in form] + [settings.SECRET_KEY]
    110122        # Use HIGHEST_PROTOCOL because it's the most efficient. It requires
    111123        # Python 2.3, but Django requires 2.3 anyway, so that's OK.
    112124        pickled = pickle.dumps(data, pickle.HIGHEST_PROTOCOL)
  • django/contrib/formtools/tests.py

    diff --git a/django/contrib/formtools/tests.py b/django/contrib/formtools/tests.py
    index 2ea0cc9..1a5182a 100644
    a b from django import http  
    44from django.test import TestCase
    55
    66success_string = "Done was called!"
    7 test_data = {'field1': u'foo',
    8              'field1_': u'asdf'}
    97
    108
    119class TestFormPreview(preview.FormPreview):
    class TestFormPreview(preview.FormPreview):  
    1715class TestForm(forms.Form):
    1816    field1 = forms.CharField()
    1917    field1_ = forms.CharField()
     18    bool1 = forms.BooleanField(required=False)
    2019
    2120
    2221class PreviewTests(TestCase):
    class PreviewTests(TestCase):  
    2726        self.preview = preview.FormPreview(TestForm)
    2827        input_template = '<input type="hidden" name="%s" value="%s" />'
    2928        self.input = input_template % (self.preview.unused_name('stage'), "%d")
     29        self.test_data = {'field1':u'foo', 'field1_':u'asdf'}
    3030
    3131    def test_unused_name(self):
    3232        """
    class PreviewTests(TestCase):  
    5959        """
    6060        # Pass strings for form submittal and add stage variable to
    6161        # show we previously saw first stage of the form.
    62         test_data.update({'stage': 1})
    63         response = self.client.post('/test1/', test_data)
     62        self.test_data.update({'stage': 1})
     63        response = self.client.post('/test1/', self.test_data)
    6464        # Check to confirm stage is set to 2 in output form.
    6565        stage = self.input % 2
    6666        self.assertContains(response, stage, 1)
    class PreviewTests(TestCase):  
    7777        """
    7878        # Pass strings for form submittal and add stage variable to
    7979        # show we previously saw first stage of the form.
    80         test_data.update({'stage': 2})
    81         response = self.client.post('/test1/', test_data)
     80        self.test_data.update({'stage':2})
     81        response = self.client.post('/test1/', self.test_data)
    8282        self.failIfEqual(response.content, success_string)
    83         hash = self.preview.security_hash(None, TestForm(test_data))
    84         test_data.update({'hash': hash})
    85         response = self.client.post('/test1/', test_data)
     83        hash = self.preview.security_hash(None, TestForm(self.test_data))
     84        self.test_data.update({'hash': hash})
     85        response = self.client.post('/test1/', self.test_data)
     86        self.assertEqual(response.content, success_string)
     87
     88    def test_bool_submit(self):
     89        """
     90        Test contrib.formtools.preview form submittal when form contains:
     91        BooleanField(required=False)
     92
     93        Ticket: #6209 - When an unchecked BooleanField is previewed, the preview
     94        form's hash would be computed with no value for ``bool1``. However, when
     95        the preview form is rendered, the unchecked hidden BooleanField would be
     96        rendered with the string value 'False'. So when the preview form is
     97        resubmitted, the hash would be computed with the value 'False' for
     98        ``bool1``. We need to make sure the hashes are the same in both cases.
     99
     100        """
     101        self.test_data.update({'stage':2})
     102        hash = self.preview.security_hash(None, TestForm(self.test_data))
     103        self.test_data.update({'hash':hash, 'bool1':u'False'})
     104        response = self.client.post('/test1/', self.test_data)
    86105        self.assertEqual(response.content, success_string)
    87106
Back to Top