Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added some class attributes to pass initial form lists to the WizardV…

…iew without the need to add them in the as_view call.
  • Loading branch information...
commit b614c47f8c05f8c29a974dac984d0f07c3a09fce 1 parent 930af66
Stephan Jaekel authored January 12, 2013
29  django/contrib/formtools/tests/wizard/forms.py
@@ -78,6 +78,12 @@ def get_form_kwargs(self, step, *args, **kwargs):
78 78
             kwargs['test'] = True
79 79
         return kwargs
80 80
 
  81
+class TestWizardWithInitAttrs(TestWizard):
  82
+    form_list = [Step1, Step2]
  83
+    condition_dict = {'step2': True}
  84
+    initial_dict = {'start': {'name': 'value1'}}
  85
+    instance_dict = {'start': User()}
  86
+
81 87
 class FormTests(TestCase):
82 88
     def test_form_init(self):
83 89
         testform = TestWizard.get_initkwargs([Step1, Step2])
@@ -91,6 +97,9 @@ def test_form_init(self):
91 97
         self.assertEqual(
92 98
             testform['form_list'], {'0': Step1, '1': Step2, 'finish': Step3})
93 99
 
  100
+        testform = TestWizardWithInitAttrs.get_initkwargs()
  101
+        self.assertEqual(testform['form_list'], {'0': Step1, '1': Step2})
  102
+
94 103
     def test_first_step(self):
95 104
         request = get_request()
96 105
 
@@ -132,6 +141,11 @@ def test_form_condition(self):
132 141
         response, instance = testform(request)
133 142
         self.assertEqual(instance.get_next_step(), 'step3')
134 143
 
  144
+        testform = TestWizardWithInitAttrs.as_view(
  145
+            [('start', Step1), ('step2', Step2), ('step3', Step3)])
  146
+        response, instance = testform(request)
  147
+        self.assertEqual(instance.get_next_step(), 'step2')
  148
+
135 149
     def test_form_kwargs(self):
136 150
         request = get_request()
137 151
 
@@ -162,6 +176,13 @@ def test_form_initial(self):
162 176
         self.assertEqual(instance.get_form_initial('start'), {'name': 'value1'})
163 177
         self.assertEqual(instance.get_form_initial('step2'), {})
164 178
 
  179
+        testform = TestWizardWithInitAttrs.as_view(
  180
+            [('start', Step1), ('step2', Step2)])
  181
+        response, instance = testform(request)
  182
+
  183
+        self.assertEqual(instance.get_form_initial('start'), {'name': 'value1'})
  184
+        self.assertEqual(instance.get_form_initial('step2'), {})
  185
+
165 186
     def test_form_instance(self):
166 187
         request = get_request()
167 188
         the_instance = TestModel()
@@ -176,6 +197,14 @@ def test_form_instance(self):
176 197
             instance.get_form_instance('non_exist_instance'),
177 198
             None)
178 199
 
  200
+        testform = TestWizardWithInitAttrs.as_view(
  201
+            [('start', TestModelForm), ('step2', Step2)])
  202
+        response, instance = testform(request)
  203
+
  204
+        self.assertEqual(
  205
+            instance.get_form_instance('start'),
  206
+            TestWizardWithInitAttrs.instance_dict['start'])
  207
+
179 208
     def test_formset_instance(self):
180 209
         request = get_request()
181 210
         the_instance1, created = TestModel.objects.get_or_create(
28  django/contrib/formtools/wizard/views.py
@@ -120,8 +120,8 @@ def as_view(cls, *args, **kwargs):
120 120
         return super(WizardView, cls).as_view(**initkwargs)
121 121
 
122 122
     @classmethod
123  
-    def get_initkwargs(cls, form_list, initial_dict=None,
124  
-            instance_dict=None, condition_dict=None, *args, **kwargs):
  123
+    def get_initkwargs(cls, form_list=None, initial_dict=None,
  124
+        instance_dict=None, condition_dict=None, *args, **kwargs):
125 125
         """
126 126
         Creates a dict with all needed parameters for the form wizard instances.
127 127
 
@@ -144,12 +144,20 @@ def get_initkwargs(cls, form_list, initial_dict=None,
144 144
           will be called with the wizardview instance as the only argument.
145 145
           If the return value is true, the step's form will be used.
146 146
         """
  147
+
147 148
         kwargs.update({
148  
-            'initial_dict': initial_dict or {},
149  
-            'instance_dict': instance_dict or {},
150  
-            'condition_dict': condition_dict or {},
  149
+            'initial_dict': initial_dict or kwargs.pop('initial_dict',
  150
+                getattr(cls, 'initial_dict', None)) or {},
  151
+            'instance_dict': instance_dict or kwargs.pop('instance_dict',
  152
+                getattr(cls, 'instance_dict', None)) or {},
  153
+            'condition_dict': condition_dict or kwargs.pop('condition_dict',
  154
+                getattr(cls, 'condition_dict', None)) or {}
151 155
         })
152  
-        init_form_list = SortedDict()
  156
+
  157
+        form_list = form_list or kwargs.pop('form_list',
  158
+            getattr(cls, 'form_list', None)) or []
  159
+
  160
+        computed_form_list = SortedDict()
153 161
 
154 162
         assert len(form_list) > 0, 'at least one form is needed'
155 163
 
@@ -158,13 +166,13 @@ def get_initkwargs(cls, form_list, initial_dict=None,
158 166
             if isinstance(form, (list, tuple)):
159 167
                 # if the element is a tuple, add the tuple to the new created
160 168
                 # sorted dictionary.
161  
-                init_form_list[six.text_type(form[0])] = form[1]
  169
+                computed_form_list[six.text_type(form[0])] = form[1]
162 170
             else:
163 171
                 # if not, add the form with a zero based counter as unicode
164  
-                init_form_list[six.text_type(i)] = form
  172
+                computed_form_list[six.text_type(i)] = form
165 173
 
166 174
         # walk through the new created list of forms
167  
-        for form in six.itervalues(init_form_list):
  175
+        for form in six.itervalues(computed_form_list):
168 176
             if issubclass(form, formsets.BaseFormSet):
169 177
                 # if the element is based on BaseFormSet (FormSet/ModelFormSet)
170 178
                 # we need to override the form variable.
@@ -179,7 +187,7 @@ def get_initkwargs(cls, form_list, initial_dict=None,
179 187
                             "wizard view in order to handle file uploads.")
180 188
 
181 189
         # build the kwargs for the wizardview instances
182  
-        kwargs['form_list'] = init_form_list
  190
+        kwargs['form_list'] = computed_form_list
183 191
         return kwargs
184 192
 
185 193
     def get_prefix(self, *args, **kwargs):
20  docs/ref/contrib/formtools/form-wizard.txt
@@ -245,6 +245,13 @@ wizard's ``as_view()`` method takes a list of your
245 245
         (r'^contact/$', ContactWizard.as_view([ContactForm1, ContactForm2])),
246 246
     )
247 247
 
  248
+.. versionchanged:: 1.6
  249
+
  250
+You can also pass the form list as a class attribute named ``form_list``.
  251
+
  252
+    class ContactWizard(WizardView):
  253
+        form_list = [ContactForm1, ContactForm2]
  254
+
248 255
 .. _wizard-template-for-each-form:
249 256
 
250 257
 Using a different template for each form
@@ -295,6 +302,14 @@ The ``urls.py`` file would contain something like::
295 302
         (r'^checkout/$', OrderWizard.as_view(FORMS, condition_dict={'cc': pay_by_credit_card})),
296 303
     )
297 304
 
  305
+.. versionchanged:: 1.6
  306
+
  307
+The ``condiction_dict`` can be passed as attribute for the ``as_view()``
  308
+method or as a class attribute named ``condition_dict``.
  309
+
  310
+    class OrderWizard(WizardView):
  311
+        condition_dict = {'cc': pay_by_credit_card}
  312
+
298 313
 Note that the ``OrderWizard`` object is initialized with a list of pairs.
299 314
 The first element in the pair is a string that corresponds to the name of the
300 315
 step and the second is the form class.
@@ -550,6 +565,11 @@ Providing initial data for the forms
550 565
     The ``initial_dict`` can also take a list of dictionaries for a specific
551 566
     step if the step is a ``FormSet``.
552 567
 
  568
+    .. versionchanged:: 1.6
  569
+
  570
+    The ``initial_dict`` can also be added as a class attribute named
  571
+    ``initial_dict`` to avoid having the initial data in the ``urls.py``.
  572
+
553 573
 .. _wizard-files:
554 574
 
555 575
 Handling files

0 notes on commit b614c47

Please sign in to comment.
Something went wrong with that request. Please try again.