Commit 3e790ae6 authored by Julien Phalip's avatar Julien Phalip
Browse files

Fixed #17148 -- Fixed the signature of `WizardView.get_context_data()` to play...

Fixed #17148 -- Fixed the signature of `WizardView.get_context_data()` to play nicer with mixin inheritance. Thanks, Bradley Ayers and Stephan Jaekel.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17231 bcc190cf-cafb-0310-a4f2-bffc1f526a37
parent 5a48cb5f
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -14,4 +14,5 @@ from django.contrib.formtools.tests.wizard.wizardtests.tests import (
    SessionWizardTests,
    CookieWizardTests,
    WizardTestKwargs,
    WizardTestGenericViewInterface,
)
+51 −0
Original line number Diff line number Diff line
from __future__ import with_statement
import os

from django import forms
from django.test import TestCase
from django.test.client import RequestFactory
from django.conf import settings
from django.contrib.auth.models import User
from django.contrib.formtools.wizard.views import CookieWizardView


class WizardTests(object):
@@ -280,3 +283,51 @@ class WizardTestKwargs(TestCase):
                TEMPLATE_DIRS=list(settings.TEMPLATE_DIRS) + [templates]):
            response = self.client.get(self.wizard_url)
            self.assertTemplateUsed(response, 'other_wizard_form.html')


class WizardTestGenericViewInterface(TestCase):
    def test_get_context_data_inheritance(self):
        class TestWizard(CookieWizardView):
            """
            A subclass that implements ``get_context_data`` using the standard
            protocol for generic views (accept only **kwargs).

            See ticket #17148.
            """
            def get_context_data(self, **kwargs):
                context = super(TestWizard, self).get_context_data(**kwargs)
                context['test_key'] = 'test_value'
                return context

        factory = RequestFactory()
        view = TestWizard.as_view([forms.Form])

        response = view(factory.get('/'))
        self.assertEquals(response.context_data['test_key'], 'test_value')

    def test_get_context_data_with_mixin(self):
        class AnotherMixin(object):
            def get_context_data(self, **kwargs):
                context = super(AnotherMixin, self).get_context_data(**kwargs)
                context['another_key'] = 'another_value'
                return context

        class TestWizard(AnotherMixin, CookieWizardView):
            """
            A subclass that implements ``get_context_data`` using the standard
            protocol for generic views (accept only **kwargs).

            See ticket #17148.
            """
            def get_context_data(self, **kwargs):
                context = super(TestWizard, self).get_context_data(**kwargs)
                context['test_key'] = 'test_value'
                return context

        factory = RequestFactory()

        view = TestWizard.as_view([forms.Form])

        response = view(factory.get('/'))
        self.assertEquals(response.context_data['test_key'], 'test_value')
        self.assertEquals(response.context_data['another_key'], 'another_value')
+4 −5
Original line number Diff line number Diff line
@@ -497,7 +497,7 @@ class WizardView(TemplateView):
            step = self.steps.current
        return self.get_form_list().keyOrder.index(step)

    def get_context_data(self, form, *args, **kwargs):
    def get_context_data(self, form, **kwargs):
        """
        Returns the template context for a step. You can overwrite this method
        to add more data for all or some steps. This method returns a
@@ -514,12 +514,12 @@ class WizardView(TemplateView):

            class MyWizard(FormWizard):
                def get_context_data(self, form, **kwargs):
                    context = super(MyWizard, self).get_context_data(form, **kwargs)
                    context = super(MyWizard, self).get_context_data(form=form, **kwargs)
                    if self.steps.current == 'my_step_name':
                        context.update({'another_var': True})
                    return context
        """
        context = super(WizardView, self).get_context_data(*args, **kwargs)
        context = super(WizardView, self).get_context_data(**kwargs)
        context.update(self.storage.extra_data)
        context['wizard'] = {
            'form': form,
@@ -535,7 +535,7 @@ class WizardView(TemplateView):
        Returns a ``HttpResponse`` containing all needed context data.
        """
        form = form or self.get_form()
        context = self.get_context_data(form, **kwargs)
        context = self.get_context_data(form=form, **kwargs)
        return self.render_to_response(context)

    def done(self, form_list, **kwargs):
@@ -683,4 +683,3 @@ class NamedUrlCookieWizardView(NamedUrlWizardView):
    A NamedUrlFormWizard with pre-configured CookieStorageBackend.
    """
    storage_name = 'django.contrib.formtools.wizard.storage.cookie.CookieStorage'
+2 −2
Original line number Diff line number Diff line
@@ -309,7 +309,7 @@ Advanced ``WizardView`` methods
    Example to add extra variables for a specific step::

        def get_context_data(self, form, **kwargs):
            context = super(MyWizard, self).get_context_data(form, **kwargs)
            context = super(MyWizard, self).get_context_data(form=form, **kwargs)
            if self.steps.current == 'my_step_name':
                context.update({'another_var': True})
            return context
@@ -436,7 +436,7 @@ Advanced ``WizardView`` methods

        def render(self, form=None, **kwargs):
            form = form or self.get_form()
            context = self.get_context_data(form, **kwargs)
            context = self.get_context_data(form=form, **kwargs)
            return self.render_to_response(context)

Providing initial data for the forms