Skip to content

Commit

Permalink
Redirect back to form after adding or removing deviations
Browse files Browse the repository at this point in the history
Part of #1122
  • Loading branch information
Mikael-Lenander authored and ihalaij1 committed Feb 1, 2024
1 parent 4ae3580 commit 0424b42
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 20 deletions.
21 changes: 9 additions & 12 deletions deviations/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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")

Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -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")

Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand Down
11 changes: 5 additions & 6 deletions deviations/viewbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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]:
Expand All @@ -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])
Expand Down Expand Up @@ -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]:
Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions deviations/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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({
Expand Down Expand Up @@ -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']
Expand All @@ -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']
Expand Down
10 changes: 9 additions & 1 deletion locale/en/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>\n"
"Language-Team: English<>\n"
Expand Down Expand Up @@ -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."
Expand Down
10 changes: 9 additions & 1 deletion locale/fi/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>\n"
"Language-Team: Finnish <>\n"
Expand Down Expand Up @@ -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."
Expand Down

0 comments on commit 0424b42

Please sign in to comment.