Login

Mixin for named URL WizardViews with one form on the frontpage

Author:
jezdez
Posted:
March 5, 2012
Language:
Python
Version:
Not specified
Score:
0 (after 0 ratings)

A Django 1.4 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_page') and it does the setup and redirection automatically.

 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. Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
  5. Help text hyperlinks by sa2812 1 year, 8 months ago

Comments

Please login first before commenting.