Fixes ticket #2251. Implemented is_multipart on EditHandler and created tests.

This commit is contained in:
Wietze Helmantel 2017-03-24 13:33:27 +01:00 committed by Matt Westcott
parent 78a2e8ae96
commit a6bb67f75d
6 changed files with 50 additions and 2 deletions

View file

@ -44,6 +44,7 @@ Changelog
* Fix: Default avatar no longer visible when using a transparent gravatar image (Thijs Kramer)
* Fix: Scrolling within the datetime picker is now usable again for touchpads (Ralph Jacobs)
* Fix: List-based fields within form builder form submissions are now displayed as comma-separated strings rather than as Python lists (Christine Ho, Matt Westcott)
* Fix: Ensure that page editor forms are submitted as multipart when file fields exist in InlinePanels (Wietze Helmantel)
1.9 (16.02.2017)

View file

@ -56,6 +56,7 @@ Bug fixes
* Default avatar no longer visible when using a transparent gravatar image (Thijs Kramer)
* Scrolling within the datetime picker is now usable again for touchpads (Ralph Jacobs)
* List-based fields within form builder form submissions are now displayed as comma-separated strings rather than as Python lists (Christine Ho, Matt Westcott)
* Ensure that page editor forms are submitted as multipart when file fields exist in InlinePanels (Wietze Helmantel)
Upgrade considerations

View file

@ -163,6 +163,19 @@ class EditHandler(object):
"""
return ""
def is_multipart(self):
""" Checks form's *and* formsets' is_multipart method """
multipart = False
if self.form.is_multipart():
multipart = True
else:
for k in self.form.formsets.keys():
if self.form.formsets[k].is_multipart():
multipart = True
break
return multipart
def render_as_object(self):
"""
Render this object as it should appear within an ObjectList. Should not

View file

@ -18,7 +18,7 @@
</div>
</header>
<form id="page-edit-form" action="{% url 'wagtailadmin_pages:add' content_type.app_label content_type.model parent_page.id %}" method="POST" novalidate{% if form.is_multipart %} enctype="multipart/form-data"{% endif %}>
<form id="page-edit-form" action="{% url 'wagtailadmin_pages:add' content_type.app_label content_type.model parent_page.id %}" method="POST" novalidate{% if edit_handler.is_multipart %} enctype="multipart/form-data"{% endif %}>
{% csrf_token %}
<input type="hidden" name="next" value="{{ next }}">
{{ edit_handler.render_form_content }}

View file

@ -26,7 +26,7 @@
</div>
</header>
<form id="page-edit-form" action="{% url 'wagtailadmin_pages:edit' page.id %}" method="POST" novalidate{% if form.is_multipart %} enctype="multipart/form-data"{% endif %}>
<form id="page-edit-form" action="{% url 'wagtailadmin_pages:edit' page.id %}" method="POST" novalidate{% if edit_handler.is_multipart %} enctype="multipart/form-data"{% endif %}>
{% csrf_token %}
<input type="hidden" name="next" value="{{ next }}">

View file

@ -802,3 +802,36 @@ class TestInlinePanel(TestCase, WagtailTestUtils):
with self.ignore_deprecation_warnings():
self.assertRaises(TypeError, lambda: InlinePanel(label="Speakers"))
self.assertRaises(TypeError, lambda: InlinePanel(EventPage, 'speakers', label="Speakers", bacon="chunky"))
def test_is_multipart(self):
"""
Check whether is_multipart returns True when an InlinePanel contains
a FileInput and False otherwise
"""
SpeakerObjectList = ObjectList([
InlinePanel('speakers', label="Speakers", panels=[
FieldPanel('first_name', widget=forms.FileInput),
]),
]).bind_to_model(EventPage)
SpeakerInlinePanel = SpeakerObjectList.children[0]
EventPageForm = SpeakerObjectList.get_form_class(EventPage)
event_page = EventPage.objects.get(slug='christmas')
form = EventPageForm(instance=event_page)
panel = SpeakerInlinePanel(instance=event_page, form=form)
self.assertTrue(panel.is_multipart())
SpeakerObjectList = ObjectList([
InlinePanel('speakers', label="Speakers", panels=[
FieldPanel('first_name', widget=forms.Textarea),
]),
]).bind_to_model(EventPage)
SpeakerInlinePanel = SpeakerObjectList.children[0]
EventPageForm = SpeakerObjectList.get_form_class(EventPage)
event_page = EventPage.objects.get(slug='christmas')
form = EventPageForm(instance=event_page)
panel = SpeakerInlinePanel(instance=event_page, form=form)
self.assertFalse(panel.is_multipart())