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
This commit is contained in:
Julien Phalip 2011-12-19 12:11:12 +00:00
parent 5a48cb5f61
commit 3e790ae632
4 changed files with 58 additions and 7 deletions

View File

@ -14,4 +14,5 @@ from django.contrib.formtools.tests.wizard.wizardtests.tests import (
SessionWizardTests, SessionWizardTests,
CookieWizardTests, CookieWizardTests,
WizardTestKwargs, WizardTestKwargs,
WizardTestGenericViewInterface,
) )

View File

@ -1,9 +1,12 @@
from __future__ import with_statement from __future__ import with_statement
import os import os
from django import forms
from django.test import TestCase from django.test import TestCase
from django.test.client import RequestFactory
from django.conf import settings from django.conf import settings
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.contrib.formtools.wizard.views import CookieWizardView
class WizardTests(object): class WizardTests(object):
@ -280,3 +283,51 @@ class WizardTestKwargs(TestCase):
TEMPLATE_DIRS=list(settings.TEMPLATE_DIRS) + [templates]): TEMPLATE_DIRS=list(settings.TEMPLATE_DIRS) + [templates]):
response = self.client.get(self.wizard_url) response = self.client.get(self.wizard_url)
self.assertTemplateUsed(response, 'other_wizard_form.html') 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')

View File

@ -497,7 +497,7 @@ class WizardView(TemplateView):
step = self.steps.current step = self.steps.current
return self.get_form_list().keyOrder.index(step) 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 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 to add more data for all or some steps. This method returns a
@ -514,12 +514,12 @@ class WizardView(TemplateView):
class MyWizard(FormWizard): class MyWizard(FormWizard):
def get_context_data(self, form, **kwargs): 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': if self.steps.current == 'my_step_name':
context.update({'another_var': True}) context.update({'another_var': True})
return context 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.update(self.storage.extra_data)
context['wizard'] = { context['wizard'] = {
'form': form, 'form': form,
@ -535,7 +535,7 @@ class WizardView(TemplateView):
Returns a ``HttpResponse`` containing all needed context data. Returns a ``HttpResponse`` containing all needed context data.
""" """
form = form or self.get_form() 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) return self.render_to_response(context)
def done(self, form_list, **kwargs): def done(self, form_list, **kwargs):
@ -683,4 +683,3 @@ class NamedUrlCookieWizardView(NamedUrlWizardView):
A NamedUrlFormWizard with pre-configured CookieStorageBackend. A NamedUrlFormWizard with pre-configured CookieStorageBackend.
""" """
storage_name = 'django.contrib.formtools.wizard.storage.cookie.CookieStorage' storage_name = 'django.contrib.formtools.wizard.storage.cookie.CookieStorage'

View File

@ -309,7 +309,7 @@ Advanced ``WizardView`` methods
Example to add extra variables for a specific step:: Example to add extra variables for a specific step::
def get_context_data(self, form, **kwargs): 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': if self.steps.current == 'my_step_name':
context.update({'another_var': True}) context.update({'another_var': True})
return context return context
@ -436,7 +436,7 @@ Advanced ``WizardView`` methods
def render(self, form=None, **kwargs): def render(self, form=None, **kwargs):
form = form or self.get_form() 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) return self.render_to_response(context)
Providing initial data for the forms Providing initial data for the forms