@@ -46,7 +46,7 @@ def __repr__(self):
46
46
@property
47
47
def all (self ):
48
48
"Returns the names of all steps/forms."
49
- return list (self ._wizard .get_form_list () )
49
+ return list (self ._wizard .form_list )
50
50
51
51
@property
52
52
def count (self ):
@@ -201,28 +201,21 @@ def get_prefix(self, request, *args, **kwargs):
201
201
# TODO: Add some kind of unique id to prefix
202
202
return normalize_name (self .__class__ .__name__ )
203
203
204
- def get_form_list (self ):
204
+ def process_condition_dict (self ):
205
205
"""
206
- This method returns a form_list based on the initial form list but
207
- checks if there is a condition method/value in the condition_list.
208
- If an entry exists in the condition list, it will call/read the value
209
- and respect the result. (True means add the form, False means ignore
210
- the form)
211
-
212
- The form_list is always generated on the fly because condition methods
213
- could use data from other (maybe previous forms).
206
+ This method prunes `self.form_list` by checking if there is a condition method/value in `condition_list`.
207
+ If an entry exists, it will call/read the value and respect the result. If the condition returns False, the
208
+ form will be removed from `form_list`.
214
209
"""
215
- form_list = OrderedDict ()
216
- for form_key , form_class in self .form_list .items ():
210
+ for form_key in list (self .form_list .keys ()):
217
211
# try to fetch the value from condition list, by default, the form
218
212
# gets passed to the new list.
219
213
condition = self .condition_dict .get (form_key , True )
220
214
if callable (condition ):
221
215
# call the value if needed, passes the current instance.
222
216
condition = condition (self )
223
- if condition :
224
- form_list [form_key ] = form_class
225
- return form_list
217
+ if not condition :
218
+ del self .form_list [form_key ]
226
219
227
220
def dispatch (self , request , * args , ** kwargs ):
228
221
"""
@@ -241,6 +234,7 @@ def dispatch(self, request, *args, **kwargs):
241
234
getattr (self , 'file_storage' , None ),
242
235
)
243
236
self .steps = StepsHelper (self )
237
+ self .process_condition_dict ()
244
238
response = super ().dispatch (request , * args , ** kwargs )
245
239
246
240
# update the response (e.g. adding cookies)
@@ -273,7 +267,7 @@ def post(self, *args, **kwargs):
273
267
# contains a valid step name. If one was found, render the requested
274
268
# form. (This makes stepping back a lot easier).
275
269
wizard_goto_step = self .request .POST .get ('wizard_goto_step' , None )
276
- if wizard_goto_step and wizard_goto_step in self .get_form_list () :
270
+ if wizard_goto_step and wizard_goto_step in self .form_list :
277
271
return self .render_goto_step (wizard_goto_step )
278
272
279
273
# Check if form was refreshed
@@ -342,7 +336,7 @@ def render_done(self, form, **kwargs):
342
336
"""
343
337
final_forms = OrderedDict ()
344
338
# walk through the form list and try to validate the data again.
345
- for form_key in self .get_form_list ():
339
+ for form_key in self .form_list . keys ():
346
340
form_obj = self .get_form (
347
341
step = form_key ,
348
342
data = self .storage .get_step_data (form_key ),
@@ -406,7 +400,7 @@ def get_form(self, step=None, data=None, files=None):
406
400
"""
407
401
if step is None :
408
402
step = self .steps .current
409
- form_class = self .get_form_list () [step ]
403
+ form_class = self .form_list [step ]
410
404
# prepare the kwargs for the form instance.
411
405
kwargs = self .get_form_kwargs (step )
412
406
kwargs .update ({
@@ -469,7 +463,7 @@ def get_all_cleaned_data(self):
469
463
'formset-' and contain a list of the formset cleaned_data dictionaries.
470
464
"""
471
465
cleaned_data = {}
472
- for form_key in self .get_form_list ():
466
+ for form_key in self .form_list . keys ():
473
467
form_obj = self .get_form (
474
468
step = form_key ,
475
469
data = self .storage .get_step_data (form_key ),
@@ -510,8 +504,7 @@ def get_next_step(self, step=None):
510
504
"""
511
505
if step is None :
512
506
step = self .steps .current
513
- form_list = self .get_form_list ()
514
- keys = list (form_list .keys ())
507
+ keys = list (self .form_list .keys ())
515
508
if step not in keys :
516
509
return self .steps .first
517
510
key = keys .index (step ) + 1
@@ -529,8 +522,7 @@ def get_prev_step(self, step=None):
529
522
"""
530
523
if step is None :
531
524
step = self .steps .current
532
- form_list = self .get_form_list ()
533
- keys = list (form_list .keys ())
525
+ keys = list (self .form_list .keys ())
534
526
if step not in keys :
535
527
return None
536
528
key = keys .index (step ) - 1
@@ -547,7 +539,7 @@ def get_step_index(self, step=None):
547
539
"""
548
540
if step is None :
549
541
step = self .steps .current
550
- keys = list (self .get_form_list () .keys ())
542
+ keys = list (self .form_list .keys ())
551
543
if step in keys :
552
544
return keys .index (step )
553
545
return None
@@ -678,7 +670,7 @@ def get(self, *args, **kwargs):
678
670
)
679
671
return self .render (form , ** kwargs )
680
672
681
- elif step_url in self .get_form_list () :
673
+ elif step_url in self .form_list :
682
674
self .storage .current_step = step_url
683
675
return self .render (
684
676
self .get_form (
@@ -699,7 +691,7 @@ def post(self, *args, **kwargs):
699
691
is super'd from WizardView.
700
692
"""
701
693
wizard_goto_step = self .request .POST .get ('wizard_goto_step' , None )
702
- if wizard_goto_step and wizard_goto_step in self .get_form_list () :
694
+ if wizard_goto_step and wizard_goto_step in self .form_list :
703
695
return self .render_goto_step (wizard_goto_step )
704
696
return super ().post (* args , ** kwargs )
705
697
0 commit comments