Ticket #17162: 17162.diff

File 17162.diff, 3.2 KB (added by Bradley Ayers <bradley.ayers@…>, 4 years ago)
  • docs/ref/contrib/formtools/form-wizard.txt

     
    318318        def get_wizard_name(self):
    319319            return normalize_name(self.__class__.__name__)
    320320
    321 .. method:: WizardView.get_prefix()
     321.. method:: WizardView.get_prefix(request, *args, **kwargs)
    322322
    323     This method returns a prefix for the storage backends. These backends use
    324     the prefix to fetch the correct data for the wizard. (Multiple wizards
    325     could save their data in one session)
     323    This method returns a prefix for use by the storage backends. Backends use
     324    the prefix as a mechanism to allow data to be stored separately for each
     325    wizard. This allows wizards to store their data in a single backend
     326    without overwriting each other.
    326327
    327328    You can change this method to make the wizard data prefix more unique to,
    328329    e.g. have multiple instances of one wizard in one session.
    329330
    330331    Default implementation::
    331332
    332         def get_prefix(self):
    333             return self.wizard_name
     333        def get_prefix(self, request, *args, **kwargs):
     334            # use the lowercase underscore version of the class name
     335            return normalize_name(self.__class__.__name__)
    334336
    335337.. method:: WizardView.get_form(step=None, data=None, files=None)
    336338
  • django/contrib/formtools/wizard/views.py

     
    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
     
    169178        kwargs['form_list'] = init_form_list
    170179        return kwargs
    171180
    172     def get_wizard_name(self):
     181    def get_prefix(self, request, *args, **kwargs):
     182        # TODO: Add some kind of unique id to prefix
    173183        return normalize_name(self.__class__.__name__)
    174184
    175     def get_prefix(self):
    176         # TODO: Add some kind of unique id to prefix
    177         return self.wizard_name
    178 
    179185    def get_form_list(self):
    180186        """
    181187        This method returns a form_list based on the initial form list but
     
    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(request, *args, **kwargs)
    215220        self.storage = get_storage(self.storage_name, self.prefix, request,
    216221            getattr(self, 'file_storage', None))
    217222        self.steps = StepsHelper(self)
     
    683688    A NamedUrlFormWizard with pre-configured CookieStorageBackend.
    684689    """
    685690    storage_name = 'django.contrib.formtools.wizard.storage.cookie.CookieStorage'
    686 
Back to Top