diff --git a/wagtail/contrib/forms/templates/wagtailforms/confirm_delete.html b/wagtail/contrib/forms/templates/wagtailforms/confirm_delete.html index ba68b3276d..90a380cca9 100644 --- a/wagtail/contrib/forms/templates/wagtailforms/confirm_delete.html +++ b/wagtail/contrib/forms/templates/wagtailforms/confirm_delete.html @@ -17,6 +17,7 @@

{% csrf_token %} +
diff --git a/wagtail/contrib/forms/templates/wagtailforms/list_submissions.html b/wagtail/contrib/forms/templates/wagtailforms/list_submissions.html index 3f7e5c969e..9b2b4e4b12 100644 --- a/wagtail/contrib/forms/templates/wagtailforms/list_submissions.html +++ b/wagtail/contrib/forms/templates/wagtailforms/list_submissions.html @@ -2,6 +2,7 @@ {% load i18n %} {% block results %}
+ diff --git a/wagtail/contrib/forms/tests/test_views.py b/wagtail/contrib/forms/tests/test_views.py index e2e11c2b7f..14828149b5 100644 --- a/wagtail/contrib/forms/tests/test_views.py +++ b/wagtail/contrib/forms/tests/test_views.py @@ -481,10 +481,8 @@ class TestFormsSubmissionsList(WagtailTestUtils, TestCase): def test_list_submissions_pagination(self): self.make_list_submissions() - response = self.client.get( - reverse("wagtailforms:list_submissions", args=(self.form_page.id,)), - {"p": 2}, - ) + list_url = reverse("wagtailforms:list_submissions", args=(self.form_page.id,)) + response = self.client.get(list_url, {"p": 2}) # Check response self.assertEqual(response.status_code, 200) @@ -493,6 +491,18 @@ class TestFormsSubmissionsList(WagtailTestUtils, TestCase): # Check that we got the correct page self.assertEqual(response.context["page_obj"].number, 2) + # The delete form should have a 'next' input that points back to the list page + # with the same pagination querystring + soup = self.get_soup(response.content) + delete_url = reverse( + "wagtailforms:delete_submissions", args=(self.form_page.id,) + ) + form = soup.find("form", {"action": delete_url}) + self.assertIsNotNone(form) + next_input = form.find("input", {"name": "next"}) + self.assertIsNotNone(next_input) + self.assertEqual(next_input.get("value"), f"{list_url}?p=2") + def test_list_submissions_pagination_invalid(self): self.make_list_submissions() @@ -1184,10 +1194,8 @@ class TestCustomFormsSubmissionsList(WagtailTestUtils, TestCase): def test_list_submissions_pagination(self): self.make_list_submissions() - response = self.client.get( - reverse("wagtailforms:list_submissions", args=(self.form_page.id,)), - {"p": 2}, - ) + list_url = reverse("wagtailforms:list_submissions", args=(self.form_page.id,)) + response = self.client.get(list_url, {"p": 2}) # Check response self.assertEqual(response.status_code, 200) @@ -1202,6 +1210,18 @@ class TestCustomFormsSubmissionsList(WagtailTestUtils, TestCase): ) self.assertContains(response, "generated-username-", count=20) + # The delete form should have a 'next' input that points back to the list page + # with the same pagination querystring + soup = self.get_soup(response.content) + delete_url = reverse( + "wagtailforms:delete_submissions", args=(self.form_page.id,) + ) + form = soup.find("form", {"action": delete_url}) + self.assertIsNotNone(form) + next_input = form.find("input", {"name": "next"}) + self.assertIsNotNone(next_input) + self.assertEqual(next_input.get("value"), f"{list_url}?p=2") + def test_list_submissions_pagination_invalid(self): self.make_list_submissions() @@ -1327,6 +1347,18 @@ class TestDeleteFormSubmission(WagtailTestUtils, TestCase): reverse("wagtailforms:list_submissions", args=(self.form_page.id,)), ) + def test_delete_submission_with_next_url(self): + list_url = reverse("wagtailforms:list_submissions", args=(self.form_page.id,)) + next_url = list_url + "?p=2" + response = self.client.post( + reverse("wagtailforms:delete_submissions", args=(self.form_page.id,)), + { + "selected-submissions": FormSubmission.objects.first().id, + "next": next_url, + }, + ) + self.assertRedirects(response, next_url) + class TestDeleteCustomFormSubmission(WagtailTestUtils, TestCase): fixtures = ["test.json"] @@ -1521,10 +1553,8 @@ class TestFormsWithCustomSubmissionsList(WagtailTestUtils, TestCase): def test_list_submissions_pagination(self): self.make_list_submissions() - response = self.client.get( - reverse("wagtailforms:list_submissions", args=(self.form_page.id,)), - {"p": 2}, - ) + list_url = reverse("wagtailforms:list_submissions", args=(self.form_page.id,)) + response = self.client.get(list_url, {"p": 2}) # Check response self.assertEqual(response.status_code, 200) @@ -1535,6 +1565,18 @@ class TestFormsWithCustomSubmissionsList(WagtailTestUtils, TestCase): self.assertContains(response, "Wet my pants excited!", count=50) self.assertEqual(response.context["page_obj"].number, 2) + # The delete form should have a 'next' input that points back to the list page + # with the same pagination querystring + soup = self.get_soup(response.content) + delete_url = reverse( + "wagtailforms:delete_submissions", args=(self.form_page.id,) + ) + form = soup.find("form", {"action": delete_url}) + self.assertIsNotNone(form) + next_input = form.find("input", {"name": "next"}) + self.assertIsNotNone(next_input) + self.assertEqual(next_input.get("value"), f"{list_url}?p=2") + def test_list_submissions_csv_export(self): response = self.client.get( reverse("wagtailforms:list_submissions", args=(self.form_page.id,)), diff --git a/wagtail/contrib/forms/views.py b/wagtail/contrib/forms/views.py index 901587c23c..faf81aa2e9 100644 --- a/wagtail/contrib/forms/views.py +++ b/wagtail/contrib/forms/views.py @@ -12,6 +12,7 @@ from django_filters import DateFromToRangeFilter from wagtail.admin import messages from wagtail.admin.filters import DateRangePickerWidget, WagtailFilterSet from wagtail.admin.ui.tables import Column, TitleColumn +from wagtail.admin.utils import get_valid_next_url_from_request from wagtail.admin.views import generic from wagtail.admin.views.generic.base import BaseListingView from wagtail.admin.views.mixins import SpreadsheetExportMixin @@ -111,6 +112,9 @@ class DeleteSubmissionsView(TemplateView): def get_success_url(self): """Returns the success URL to redirect to after a successful deletion""" + next_url = get_valid_next_url_from_request(self.request) + if next_url: + return next_url return self.success_url def dispatch(self, request, *args, **kwargs): @@ -140,6 +144,9 @@ class DeleteSubmissionsView(TemplateView): "submissions": self.submissions, } ) + next_url = get_valid_next_url_from_request(self.request) + if next_url: + context["next_url"] = next_url return context @@ -319,4 +326,5 @@ class SubmissionsListView(SpreadsheetExportMixin, BaseListingView): } ) + context["next_url"] = self.request.get_full_path() return context