Ticket #17677: 17677-2.diff

File 17677-2.diff, 5.2 KB (added by steph, 4 years ago)

Updated patch (some test changes)

  • django/contrib/formtools/tests/wizard/__init__.py

    diff --git a/django/contrib/formtools/tests/wizard/__init__.py b/django/contrib/formtools/tests/wizard/__init__.py
    index 732c02f..a2a9692 100644
    a b from django.contrib.formtools.tests.wizard.wizardtests.tests import ( 
    1515    CookieWizardTests,
    1616    WizardTestKwargs,
    1717    WizardTestGenericViewInterface,
     18    WizardFormKwargsOverrideTests,
    1819)
  • django/contrib/formtools/tests/wizard/wizardtests/forms.py

    diff --git a/django/contrib/formtools/tests/wizard/wizardtests/forms.py b/django/contrib/formtools/tests/wizard/wizardtests/forms.py
    index 9013e89..6a81329 100644
    a b import os 
    22import tempfile
    33
    44from django import forms
     5from django.contrib.auth.models import User
    56from django.core.files.storage import FileSystemStorage
    67from django.forms.formsets import formset_factory
     8from django.forms.models import modelformset_factory
    79from django.http import HttpResponse
    810from django.template import Template, Context
    911
    class ContactWizard(WizardView): 
    5052            context.update({'another_var': True})
    5153        return context
    5254
     55class UserForm(forms.ModelForm):
     56    class Meta:
     57        model = User
     58        fields = ('username', 'email')
     59
     60UserFormSet = modelformset_factory(User, form=UserForm)
     61
    5362class SessionContactWizard(ContactWizard):
    5463    storage_name = 'django.contrib.formtools.wizard.storage.session.SessionStorage'
    5564
  • django/contrib/formtools/tests/wizard/wizardtests/tests.py

    diff --git a/django/contrib/formtools/tests/wizard/wizardtests/tests.py b/django/contrib/formtools/tests/wizard/wizardtests/tests.py
    index 592065e..862ef3d 100644
    a b from django.test.client import RequestFactory 
    77from django.conf import settings
    88from django.contrib.auth.models import User
    99from django.contrib.formtools.wizard.views import CookieWizardView
     10from django.contrib.formtools.tests.wizard.forms import UserForm, UserFormSet
    1011
    1112
    1213class WizardTests(object):
    class WizardTestGenericViewInterface(TestCase): 
    331332        response = view(factory.get('/'))
    332333        self.assertEquals(response.context_data['test_key'], 'test_value')
    333334        self.assertEquals(response.context_data['another_key'], 'another_value')
     335
     336
     337class WizardFormKwargsOverrideTests(TestCase):
     338    def setUp(self):
     339        super(WizardFormKwargsOverrideTests, self).setUp()
     340
     341        # Create two users so we can filter by is_staff when handing our
     342        # wizard a queryset keyword argument.
     343        User.objects.create(username='test1', email='test1@example.com')
     344        User.objects.create(username='test2', email='test2@example.com',
     345            is_staff=True)
     346
     347    def test_instance_is_maintained(self):
     348        class InstanceOverrideWizard(CookieWizardView):
     349            def get_form_kwargs(self, step):
     350                return {'instance': User.objects.get(is_staff=True)}
     351
     352        factory = RequestFactory()
     353
     354        view = InstanceOverrideWizard.as_view([UserForm])
     355        response = view(factory.get('/'))
     356
     357        form = response.context_data['wizard']['form']
     358
     359        self.assertNotEqual(form.instance.pk, None)
     360        self.assertEqual('test2@example.com', form.initial.get('email', None))
     361
     362    def test_queryset_is_maintained(self):
     363        class QuerySetOverrideWizard(CookieWizardView):
     364            def get_form_kwargs(self, step):
     365                return {'queryset': User.objects.filter(is_staff=True)}
     366
     367        factory = RequestFactory()
     368
     369        view = QuerySetOverrideWizard.as_view([UserFormSet])
     370        response = view(factory.get('/'))
     371
     372        formset = response.context_data['wizard']['form']
     373
     374        self.assertNotEqual(formset.queryset, None)
     375        self.assertEqual(1, formset.initial_form_count())
     376        self.assertEqual(['test2@example.com'],
     377            list(formset.queryset.values_list('email', flat=True)))
  • django/contrib/formtools/wizard/views.py

    diff --git a/django/contrib/formtools/wizard/views.py b/django/contrib/formtools/wizard/views.py
    index 06a0398..4372c8a 100644
    a b class WizardView(TemplateView): 
    385385            'initial': self.get_form_initial(step),
    386386        })
    387387        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))
    390391        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))
    393395        return self.form_list[step](**kwargs)
    394396
    395397    def process_step(self, form):
Back to Top