From 0424b42ab099192d18154dd63e883c9b3f26cb26 Mon Sep 17 00:00:00 2001 From: Mikael Lenander Date: Mon, 22 Jan 2024 16:37:46 +0200 Subject: [PATCH] Redirect back to form after adding or removing deviations Part of #1122 --- deviations/tests.py | 21 +++++++++------------ deviations/viewbase.py | 11 +++++------ deviations/views.py | 12 ++++++++++++ locale/en/LC_MESSAGES/django.po | 10 +++++++++- locale/fi/LC_MESSAGES/django.po | 10 +++++++++- 5 files changed, 44 insertions(+), 20 deletions(-) diff --git a/deviations/tests.py b/deviations/tests.py index 71f6335dc..77965c627 100644 --- a/deviations/tests.py +++ b/deviations/tests.py @@ -369,7 +369,6 @@ def test_get_deviation_groups(self): def test_add_deadline_deviations(self): self.client.login(username="staff", password="staffPassword") - list_deadline_deviations_url = self.course_instance.get_url("deviations-list-dl") add_deadline_deviations_url = self.course_instance.get_url("deviations-add-dl") # Module and user tag provided @@ -382,7 +381,7 @@ def test_add_deadline_deviations(self): } ) self.assertEqual(response.status_code, 302) - self.assertEqual(response.url, list_deadline_deviations_url) + self.assertEqual(response.url, add_deadline_deviations_url) deviation = DeadlineRuleDeviation.objects.get( exercise=self.module_2_exercise_1, @@ -418,7 +417,7 @@ def test_add_deadline_deviations(self): } ) self.assertEqual(response.status_code, 302) - self.assertEqual(response.url, list_deadline_deviations_url) + self.assertEqual(response.url, add_deadline_deviations_url) deviation = DeadlineRuleDeviation.objects.get( exercise=self.module_2_exercise_1, @@ -456,7 +455,7 @@ def test_add_deadline_deviations(self): } ) self.assertEqual(response.status_code, 302) - self.assertEqual(response.url, list_deadline_deviations_url) + self.assertEqual(response.url, add_deadline_deviations_url) deviation = DeadlineRuleDeviation.objects.get( exercise=self.module_2_exercise_1, @@ -490,7 +489,6 @@ def test_add_deadline_deviations(self): def test_override_deadline_deviations(self): self.client.login(username="staff", password="staffPassword") - list_deadline_deviations_url = self.course_instance.get_url("deviations-list-dl") add_deadline_deviations_url = self.course_instance.get_url("deviations-add-dl") override_deadline_deviations_url = self.course_instance.get_url("deviations-override-dl") @@ -504,7 +502,7 @@ def test_override_deadline_deviations(self): } ) self.assertEqual(response.status_code, 302) - self.assertEqual(response.url, list_deadline_deviations_url) + self.assertEqual(response.url, add_deadline_deviations_url) self.assertEqual( DeadlineRuleDeviation.objects.get( @@ -547,7 +545,7 @@ def test_override_deadline_deviations(self): } ) self.assertEqual(response.status_code, 302) - self.assertEqual(response.url, list_deadline_deviations_url) + self.assertEqual(response.url, add_deadline_deviations_url) self.assertEqual( DeadlineRuleDeviation.objects.get( @@ -583,7 +581,7 @@ def test_override_deadline_deviations(self): } ) self.assertEqual(response.status_code, 302) - self.assertEqual(response.url, list_deadline_deviations_url) + self.assertEqual(response.url, add_deadline_deviations_url) self.assertEqual( DeadlineRuleDeviation.objects.get( @@ -604,7 +602,6 @@ def test_override_deadline_deviations(self): def test_remove_deadline_deviations(self): self.client.login(username="staff", password="staffPassword") - list_deadline_deviations_url = self.course_instance.get_url("deviations-list-dl") add_deadline_deviations_url = self.course_instance.get_url("deviations-add-dl") remove_deadline_deviations_url = self.course_instance.get_url("deviations-remove-dl") @@ -618,7 +615,7 @@ def test_remove_deadline_deviations(self): } ) self.assertEqual(response.status_code, 302) - self.assertEqual(response.url, list_deadline_deviations_url) + self.assertEqual(response.url, add_deadline_deviations_url) self.assertEqual( DeadlineRuleDeviation.objects.get( @@ -644,7 +641,7 @@ def test_remove_deadline_deviations(self): } ) self.assertEqual(response.status_code, 302) - self.assertEqual(response.url, list_deadline_deviations_url) + self.assertEqual(response.url, remove_deadline_deviations_url) with self.assertRaises(DeadlineRuleDeviation.DoesNotExist): DeadlineRuleDeviation.objects.get( @@ -661,7 +658,7 @@ def test_remove_deadline_deviations(self): } ) self.assertEqual(response.status_code, 302) - self.assertEqual(response.url, list_deadline_deviations_url) + self.assertEqual(response.url, remove_deadline_deviations_url) with self.assertRaises(DeadlineRuleDeviation.DoesNotExist): DeadlineRuleDeviation.objects.get( diff --git a/deviations/viewbase.py b/deviations/viewbase.py index 666b0f0f9..ec829df6a 100644 --- a/deviations/viewbase.py +++ b/deviations/viewbase.py @@ -77,7 +77,8 @@ def form_valid(self, form: forms.BaseForm) -> HttpResponse: self.success_url = self.deviation_model.get_override_url(self.instance) self.request.session[self.session_key] = self.serialize_session_data(form.cleaned_data) else: - self.success_url = self.deviation_model.get_list_url(self.instance) + self.success_url = self.get_success_no_override_url() + for exercise in exercises: for submitter in submitters: new_deviation = self.deviation_model( @@ -87,7 +88,7 @@ def form_valid(self, form: forms.BaseForm) -> HttpResponse: ) new_deviation.update_by_form(form.cleaned_data) new_deviation.save() - + messages.success(self.request, _("SUCCESS_ADDING_DEVIATIONS")) return super().form_valid(form) def serialize_session_data(self, form_data: Dict[str, Any]) -> Dict[str, Any]: @@ -110,9 +111,6 @@ class OverrideDeviationsView(CourseInstanceMixin, BaseFormView): deviation_model: Type[SubmissionRuleDeviation] session_key: str - def get_success_url(self) -> str: - return self.deviation_model.get_list_url(self.instance) - def get_common_objects(self) -> None: super().get_common_objects() self.session_data = self.deserialize_session_data(self.request.session[self.session_key]) @@ -162,6 +160,7 @@ def form_valid(self, form: forms.BaseForm) -> HttpResponse: new_deviation.save() del self.request.session[self.session_key] + messages.success(self.request, _("SUCCESS_OVERRIDING_DEVIATIONS")) return super().form_valid(form) def deserialize_session_data(self, session_data: Dict[str, Any]) -> Dict[str, Any]: @@ -203,7 +202,7 @@ def get_form_kwargs(self) -> Dict[str, Any]: return kwargs def get_success_url(self) -> str: - return self.deviation_model.get_list_url(self.instance) + return self.instance.get_url('deviations-remove-dl') def form_valid(self, form: forms.BaseForm) -> HttpResponse: number_of_removed = 0 diff --git a/deviations/views.py b/deviations/views.py index db8e255ad..8a09aed0d 100644 --- a/deviations/views.py +++ b/deviations/views.py @@ -28,6 +28,9 @@ class AddDeadlinesView(AddDeviationsView): deviation_model = DeadlineRuleDeviation session_key = 'add-deviations-data-dl' + def get_success_no_override_url(self) -> str: + return self.instance.get_url('deviations-add-dl') + def get_initial_get_param_spec(self) -> Dict[str, Optional[Callable[[str], Any]]]: spec = super().get_initial_get_param_spec() spec.update({ @@ -52,6 +55,9 @@ class OverrideDeadlinesView(OverrideDeviationsView): deviation_model = DeadlineRuleDeviation session_key = 'add-deviations-data-dl' + def get_success_url(self) -> str: + return self.instance.get_url('deviations-add-dl') + def deserialize_session_data(self, session_data: Dict[str, Any]) -> Dict[str, Any]: result = super().deserialize_session_data(session_data) result.update({ @@ -83,6 +89,9 @@ class AddSubmissionsView(AddDeviationsView): deviation_model = MaxSubmissionsRuleDeviation session_key = 'add-deviations-data-submissions' + def get_success_no_override_url(self) -> str: + return self.instance.get_url('deviations-add-submissions') + def serialize_session_data(self, form_data: Dict[str, Any]) -> Dict[str, Any]: result = super().serialize_session_data(form_data) result['extra_submissions'] = form_data['extra_submissions'] @@ -94,6 +103,9 @@ class OverrideSubmissionsView(OverrideDeviationsView): deviation_model = MaxSubmissionsRuleDeviation session_key = 'add-deviations-data-submissions' + def get_success_url(self) -> str: + return self.instance.get_url('deviations-add-submissions') + def deserialize_session_data(self, session_data: Dict[str, Any]) -> Dict[str, Any]: result = super().deserialize_session_data(session_data) result['extra_submissions'] = session_data['extra_submissions'] diff --git a/locale/en/LC_MESSAGES/django.po b/locale/en/LC_MESSAGES/django.po index 638604969..74ec5c2c1 100644 --- a/locale/en/LC_MESSAGES/django.po +++ b/locale/en/LC_MESSAGES/django.po @@ -6,7 +6,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2023-10-27 20:01+0300\n" +"POT-Creation-Date: 2024-01-20 17:06+0200\n" "PO-Revision-Date: 2021-05-27 14:47+0300\n" "Last-Translator: Jimmy Ihalainen \n" "Language-Team: English<>\n" @@ -1845,11 +1845,19 @@ msgstr "Override" msgid "SUBMIT" msgstr "Submit" +#: deviations/viewbase.py +msgid "SUCCESS_ADDING_DEVIATIONS" +msgstr "Deviations were added succesfully." + #: deviations/viewbase.py #, python-brace-format msgid "INVALID_EXERCISE_OR_SUBMITTER_ID -- {id}" msgstr "Invalid assignment or submitter id: {id}" +#: deviations/viewbase.py +msgid "SUCCESS_OVERRIDING_DEVIATIONS" +msgstr "Deviations were overriden succesfully." + #: deviations/viewbase.py msgid "NOTHING_REMOVED" msgstr "Nothing removed." diff --git a/locale/fi/LC_MESSAGES/django.po b/locale/fi/LC_MESSAGES/django.po index 89ed8d080..68fe64e9f 100644 --- a/locale/fi/LC_MESSAGES/django.po +++ b/locale/fi/LC_MESSAGES/django.po @@ -6,7 +6,7 @@ msgid "" msgstr "" "Project-Id-Version: \n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2023-10-27 20:01+0300\n" +"POT-Creation-Date: 2024-01-20 17:06+0200\n" "PO-Revision-Date: 2019-08-14 12:16+0200\n" "Last-Translator: Jimmy Ihalainen \n" "Language-Team: Finnish <>\n" @@ -1856,11 +1856,19 @@ msgstr "Ylikirjoita" msgid "SUBMIT" msgstr "Lähetä" +#: deviations/viewbase.py +msgid "SUCCESS_ADDING_DEVIATIONS" +msgstr "Poikkeamat lisättiin onnistuneesti." + #: deviations/viewbase.py #, python-brace-format msgid "INVALID_EXERCISE_OR_SUBMITTER_ID -- {id}" msgstr "Virheellinen tehtävän tai palauttajan id: {id}" +#: deviations/viewbase.py +msgid "SUCCESS_OVERRIDING_DEVIATIONS" +msgstr "Poikkeamat ylikirjoitettiin onnistuneesti." + #: deviations/viewbase.py msgid "NOTHING_REMOVED" msgstr "Mitään ei poistettu."