From 3e790ae63266bee48ad659ddedc792219ee33a0e Mon Sep 17 00:00:00 2001 From: Julien Phalip Date: Mon, 19 Dec 2011 12:11:12 +0000 Subject: [PATCH] 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 --- .../formtools/tests/wizard/__init__.py | 1 + .../tests/wizard/wizardtests/tests.py | 51 +++++++++++++++++++ django/contrib/formtools/wizard/views.py | 9 ++-- docs/ref/contrib/formtools/form-wizard.txt | 4 +- 4 files changed, 58 insertions(+), 7 deletions(-) diff --git a/django/contrib/formtools/tests/wizard/__init__.py b/django/contrib/formtools/tests/wizard/__init__.py index dce2fedfe0..7b15fb5059 100644 --- a/django/contrib/formtools/tests/wizard/__init__.py +++ b/django/contrib/formtools/tests/wizard/__init__.py @@ -14,4 +14,5 @@ from django.contrib.formtools.tests.wizard.wizardtests.tests import ( SessionWizardTests, CookieWizardTests, WizardTestKwargs, + WizardTestGenericViewInterface, ) diff --git a/django/contrib/formtools/tests/wizard/wizardtests/tests.py b/django/contrib/formtools/tests/wizard/wizardtests/tests.py index 9868721ca2..3eb77cb648 100644 --- a/django/contrib/formtools/tests/wizard/wizardtests/tests.py +++ b/django/contrib/formtools/tests/wizard/wizardtests/tests.py @@ -1,9 +1,12 @@ 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') diff --git a/django/contrib/formtools/wizard/views.py b/django/contrib/formtools/wizard/views.py index 15ba14695b..0e1486c0fa 100644 --- a/django/contrib/formtools/wizard/views.py +++ b/django/contrib/formtools/wizard/views.py @@ -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' - diff --git a/docs/ref/contrib/formtools/form-wizard.txt b/docs/ref/contrib/formtools/form-wizard.txt index aadd7d276f..4c071c155f 100644 --- a/docs/ref/contrib/formtools/form-wizard.txt +++ b/docs/ref/contrib/formtools/form-wizard.txt @@ -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