Ticket #17162: 17162-2.diff

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

     
    309309                context.update({'another_var': True})
    310310            return context
    311311
    312 .. method:: WizardView.get_wizard_name()
     312.. method:: WizardView.get_prefix(request, *args, **kwargs)
    313313
    314     This method can be used to change the wizard's internal name.
     314    This method returns a prefix for use by the storage backends. Backends use
     315    the prefix as a mechanism to allow data to be stored separately for each
     316    wizard. This allows wizards to store their data in a single backend
     317    without overwriting each other.
    315318
    316     Default implementation::
    317 
    318         def get_wizard_name(self):
    319             return normalize_name(self.__class__.__name__)
    320 
    321 .. method:: WizardView.get_prefix()
    322 
    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)
    326 
    327319    You can change this method to make the wizard data prefix more unique to,
    328320    e.g. have multiple instances of one wizard in one session.
    329321
    330322    Default implementation::
    331323
    332         def get_prefix(self):
    333             return self.wizard_name
     324        def get_prefix(self, request, *args, **kwargs):
     325            # use the lowercase underscore version of the class name
     326            return normalize_name(self.__class__.__name__)
    334327
    335328.. method:: WizardView.get_form(step=None, data=None, files=None)
    336329
  • 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