Skip to content

Commit

Permalink
Replace toggle_no_grades with an idempotent alternative (#2040)
Browse files Browse the repository at this point in the history
* Replace toggle_no_grades with an idempotent alternative

Fixes #1965

Co-Authored-By: Christoph Thiede <[email protected]>
Co-Authored-By: Constantin Kühne <[email protected]>

* Fix formatting

Co-Authored-By: Christoph Thiede <[email protected]>
Co-Authored-By: Constantin Kühne <[email protected]>

* test gets_no_grade_documents for idempotency

* rename html confirm function

---------

Co-authored-by: Christoph Thiede <[email protected]>
Co-authored-by: Constantin Kühne <[email protected]>
  • Loading branch information
3 people authored Oct 23, 2023
1 parent 0019af0 commit 29b70ce
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 12 deletions.
10 changes: 7 additions & 3 deletions evap/grades/templates/grades_semester_view.html
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,12 @@ <h3 class="col-8 mb-0">
{% include 'confirmation_modal.html' with modal_id='confirmNouploadModal' title=title question=question action_text=action_text btn_type='primary' %}
<script type="text/javascript">
function confirmNouploadModalAction(dataId) {
fetch("{% url 'grades:toggle_no_grades' %}", {
body: new URLSearchParams({course_id: dataId}),
updateGradeStatusOfCourse(dataId, true);
}

function updateGradeStatusOfCourse(dataId, status) {
fetch("{% url 'grades:set_no_grades'%}", {
body: new URLSearchParams({course_id: dataId, status: `${Number(status)}` }),
headers: CSRF_HEADERS,
method: "POST",
}).then(response => {
Expand All @@ -124,7 +128,7 @@ <h3 class="col-8 mb-0">
{% include 'confirmation_modal.html' with modal_id='confirmLateruploadModal' title=title question=question action_text=action_text btn_type='primary' %}
<script type="text/javascript">
function confirmLateruploadModalAction(dataId) {
confirmNouploadModalAction(dataId);
updateGradeStatusOfCourse(dataId, false);
};
</script>
{% endblock %}
Expand Down
19 changes: 14 additions & 5 deletions evap/grades/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def test_upload_final_grades(self):
evaluation.save()
self.helper_check_final_grade_upload(course, 0)

def test_toggle_no_grades(self):
def test_set_no_grades(self):
evaluation = self.evaluation
evaluation.manager_approve()
evaluation.begin_evaluation()
Expand All @@ -146,8 +146,8 @@ def test_toggle_no_grades(self):
self.assertFalse(evaluation.course.gets_no_grade_documents)

self.app.post(
"/grades/toggle_no_grades",
params={"course_id": evaluation.course.id},
"/grades/set_no_grades",
params={"course_id": evaluation.course.id, "status": "1"},
user=self.grade_publisher,
status=200,
)
Expand All @@ -160,8 +160,17 @@ def test_toggle_no_grades(self):
)

self.app.post(
"/grades/toggle_no_grades",
params={"course_id": evaluation.course.id},
"/grades/set_no_grades",
params={"course_id": evaluation.course.id, "status": "0"},
user=self.grade_publisher,
status=200,
)
evaluation = Evaluation.objects.get(id=evaluation.id)
self.assertFalse(evaluation.course.gets_no_grade_documents)

self.app.post(
"/grades/set_no_grades",
params={"course_id": evaluation.course.id, "status": "0"},
user=self.grade_publisher,
status=200,
)
Expand Down
2 changes: 1 addition & 1 deletion evap/grades/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
path("grade_document/<int:grade_document_id>/edit", views.edit_grades, name="edit_grades"),

path("delete_grades", views.delete_grades, name="delete_grades"),
path("toggle_no_grades", views.toggle_no_grades, name="toggle_no_grades"),
path("set_no_grades", views.set_no_grades, name="set_no_grades"),
]
12 changes: 9 additions & 3 deletions evap/grades/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.conf import settings
from django.contrib import messages
from django.core.exceptions import PermissionDenied
from django.core.exceptions import PermissionDenied, SuspiciousOperation
from django.db.models.query import QuerySet
from django.http import FileResponse, HttpResponse
from django.shortcuts import get_object_or_404, redirect, render
Expand Down Expand Up @@ -146,12 +146,18 @@ def upload_grades(request, course_id):

@require_POST
@grade_publisher_required
def toggle_no_grades(request):
def set_no_grades(request):
course = get_object_from_dict_pk_entry_or_logged_40x(Course, request.POST, "course_id")

try:
status = bool(int(request.POST["status"]))
except (KeyError, TypeError, ValueError) as e:
raise SuspiciousOperation from e

if course.semester.grade_documents_are_deleted:
raise PermissionDenied

course.gets_no_grade_documents = not course.gets_no_grade_documents
course.gets_no_grade_documents = status
course.save()

if course.gets_no_grade_documents:
Expand Down

0 comments on commit 29b70ce

Please sign in to comment.