Ticket #6209: ticket-6209-r8231.diff
File ticket-6209-r8231.diff, 4.9 KB (added by , 16 years ago) |
---|
-
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 6 6 7 7 from django.conf import settings 8 8 from django.http import Http404 9 from django.forms import BooleanField 9 10 from django.shortcuts import render_to_response 10 11 from django.template.context import RequestContext 11 12 from django.utils.hashcompat import md5_constructor … … class FormPreview(object): 106 107 Subclasses may want to take into account request-specific information 107 108 such as the IP address. 108 109 """ 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] 110 122 # Use HIGHEST_PROTOCOL because it's the most efficient. It requires 111 123 # Python 2.3, but Django requires 2.3 anyway, so that's OK. 112 124 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 4 4 from django.test import TestCase 5 5 6 6 success_string = "Done was called!" 7 test_data = {'field1': u'foo',8 'field1_': u'asdf'}9 7 10 8 11 9 class TestFormPreview(preview.FormPreview): … … class TestFormPreview(preview.FormPreview): 17 15 class TestForm(forms.Form): 18 16 field1 = forms.CharField() 19 17 field1_ = forms.CharField() 18 bool1 = forms.BooleanField(required=False) 20 19 21 20 22 21 class PreviewTests(TestCase): … … class PreviewTests(TestCase): 27 26 self.preview = preview.FormPreview(TestForm) 28 27 input_template = '<input type="hidden" name="%s" value="%s" />' 29 28 self.input = input_template % (self.preview.unused_name('stage'), "%d") 29 self.test_data = {'field1':u'foo', 'field1_':u'asdf'} 30 30 31 31 def test_unused_name(self): 32 32 """ … … class PreviewTests(TestCase): 59 59 """ 60 60 # Pass strings for form submittal and add stage variable to 61 61 # 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) 64 64 # Check to confirm stage is set to 2 in output form. 65 65 stage = self.input % 2 66 66 self.assertContains(response, stage, 1) … … class PreviewTests(TestCase): 77 77 """ 78 78 # Pass strings for form submittal and add stage variable to 79 79 # 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) 82 82 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) 86 105 self.assertEqual(response.content, success_string) 87 106