Mixin for named URL WizardViews with one form on the frontpage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from django.core.urlresolvers import reverse
from django.shortcuts import render, redirect
from django.contrib.formtools.wizard.views import (NamedUrlSessionWizardView,
                                                   NamedUrlCookieWizardView)


class RootStepWizard(object):
    """
    A wizard mixin for use cases with a wizard step on the frontpage
    of your site -- with a request path of ``'/'``.

    Just define the name of the step (e.g. ``root_step = 'landing'``)
    and it does the setup and redirection automatically.

    Example::

        # views.py
        class LandingWizardView(RootStepNamedUrlSessionWizardView):
            root_step = 'landing_page'

        # urls.py
        landing_forms = (
            ('landing_page', LandingPageForm),
            ('welcome', WelcomeForm),
            ('account', AccountForm),
        )

        landing_wizard = LandingWizardView.as_view(landing_forms,
                                                   url_name='landing_step')

    """
    root_step = None

    def get(self, *args, **kwargs):
        """
        Redirects to the root path if needed and sets the step
        variable if on the root path.
        """
        if self.request.path == '/':
            kwargs['step'] = self.root_step
        elif self.request.path == reverse(self.url_name,
                                          kwargs={'step': self.root_step}):
            return redirect('/')
        return super(RootStepWizard, self).get(*args, **kwargs)

    def get_step_url(self, step):
        """
        Returns a root path for the configured step.
        """
        if step == self.root_step:
            return '/'
        return super(RootStepWizard, self).get_step_url(step)


class RootStepNamedUrlSessionWizardView(RootStepWizard,
                                        NamedUrlSessionWizardView):
    pass


class RootStepNamedUrlCookieWizardView(RootStepWizard,
                                       NamedUrlCookieWizardView):
    pass

More like this

  1. Extended Form Wizard with ability to go backwards by mtsyganov 4 years, 12 months ago
  2. Session Wizard by ddurham 5 years, 6 months ago
  3. Wizard class by king 6 years, 11 months ago
  4. MultiFormWizard by flashingpumpkin 4 years, 11 months ago
  5. Session-backed FormWizard by donspaulding 5 years, 6 months ago

Comments

(Forgotten your password?)