12 from django.shortcuts import render_to_response |
12 from django.shortcuts import render_to_response |
13 from django.template.context import RequestContext |
13 from django.template.context import RequestContext |
14 from django.utils.hashcompat import md5_constructor |
14 from django.utils.hashcompat import md5_constructor |
15 from django.utils.translation import ugettext_lazy as _ |
15 from django.utils.translation import ugettext_lazy as _ |
16 from django.contrib.formtools.utils import security_hash |
16 from django.contrib.formtools.utils import security_hash |
|
17 from django.utils.decorators import method_decorator |
|
18 from django.views.decorators.csrf import csrf_protect |
|
19 |
17 |
20 |
18 class FormWizard(object): |
21 class FormWizard(object): |
19 # Dictionary of extra template context variables. |
|
20 extra_context = {} |
|
21 |
|
22 # The HTML (and POST data) field name for the "step" variable. |
22 # The HTML (and POST data) field name for the "step" variable. |
23 step_field_name="wizard_step" |
23 step_field_name="wizard_step" |
24 |
24 |
25 # METHODS SUBCLASSES SHOULDN'T OVERRIDE ################################### |
25 # METHODS SUBCLASSES SHOULDN'T OVERRIDE ################################### |
26 |
26 |
27 def __init__(self, form_list, initial=None): |
27 def __init__(self, form_list, initial=None): |
28 "form_list should be a list of Form classes (not instances)." |
28 """ |
|
29 Start a new wizard with a list of forms. |
|
30 |
|
31 form_list should be a list of Form classes (not instances). |
|
32 """ |
29 self.form_list = form_list[:] |
33 self.form_list = form_list[:] |
30 self.initial = initial or {} |
34 self.initial = initial or {} |
31 self.step = 0 # A zero-based counter keeping track of which step we're in. |
35 |
|
36 # Dictionary of extra template context variables. |
|
37 self.extra_context = {} |
|
38 |
|
39 # A zero-based counter keeping track of which step we're in. |
|
40 self.step = 0 |
32 |
41 |
33 def __repr__(self): |
42 def __repr__(self): |
34 return "step: %d\nform_list: %s\ninitial_data: %s" % (self.step, self.form_list, self.initial) |
43 return "step: %d\nform_list: %s\ninitial_data: %s" % (self.step, self.form_list, self.initial) |
35 |
44 |
36 def get_form(self, step, data=None): |
45 def get_form(self, step, data=None): |
42 # You might think we should just set "self.form_list = len(form_list)" |
51 # You might think we should just set "self.form_list = len(form_list)" |
43 # in __init__(), but this calculation needs to be dynamic, because some |
52 # in __init__(), but this calculation needs to be dynamic, because some |
44 # hook methods might alter self.form_list. |
53 # hook methods might alter self.form_list. |
45 return len(self.form_list) |
54 return len(self.form_list) |
46 |
55 |
|
56 @method_decorator(csrf_protect) |
47 def __call__(self, request, *args, **kwargs): |
57 def __call__(self, request, *args, **kwargs): |
48 """ |
58 """ |
49 Main method that does all the hard work, conforming to the Django view |
59 Main method that does all the hard work, conforming to the Django view |
50 interface. |
60 interface. |
51 """ |
61 """ |