From 0cf00769ad11fa5ff0dff585d7f491a80f3e45ef Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Wed, 8 Nov 2017 01:06:32 +0100 Subject: [PATCH] Fixed #28585 -- Calculated admin's change form multipart context variable from forms and formsets Thanks Tim Graham for the review. --- django/contrib/admin/options.py | 5 ++++- tests/admin_views/tests.py | 18 +++++++++++++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py index 6e4ad180ac..545c8a53cd 100644 --- a/django/contrib/admin/options.py +++ b/django/contrib/admin/options.py @@ -1061,7 +1061,10 @@ class ModelAdmin(BaseModelAdmin): 'has_add_permission': self.has_add_permission(request), 'has_change_permission': self.has_change_permission(request, obj), 'has_delete_permission': self.has_delete_permission(request, obj), - 'has_file_field': True, # FIXME - this should check if form or formsets have a FileField, + 'has_file_field': context['adminform'].form.is_multipart() or any( + admin_formset.formset.form().is_multipart() + for admin_formset in context['inline_admin_formsets'] + ), 'has_absolute_url': view_on_site_url is not None, 'absolute_url': view_on_site_url, 'form_url': form_url, diff --git a/tests/admin_views/tests.py b/tests/admin_views/tests.py index b7c405f271..2a5ab9b55d 100644 --- a/tests/admin_views/tests.py +++ b/tests/admin_views/tests.py @@ -56,10 +56,11 @@ from .models import ( UnorderedObject, Villain, Vodcast, Whatsit, Widget, Worker, WorkHour, ) - ERROR_MESSAGE = "Please enter the correct username and password \ for a staff account. Note that both fields may be case-sensitive." +MULTIPART_ENCTYPE = 'enctype="multipart/form-data"' + class AdminFieldExtractionMixin: """ @@ -3522,6 +3523,13 @@ class AdminInlineFileUploadTest(TestCase): self.picture = Picture(name="Test Picture", image=filename, gallery=self.gallery) self.picture.save() + def test_form_has_multipart_enctype(self): + response = self.client.get( + reverse('admin:admin_views_gallery_change', args=(self.gallery.id,)) + ) + self.assertIs(response.context['has_file_field'], True) + self.assertContains(response, MULTIPART_ENCTYPE) + def test_inline_file_upload_edit_validation_error_post(self): """ Inline file uploads correctly display prior data (#10002). @@ -3658,6 +3666,10 @@ class AdminInlineTests(TestCase): response = self.client.get(collector_url) self.assertContains(response, 'name="widget_set-0-id"') + # No file or image fields, no enctype on the forms + self.assertIs(response.context['has_file_field'], False) + self.assertNotContains(response, MULTIPART_ENCTYPE) + # Now resave that inline self.post_data['widget_set-INITIAL_FORMS'] = "1" self.post_data['widget_set-0-id'] = str(widget_id) @@ -5451,7 +5463,7 @@ class AdminKeepChangeListFiltersTests(TestCase): # Check the form action. form_action = re.search( - '
', + '', force_text(response.content) ) self.assertURLEqual(form_action.group(1), '?%s' % self.get_preserved_filters_querystring()) @@ -5515,7 +5527,7 @@ class AdminKeepChangeListFiltersTests(TestCase): # Check the form action. form_action = re.search( - '', + '', force_text(response.content) ) self.assertURLEqual(form_action.group(1), '?%s' % self.get_preserved_filters_querystring())