Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #17677 -- Made sure the WizardView doesn't accidentally overwri…

…te the `instance` or `queryset` form parameters when they are already set by the `get_form_kwargs` method. Thanks to goodtune and Stephan Jäkel.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17651 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 59a436e963db9018a930b9b5c83785bdc2ab0139 1 parent 60a62bf
Jannis Leidel authored March 04, 2012
1  django/contrib/formtools/tests/wizard/__init__.py
@@ -15,4 +15,5 @@
15 15
     CookieWizardTests,
16 16
     WizardTestKwargs,
17 17
     WizardTestGenericViewInterface,
  18
+    WizardFormKwargsOverrideTests,
18 19
 )
9  django/contrib/formtools/tests/wizard/wizardtests/forms.py
@@ -2,8 +2,10 @@
2 2
 import tempfile
3 3
 
4 4
 from django import forms
  5
+from django.contrib.auth.models import User
5 6
 from django.core.files.storage import FileSystemStorage
6 7
 from django.forms.formsets import formset_factory
  8
+from django.forms.models import modelformset_factory
7 9
 from django.http import HttpResponse
8 10
 from django.template import Template, Context
9 11
 
@@ -50,6 +52,13 @@ def get_context_data(self, form, **kwargs):
50 52
             context.update({'another_var': True})
51 53
         return context
52 54
 
  55
+class UserForm(forms.ModelForm):
  56
+    class Meta:
  57
+        model = User
  58
+        fields = ('username', 'email')
  59
+
  60
+UserFormSet = modelformset_factory(User, form=UserForm)
  61
+
53 62
 class SessionContactWizard(ContactWizard):
54 63
     storage_name = 'django.contrib.formtools.wizard.storage.session.SessionStorage'
55 64
 
46  django/contrib/formtools/tests/wizard/wizardtests/tests.py
@@ -7,6 +7,7 @@
7 7
 from django.conf import settings
8 8
 from django.contrib.auth.models import User
9 9
 from django.contrib.formtools.wizard.views import CookieWizardView
  10
+from django.contrib.formtools.tests.wizard.forms import UserForm, UserFormSet
10 11
 
11 12
 
12 13
 class WizardTests(object):
@@ -331,3 +332,48 @@ def get_context_data(self, **kwargs):
331 332
         response = view(factory.get('/'))
332 333
         self.assertEquals(response.context_data['test_key'], 'test_value')
333 334
         self.assertEquals(response.context_data['another_key'], 'another_value')
  335
+
  336
+
  337
+class WizardFormKwargsOverrideTests(TestCase):
  338
+    def setUp(self):
  339
+        super(WizardFormKwargsOverrideTests, self).setUp()
  340
+        self.rf = RequestFactory()
  341
+
  342
+        # Create two users so we can filter by is_staff when handing our
  343
+        # wizard a queryset keyword argument.
  344
+        self.normal_user = User.objects.create(username='test1', email='normal@example.com')
  345
+        self.staff_user = User.objects.create(username='test2', email='staff@example.com', is_staff=True)
  346
+
  347
+    def test_instance_is_maintained(self):
  348
+        self.assertEqual(2, User.objects.count())
  349
+        queryset = User.objects.get(pk=self.staff_user.pk)
  350
+
  351
+        class InstanceOverrideWizard(CookieWizardView):
  352
+            def get_form_kwargs(self, step):
  353
+                return {'instance': queryset}
  354
+
  355
+        view = InstanceOverrideWizard.as_view([UserForm])
  356
+        response = view(self.rf.get('/'))
  357
+
  358
+        form = response.context_data['wizard']['form']
  359
+
  360
+        self.assertNotEqual(form.instance.pk, None)
  361
+        self.assertEqual(form.instance.pk, self.staff_user.pk)
  362
+        self.assertEqual('staff@example.com', form.initial.get('email', None))
  363
+
  364
+    def test_queryset_is_maintained(self):
  365
+        queryset = User.objects.filter(pk=self.staff_user.pk)
  366
+
  367
+        class QuerySetOverrideWizard(CookieWizardView):
  368
+            def get_form_kwargs(self, step):
  369
+                return {'queryset': queryset}
  370
+
  371
+        view = QuerySetOverrideWizard.as_view([UserFormSet])
  372
+        response = view(self.rf.get('/'))
  373
+
  374
+        formset = response.context_data['wizard']['form']
  375
+
  376
+        self.assertNotEqual(formset.queryset, None)
  377
+        self.assertEqual(formset.initial_form_count(), 1)
  378
+        self.assertEqual(['staff@example.com'],
  379
+            list(formset.queryset.values_list('email', flat=True)))
10  django/contrib/formtools/wizard/views.py
@@ -385,11 +385,13 @@ def get_form(self, step=None, data=None, files=None):
385 385
             'initial': self.get_form_initial(step),
386 386
         })
387 387
         if issubclass(self.form_list[step], forms.ModelForm):
388  
-            # If the form is based on ModelForm, add instance if available.
389  
-            kwargs.update({'instance': self.get_form_instance(step)})
  388
+            # If the form is based on ModelForm, add instance if available
  389
+            # and not previously set.
  390
+            kwargs.setdefault('instance', self.get_form_instance(step))
390 391
         elif issubclass(self.form_list[step], forms.models.BaseModelFormSet):
391  
-            # If the form is based on ModelFormSet, add queryset if available.
392  
-            kwargs.update({'queryset': self.get_form_instance(step)})
  392
+            # If the form is based on ModelFormSet, add queryset if available
  393
+            # and not previous set.
  394
+            kwargs.setdefault('queryset', self.get_form_instance(step))
393 395
         return self.form_list[step](**kwargs)
394 396
 
395 397
     def process_step(self, form):

0 notes on commit 59a436e

Please sign in to comment.
Something went wrong with that request. Please try again.