Code

Ticket #17162: trac-17162.diff

File trac-17162.diff, 3.5 KB (added by steph, 3 years ago)

get_prefix without request

Line 
1diff --git a/django/contrib/formtools/wizard/views.py b/django/contrib/formtools/wizard/views.py
2index 15ba146..22e6184 100644
3--- a/django/contrib/formtools/wizard/views.py
4+++ b/django/contrib/formtools/wizard/views.py
5@@ -14,6 +14,15 @@ from django.contrib.formtools.wizard.forms import ManagementForm
6 
7 
8 def normalize_name(name):
9+    """
10+    Converts camel-case style names into underscore seperated words. Example::
11+
12+        >>> normalize_name('oneTwoThree')
13+        'one_two_three'
14+        >>> normalize_name('FourFiveSix')
15+        'four_five_six'
16+
17+    """
18     new = re.sub('(((?<=[a-z])[A-Z])|([A-Z](?![A-Z]|$)))', '_\\1', name)
19     return new.lower().strip('_')
20 
21@@ -169,12 +178,9 @@ class WizardView(TemplateView):
22         kwargs['form_list'] = init_form_list
23         return kwargs
24 
25-    def get_wizard_name(self):
26-        return normalize_name(self.__class__.__name__)
27-
28-    def get_prefix(self):
29+    def get_prefix(self, *args, **kwargs):
30         # TODO: Add some kind of unique id to prefix
31-        return self.wizard_name
32+        return normalize_name(self.__class__.__name__)
33 
34     def get_form_list(self):
35         """
36@@ -210,8 +216,7 @@ class WizardView(TemplateView):
37         response gets updated by the storage engine (for example add cookies).
38         """
39         # add the storage engine to the current formwizard instance
40-        self.wizard_name = self.get_wizard_name()
41-        self.prefix = self.get_prefix()
42+        self.prefix = self.get_prefix(*args, **kwargs)
43         self.storage = get_storage(self.storage_name, self.prefix, request,
44             getattr(self, 'file_storage', None))
45         self.steps = StepsHelper(self)
46@@ -683,4 +688,3 @@ class NamedUrlCookieWizardView(NamedUrlWizardView):
47     A NamedUrlFormWizard with pre-configured CookieStorageBackend.
48     """
49     storage_name = 'django.contrib.formtools.wizard.storage.cookie.CookieStorage'
50-
51diff --git a/docs/ref/contrib/formtools/form-wizard.txt b/docs/ref/contrib/formtools/form-wizard.txt
52index aadd7d2..59e68e2 100644
53--- a/docs/ref/contrib/formtools/form-wizard.txt
54+++ b/docs/ref/contrib/formtools/form-wizard.txt
55@@ -314,28 +314,21 @@ Advanced ``WizardView`` methods
56                 context.update({'another_var': True})
57             return context
58 
59-.. method:: WizardView.get_wizard_name()
60+.. method:: WizardView.get_prefix(*args, **kwargs)
61 
62-    This method can be used to change the wizard's internal name.
63-
64-    Default implementation::
65-
66-        def get_wizard_name(self):
67-            return normalize_name(self.__class__.__name__)
68-
69-.. method:: WizardView.get_prefix()
70-
71-    This method returns a prefix for the storage backends. These backends use
72-    the prefix to fetch the correct data for the wizard. (Multiple wizards
73-    could save their data in one session)
74+    This method returns a prefix for use by the storage backends. Backends use
75+    the prefix as a mechanism to allow data to be stored separately for each
76+    wizard. This allows wizards to store their data in a single backend
77+    without overwriting each other.
78 
79     You can change this method to make the wizard data prefix more unique to,
80     e.g. have multiple instances of one wizard in one session.
81 
82     Default implementation::
83 
84-        def get_prefix(self):
85-            return self.wizard_name
86+        def get_prefix(self, *args, **kwargs):
87+            # use the lowercase underscore version of the class name
88+            return normalize_name(self.__class__.__name__)
89 
90 .. method:: WizardView.get_form(step=None, data=None, files=None)
91