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