Ticket #17162: trac-17162.diff

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

get_prefix without request

  • django/contrib/formtools/wizard/views.py

    diff --git a/django/contrib/formtools/wizard/views.py b/django/contrib/formtools/wizard/views.py
    index 15ba146..22e6184 100644
    a b from django.contrib.formtools.wizard.forms import ManagementForm 
    1414
    1515
    1616def normalize_name(name):
     17    """
     18    Converts camel-case style names into underscore seperated words. Example::
     19
     20        >>> normalize_name('oneTwoThree')
     21        'one_two_three'
     22        >>> normalize_name('FourFiveSix')
     23        'four_five_six'
     24
     25    """
    1726    new = re.sub('(((?<=[a-z])[A-Z])|([A-Z](?![A-Z]|$)))', '_\\1', name)
    1827    return new.lower().strip('_')
    1928
    class WizardView(TemplateView): 
    169178        kwargs['form_list'] = init_form_list
    170179        return kwargs
    171180
    172     def get_wizard_name(self):
    173         return normalize_name(self.__class__.__name__)
    174 
    175     def get_prefix(self):
     181    def get_prefix(self, *args, **kwargs):
    176182        # TODO: Add some kind of unique id to prefix
    177         return self.wizard_name
     183        return normalize_name(self.__class__.__name__)
    178184
    179185    def get_form_list(self):
    180186        """
    class WizardView(TemplateView): 
    210216        response gets updated by the storage engine (for example add cookies).
    211217        """
    212218        # add the storage engine to the current formwizard instance
    213         self.wizard_name = self.get_wizard_name()
    214         self.prefix = self.get_prefix()
     219        self.prefix = self.get_prefix(*args, **kwargs)
    215220        self.storage = get_storage(self.storage_name, self.prefix, request,
    216221            getattr(self, 'file_storage', None))
    217222        self.steps = StepsHelper(self)
    class NamedUrlCookieWizardView(NamedUrlWizardView): 
    683688    A NamedUrlFormWizard with pre-configured CookieStorageBackend.
    684689    """
    685690    storage_name = 'django.contrib.formtools.wizard.storage.cookie.CookieStorage'
    686 
  • docs/ref/contrib/formtools/form-wizard.txt

    diff --git a/docs/ref/contrib/formtools/form-wizard.txt b/docs/ref/contrib/formtools/form-wizard.txt
    index aadd7d2..59e68e2 100644
    a b Advanced ``WizardView`` methods 
    314314                context.update({'another_var': True})
    315315            return context
    316316
    317 .. method:: WizardView.get_wizard_name()
     317.. method:: WizardView.get_prefix(*args, **kwargs)
    318318
    319     This method can be used to change the wizard's internal name.
    320 
    321     Default implementation::
    322 
    323         def get_wizard_name(self):
    324             return normalize_name(self.__class__.__name__)
    325 
    326 .. method:: WizardView.get_prefix()
    327 
    328     This method returns a prefix for the storage backends. These backends use
    329     the prefix to fetch the correct data for the wizard. (Multiple wizards
    330     could save their data in one session)
     319    This method returns a prefix for use by the storage backends. Backends use
     320    the prefix as a mechanism to allow data to be stored separately for each
     321    wizard. This allows wizards to store their data in a single backend
     322    without overwriting each other.
    331323
    332324    You can change this method to make the wizard data prefix more unique to,
    333325    e.g. have multiple instances of one wizard in one session.
    334326
    335327    Default implementation::
    336328
    337         def get_prefix(self):
    338             return self.wizard_name
     329        def get_prefix(self, *args, **kwargs):
     330            # use the lowercase underscore version of the class name
     331            return normalize_name(self.__class__.__name__)
    339332
    340333.. method:: WizardView.get_form(step=None, data=None, files=None)
    341334
Back to Top