Ticket #6209: patch-#6209.diff

File patch-#6209.diff, 4.8 KB (added by Rajesh Dhawan, 16 years ago)

Patch fixes issue and adds a unit test for this case

  • django/contrib/formtools/preview.py

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

     
    55from django.test import TestCase
    66
    77success_string = "Done was called!"
    8 test_data = {'field1': u'foo',
    9              'field1_': u'asdf'}
    108
    119
    1210class TestFormPreview(preview.FormPreview):
     
    1816class TestForm(forms.Form):
    1917    field1 = forms.CharField()
    2018    field1_ = forms.CharField()
     19    bool1 = forms.BooleanField(required=False)
    2120
    2221
    2322class PreviewTests(TestCase):
     
    2827        self.preview = preview.FormPreview(TestForm)
    2928        input_template = '<input type="hidden" name="%s" value="%s" />'
    3029        self.input = input_template % (self.preview.unused_name('stage'), "%d")
     30        self.test_data = {'field1':u'foo', 'field1_':u'asdf'}
    3131
    3232    def test_unused_name(self):
    3333        """
     
    6060        """
    6161        # Pass strings for form submittal and add stage variable to
    6262        # show we previously saw first stage of the form.
    63         test_data.update({'stage': 1})
    64         response = self.client.post('/test1/', test_data)
     63        self.test_data.update({'stage': 1})
     64        response = self.client.post('/test1/', self.test_data)
    6565        # Check to confirm stage is set to 2 in output form.
    6666        stage = self.input % 2
    6767        self.assertContains(response, stage, 1)
     
    7878        """
    7979        # Pass strings for form submittal and add stage variable to
    8080        # show we previously saw first stage of the form.
    81         test_data.update({'stage': 2})
    82         response = self.client.post('/test1/', test_data)
     81        self.test_data.update({'stage':2})
     82        response = self.client.post('/test1/', self.test_data)
    8383        self.failIfEqual(response.content, success_string)
    84         hash = self.preview.security_hash(None, TestForm(test_data))
    85         test_data.update({'hash': hash})
    86         response = self.client.post('/test1/', test_data)
     84        hash = self.preview.security_hash(None, TestForm(self.test_data))
     85        self.test_data.update({'hash': hash})
     86        response = self.client.post('/test1/', self.test_data)
    8787        self.assertEqual(response.content, success_string)
    8888
     89    def test_bool_submit(self):
     90        """
     91        Test contrib.formtools.preview form submittal when form contains:
     92        BooleanField(required=False)
     93
     94        Ticket: #6209 - When an unchecked BooleanField is previewed, the preview
     95        form's hash would be computed with no value for ``bool1``. However, when
     96        the preview form is rendered, the unchecked hidden BooleanField would be
     97        rendered with the string value 'False'. So when the preview form is
     98        resubmitted, the hash would be computed with the value 'False' for
     99        ``bool1``. We need to make sure the hashes are the same in both cases.
     100
     101        """
     102        self.test_data.update({'stage':2})
     103        hash = self.preview.security_hash(None, TestForm(self.test_data))
     104        self.test_data.update({'hash':hash, 'bool1':u'False'})
     105        response = self.client.post('/test1/', self.test_data)
     106        self.assertEqual(response.content, success_string)
     107
Back to Top