From 70966cb9c7ed97f76be6781e502d08f58a4a0847 Mon Sep 17 00:00:00 2001 From: Jacob Kaplan-Moss Date: Tue, 26 Aug 2008 21:33:56 +0000 Subject: [PATCH] Fixed #6893: `FormWizard` now properly updates its `step` value. Thanks, siddhi and wamberg. git-svn-id: http://code.djangoproject.com/svn/django/trunk@8603 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/formtools/tests.py | 43 +++++++++++++++++++++++++++++- django/contrib/formtools/wizard.py | 4 +-- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/django/contrib/formtools/tests.py b/django/contrib/formtools/tests.py index b1e2cb7c76..5263e6e598 100644 --- a/django/contrib/formtools/tests.py +++ b/django/contrib/formtools/tests.py @@ -1,5 +1,5 @@ from django import forms -from django.contrib.formtools import preview +from django.contrib.formtools import preview, wizard from django import http from django.test import TestCase @@ -101,3 +101,44 @@ class PreviewTests(TestCase): response = self.client.post('/test1/', self.test_data) self.assertEqual(response.content, success_string) +# +# FormWizard tests +# + +class WizardPageOneForm(forms.Form): + field = forms.CharField() + +class WizardPageTwoForm(forms.Form): + field = forms.CharField() + +class WizardClass(wizard.FormWizard): + def render_template(self, *args, **kw): + return "" + + def done(self, request, cleaned_data): + return http.HttpResponse(success_string) + +class DummyRequest(object): + def __init__(self, POST=None): + self.method = POST and "POST" or "GET" + self.POST = POST + +class WizardTests(TestCase): + def test_step_starts_at_zero(self): + """ + step should be zero for the first form + """ + wizard = WizardClass([WizardPageOneForm, WizardPageTwoForm]) + request = DummyRequest() + wizard(request) + self.assertEquals(0, wizard.step) + + def test_step_increments(self): + """ + step should be incremented when we go to the next page + """ + wizard = WizardClass([WizardPageOneForm, WizardPageTwoForm]) + request = DummyRequest(POST={"0-field":"test", "wizard_step":"0"}) + response = wizard(request) + self.assertEquals(1, wizard.step) + diff --git a/django/contrib/formtools/wizard.py b/django/contrib/formtools/wizard.py index f764ddd0da..cb64fba537 100644 --- a/django/contrib/formtools/wizard.py +++ b/django/contrib/formtools/wizard.py @@ -92,7 +92,7 @@ class FormWizard(object): # Otherwise, move along to the next step. else: form = self.get_form(next_step) - current_step = next_step + self.step = current_step = next_step return self.render(form, request, current_step) @@ -203,7 +203,7 @@ class FormWizard(object): """ context = context or {} context.update(self.extra_context) - return render_to_response(self.get_template(self.step), dict(context, + return render_to_response(self.get_template(step), dict(context, step_field=self.step_field_name, step0=step, step=step + 1,