Skip to content

Commit

Permalink
Fixed #17148 -- Fixed the signature of `WizardView.get_context_data()…
Browse files Browse the repository at this point in the history
…` 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
  • Loading branch information
julien committed Dec 19, 2011
1 parent f394ab1 commit e52376c
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 7 deletions.
1 change: 1 addition & 0 deletions django/contrib/formtools/tests/wizard/__init__.py
Expand Up @@ -14,4 +14,5 @@
SessionWizardTests,
CookieWizardTests,
WizardTestKwargs,
WizardTestGenericViewInterface,
)
51 changes: 51 additions & 0 deletions 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):
Expand Down Expand Up @@ -280,3 +283,51 @@ def test_template(self):
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')
9 changes: 4 additions & 5 deletions django/contrib/formtools/wizard/views.py
Expand Up @@ -497,7 +497,7 @@ def get_step_index(self, step=None):
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
Expand All @@ -514,12 +514,12 @@ def get_context_data(self, form, *args, **kwargs):
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,
Expand All @@ -535,7 +535,7 @@ def render(self, form=None, **kwargs):
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):
Expand Down Expand Up @@ -683,4 +683,3 @@ class NamedUrlCookieWizardView(NamedUrlWizardView):
A NamedUrlFormWizard with pre-configured CookieStorageBackend.
"""
storage_name = 'django.contrib.formtools.wizard.storage.cookie.CookieStorage'

4 changes: 2 additions & 2 deletions docs/ref/contrib/formtools/form-wizard.txt
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e52376c

Please sign in to comment.