From a42893f49791c75474426a81ce3ec98a3458d1f3 Mon Sep 17 00:00:00 2001 From: LB Date: Tue, 29 Aug 2017 15:11:22 +0200 Subject: [PATCH] FormSubmissionsPanel to use get_submission_class --- CHANGELOG.txt | 1 + docs/releases/2.0.rst | 1 + wagtail/contrib/forms/edit_handlers.py | 5 ++- wagtail/contrib/forms/tests/test_views.py | 47 ++++++++++++++++++++++- 4 files changed, 51 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 5b5c76d4a..1e02fc319 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -32,6 +32,7 @@ Changelog * Fix: Fixed incorrect z-index on userbar causing it to appear behind page content (Stein Strindhaug) * Fix: Form submissions pagination no longer looses date filter when changing page (Bertrand Bordage) * Fix: PostgreSQL search backend now removes duplicate page instances from the database (Bertrand Bordage) + * Fix: `FormSubmissionsPanel` now recognises custom form submission classes (LB (Ben Johnston)) 1.13.1 (17.11.2017) diff --git a/docs/releases/2.0.rst b/docs/releases/2.0.rst index a8d141527..608dc4b0e 100644 --- a/docs/releases/2.0.rst +++ b/docs/releases/2.0.rst @@ -50,6 +50,7 @@ Bug fixes * Fixed incorrect z-index on userbar causing it to appear behind page content (Stein Strindhaug) * Form submissions pagination no longer looses date filter when changing page (Bertrand Bordage) * PostgreSQL search backend now removes duplicate page instances from the database (Bertrand Bordage) + * ``FormSubmissionsPanel`` now recognises custom form submission classes (LB (Ben Johnston)) Upgrade considerations diff --git a/wagtail/contrib/forms/edit_handlers.py b/wagtail/contrib/forms/edit_handlers.py index 4146421b5..886d907cb 100644 --- a/wagtail/contrib/forms/edit_handlers.py +++ b/wagtail/contrib/forms/edit_handlers.py @@ -9,8 +9,9 @@ class BaseFormSubmissionsPanel(EditHandler): template = "wagtailforms/edit_handlers/form_responses_panel.html" def render(self): - from .models import FormSubmission - submissions = FormSubmission.objects.filter(page=self.instance) + form_page_model = self.model + form_submissions_model = form_page_model().get_submission_class() + submissions = form_submissions_model.objects.filter(page=self.instance) submission_count = submissions.count() if not submission_count: diff --git a/wagtail/contrib/forms/tests/test_views.py b/wagtail/contrib/forms/tests/test_views.py index c4838dabf..a522760e6 100644 --- a/wagtail/contrib/forms/tests/test_views.py +++ b/wagtail/contrib/forms/tests/test_views.py @@ -6,7 +6,8 @@ from django.test import TestCase from django.urls import reverse from wagtail.tests.testapp.models import ( - CustomFormPageSubmission, FormField, FormFieldWithCustomSubmission, FormPage) + CustomFormPageSubmission, FormField, FormFieldWithCustomSubmission, + FormPage, FormPageWithCustomSubmission) from wagtail.tests.utils import WagtailTestUtils from wagtail.admin.edit_handlers import get_form_for_model from wagtail.admin.forms import WagtailAdminPageForm @@ -50,6 +51,50 @@ class TestFormResponsesPanel(TestCase): self.assertEqual('', result) +class TestFormResponsesPanelWithCustomSubmissionClass(TestCase): + def setUp(self): + # Create a form page + self.form_page = make_form_page_with_custom_submission() + + self.FormPageForm = get_form_for_model( + FormPageWithCustomSubmission, form_class=WagtailAdminPageForm + ) + + self.test_user = get_user_model().objects.create_user( + username='user-n1kola', password='123') + + submissions_panel = FormSubmissionsPanel().bind_to_model(FormPageWithCustomSubmission) + + self.panel = submissions_panel(self.form_page, self.FormPageForm()) + + def test_render_with_submissions(self): + """Show the panel with the count of submission and a link to the list_submissions view.""" + new_form_submission = CustomFormPageSubmission.objects.create( + user=self.test_user, + page=self.form_page, + form_data=json.dumps({ + 'your-email': 'email@domain.com', + 'your-message': 'hi joe', + 'your-choices': {'foo': '', 'bar': '', 'baz': ''} + }), + ) + new_form_submission.submit_time = '2017-08-29T12:00:00.000Z' + new_form_submission.save() + + result = self.panel.render() + + url = reverse('wagtailforms:list_submissions', args=(self.form_page.id,)) + link = '1'.format(url) + + self.assertIn(link, result) + + def test_render_without_submissions(self): + """The panel should not be shown if the number of submission is zero.""" + result = self.panel.render() + + self.assertEqual('', result) + + class TestFormsIndex(TestCase, WagtailTestUtils): fixtures = ['test.json']