From 3413a11c0bce64aa67bf8be40eca1ffb8f43c480 Mon Sep 17 00:00:00 2001 From: Florian Strehl Date: Mon, 28 Nov 2022 18:09:10 +0100 Subject: [PATCH 1/7] Changed HTTP-endpoint for semester activation (#1835) --- evap/rewards/urls.py | 2 +- evap/rewards/views.py | 6 ++-- evap/staff/templates/staff_semester_view.html | 28 ++++++++----------- 3 files changed, 17 insertions(+), 19 deletions(-) diff --git a/evap/rewards/urls.py b/evap/rewards/urls.py index 58fde9cb76..f85515c445 100644 --- a/evap/rewards/urls.py +++ b/evap/rewards/urls.py @@ -13,5 +13,5 @@ path("reward_point_redemption_event//export", views.reward_point_redemption_event_export, name="reward_point_redemption_event_export"), path("reward_point_redemption_event/delete", views.reward_point_redemption_event_delete, name="reward_point_redemption_event_delete"), - path("reward_semester_activation//", views.semester_activation, name="semester_activation"), + path("reward_semester_activation//edit", views.semester_activation_edit, name="semester_activation_edit"), ] diff --git a/evap/rewards/views.py b/evap/rewards/views.py index f8e818a10e..faea1ac6be 100644 --- a/evap/rewards/views.py +++ b/evap/rewards/views.py @@ -137,10 +137,12 @@ def reward_point_redemption_event_export(request, event_id): return response +@require_POST @manager_required -def semester_activation(request, semester_id, active): +def semester_activation_edit(request, semester_id): semester = get_object_or_404(Semester, id=semester_id) - active = active == "on" + status = request.POST.get("activation_status") + active = status == "on" SemesterActivation.objects.update_or_create(semester=semester, defaults={"is_active": active}) if active: diff --git a/evap/staff/templates/staff_semester_view.html b/evap/staff/templates/staff_semester_view.html index b24504f209..45b93775e0 100644 --- a/evap/staff/templates/staff_semester_view.html +++ b/evap/staff/templates/staff_semester_view.html @@ -4,8 +4,12 @@ {% load evaluation_filters %} {% block content %} - {{ block.super }} + {{ block.super }} +
+ + {% csrf_token %} +

{{ semester.name }} {% if request.user.is_manager %} @@ -549,23 +553,15 @@

{% trans 'Activate reward points' as action_text %} {% include 'confirmation_modal.html' with modal_id='activateRewardPointsModal' title=title question=question action_text=action_text btn_type='primary' %} {% endblock %} From 789a39c7b13e4ce0a46abc0852b5c6dc1f433e68 Mon Sep 17 00:00:00 2001 From: Florian Strehl Date: Mon, 28 Nov 2022 18:22:32 +0100 Subject: [PATCH 2/7] Fixed test for semester activation --- evap/rewards/tests/test_views.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/evap/rewards/tests/test_views.py b/evap/rewards/tests/test_views.py index 178d2b7697..8211def9c6 100644 --- a/evap/rewards/tests/test_views.py +++ b/evap/rewards/tests/test_views.py @@ -205,21 +205,30 @@ def setUpTestData(cls): course=course, ) - cls.url = f"/rewards/reward_semester_activation/{cls.semester.pk}/" + cls.url = f"/rewards/reward_semester_activation/{cls.semester.pk}/edit" def test_activate(self): baker.make(SemesterActivation, semester=self.semester, is_active=False) - self.app.post(self.url + "on", user=self.manager) + response = self.app.post(self.url, user=self.manager) + form = response.forms["form_activation_status"] + form.set("activation_status", "on") + form.submit() self.assertTrue(is_semester_activated(self.semester)) def test_deactivate(self): baker.make(SemesterActivation, semester=self.semester, is_active=True) - self.app.post(self.url + "off", user=self.manager) + response = self.app.post(self.url, user=self.manager) + form = response.forms["form_activation_status"] + form.set("activation_status", "off") + form.submit() self.assertFalse(is_semester_activated(self.semester)) def test_activate_after_voting(self): baker.make(SemesterActivation, semester=self.semester, is_active=False) self.assertEqual(0, reward_points_of_user(self.student)) - response = self.app.post(self.url + "on", user=self.manager) + response = self.app.post(self.url, user=self.manager) + form = response.forms["form_activation_status"] + form.set("activation_status", "on") + response = form.submit() self.assertContains(response, "3 reward points were granted") self.assertEqual(3, reward_points_of_user(self.student)) From 6475ee912f5648e3ef227de4dc47bb07cdd46204 Mon Sep 17 00:00:00 2001 From: Florian Strehl Date: Mon, 12 Dec 2022 18:58:42 +0100 Subject: [PATCH 3/7] Minor reward activation improvements --- evap/rewards/tests/test_views.py | 26 +++++++++---------- evap/rewards/urls.py | 2 +- evap/rewards/views.py | 12 ++++++--- evap/staff/templates/staff_semester_view.html | 13 +++------- 4 files changed, 24 insertions(+), 29 deletions(-) diff --git a/evap/rewards/tests/test_views.py b/evap/rewards/tests/test_views.py index 8211def9c6..c60f07aec1 100644 --- a/evap/rewards/tests/test_views.py +++ b/evap/rewards/tests/test_views.py @@ -205,30 +205,28 @@ def setUpTestData(cls): course=course, ) - cls.url = f"/rewards/reward_semester_activation/{cls.semester.pk}/edit" + cls.url = reverse("rewards:semester_activation_edit", args=[cls.semester.pk]) + def test_invalid(self): + baker.make(SemesterActivation, semester=self.semester, is_active=False) + response = self.app.post(self.url, user=self.manager, params={"activation_status": "invalid"}, status=400) + self.assertFalse(is_semester_activated(self.semester)) def test_activate(self): baker.make(SemesterActivation, semester=self.semester, is_active=False) - response = self.app.post(self.url, user=self.manager) - form = response.forms["form_activation_status"] - form.set("activation_status", "on") - form.submit() + response = self.app.post(self.url, user=self.manager, params={"activation_status": "on"}) self.assertTrue(is_semester_activated(self.semester)) def test_deactivate(self): baker.make(SemesterActivation, semester=self.semester, is_active=True) - response = self.app.post(self.url, user=self.manager) - form = response.forms["form_activation_status"] - form.set("activation_status", "off") - form.submit() + response = self.app.post(self.url, user=self.manager, params={"activation_status": "off"}) self.assertFalse(is_semester_activated(self.semester)) def test_activate_after_voting(self): baker.make(SemesterActivation, semester=self.semester, is_active=False) self.assertEqual(0, reward_points_of_user(self.student)) - response = self.app.post(self.url, user=self.manager) - form = response.forms["form_activation_status"] - form.set("activation_status", "on") - response = form.submit() - self.assertContains(response, "3 reward points were granted") + response = self.app.post(self.url, user=self.manager, params={"activation_status": "on"}) + #form = response.forms["form_activation_status"] + #form.set("activation_status", "on") + #response = form.submit() + self.assertContains(response, "3 reward points were granted", status_code=302) self.assertEqual(3, reward_points_of_user(self.student)) diff --git a/evap/rewards/urls.py b/evap/rewards/urls.py index f85515c445..bc4c135a19 100644 --- a/evap/rewards/urls.py +++ b/evap/rewards/urls.py @@ -13,5 +13,5 @@ path("reward_point_redemption_event//export", views.reward_point_redemption_event_export, name="reward_point_redemption_event_export"), path("reward_point_redemption_event/delete", views.reward_point_redemption_event_delete, name="reward_point_redemption_event_delete"), - path("reward_semester_activation//edit", views.semester_activation_edit, name="semester_activation_edit"), + path("semester_activation//edit", views.semester_activation_edit, name="semester_activation_edit"), ] diff --git a/evap/rewards/views.py b/evap/rewards/views.py index faea1ac6be..64ed96683c 100644 --- a/evap/rewards/views.py +++ b/evap/rewards/views.py @@ -142,10 +142,14 @@ def reward_point_redemption_event_export(request, event_id): def semester_activation_edit(request, semester_id): semester = get_object_or_404(Semester, id=semester_id) status = request.POST.get("activation_status") - active = status == "on" - + if status == "on": + active = True + elif status == "off": + active = False + else: + raise SuspiciousOperation("Invalid activation keyword") SemesterActivation.objects.update_or_create(semester=semester, defaults={"is_active": active}) if active: grant_eligible_reward_points_for_semester(request, semester) - - return semester_view(request=request, semester_id=semester_id) + messages.get_messages(request).used = False + return redirect("staff:semester_view", semester_id) #semester_view(request=request, semester_id=semester_id) diff --git a/evap/staff/templates/staff_semester_view.html b/evap/staff/templates/staff_semester_view.html index 45b93775e0..ceeeb397c3 100644 --- a/evap/staff/templates/staff_semester_view.html +++ b/evap/staff/templates/staff_semester_view.html @@ -7,7 +7,7 @@ {{ block.super }}
- + {# FIXME #} {% csrf_token %}

@@ -73,7 +73,7 @@

{% trans 'Reward points active' %}
- +
@@ -553,15 +553,8 @@

{% trans 'Activate reward points' as action_text %} {% include 'confirmation_modal.html' with modal_id='activateRewardPointsModal' title=title question=question action_text=action_text btn_type='primary' %} {% endblock %} From 4fe75aa41bcf5b82157bd6d36b0d0c775278bfc0 Mon Sep 17 00:00:00 2001 From: Florian Strehl Date: Mon, 12 Dec 2022 19:12:14 +0100 Subject: [PATCH 4/7] Fixed test_activate_after_voting() --- evap/rewards/tests/test_views.py | 14 ++++++-------- evap/rewards/views.py | 2 +- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/evap/rewards/tests/test_views.py b/evap/rewards/tests/test_views.py index c60f07aec1..6b826b935d 100644 --- a/evap/rewards/tests/test_views.py +++ b/evap/rewards/tests/test_views.py @@ -209,24 +209,22 @@ def setUpTestData(cls): def test_invalid(self): baker.make(SemesterActivation, semester=self.semester, is_active=False) - response = self.app.post(self.url, user=self.manager, params={"activation_status": "invalid"}, status=400) + self.app.post(self.url, user=self.manager, params={"activation_status": "invalid"}, status=400) self.assertFalse(is_semester_activated(self.semester)) + def test_activate(self): baker.make(SemesterActivation, semester=self.semester, is_active=False) - response = self.app.post(self.url, user=self.manager, params={"activation_status": "on"}) + self.app.post(self.url, user=self.manager, params={"activation_status": "on"}) self.assertTrue(is_semester_activated(self.semester)) def test_deactivate(self): baker.make(SemesterActivation, semester=self.semester, is_active=True) - response = self.app.post(self.url, user=self.manager, params={"activation_status": "off"}) + self.app.post(self.url, user=self.manager, params={"activation_status": "off"}) self.assertFalse(is_semester_activated(self.semester)) def test_activate_after_voting(self): baker.make(SemesterActivation, semester=self.semester, is_active=False) self.assertEqual(0, reward_points_of_user(self.student)) - response = self.app.post(self.url, user=self.manager, params={"activation_status": "on"}) - #form = response.forms["form_activation_status"] - #form.set("activation_status", "on") - #response = form.submit() - self.assertContains(response, "3 reward points were granted", status_code=302) + response = self.app.post(self.url, user=self.manager, params={"activation_status": "on"}).follow() + self.assertContains(response, "3 reward points were granted") self.assertEqual(3, reward_points_of_user(self.student)) diff --git a/evap/rewards/views.py b/evap/rewards/views.py index 64ed96683c..af8f3e45dd 100644 --- a/evap/rewards/views.py +++ b/evap/rewards/views.py @@ -152,4 +152,4 @@ def semester_activation_edit(request, semester_id): if active: grant_eligible_reward_points_for_semester(request, semester) messages.get_messages(request).used = False - return redirect("staff:semester_view", semester_id) #semester_view(request=request, semester_id=semester_id) + return redirect("staff:semester_view", semester_id) # semester_view(request=request, semester_id=semester_id) From 04f13add697e04856414211347e8beba3a0d3f2d Mon Sep 17 00:00:00 2001 From: Florian Strehl Date: Mon, 9 Jan 2023 16:51:38 +0100 Subject: [PATCH 5/7] Removed unused import --- evap/rewards/views.py | 1 - 1 file changed, 1 deletion(-) diff --git a/evap/rewards/views.py b/evap/rewards/views.py index af8f3e45dd..42a2b382ad 100644 --- a/evap/rewards/views.py +++ b/evap/rewards/views.py @@ -24,7 +24,6 @@ SemesterActivation, ) from evap.rewards.tools import grant_eligible_reward_points_for_semester, reward_points_of_user, save_redemptions -from evap.staff.views import semester_view def redeem_reward_points(request): From b401341dfe03caf8251f5fa8b3db9f30ee50d2b6 Mon Sep 17 00:00:00 2001 From: Florian Strehl Date: Mon, 16 Jan 2023 17:19:22 +0100 Subject: [PATCH 6/7] Formatting changes + minor changes --- evap/rewards/views.py | 3 +-- evap/staff/templates/staff_semester_view.html | 6 +++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/evap/rewards/views.py b/evap/rewards/views.py index 42a2b382ad..b418272fcd 100644 --- a/evap/rewards/views.py +++ b/evap/rewards/views.py @@ -150,5 +150,4 @@ def semester_activation_edit(request, semester_id): SemesterActivation.objects.update_or_create(semester=semester, defaults={"is_active": active}) if active: grant_eligible_reward_points_for_semester(request, semester) - messages.get_messages(request).used = False - return redirect("staff:semester_view", semester_id) # semester_view(request=request, semester_id=semester_id) + return redirect("staff:semester_view", semester_id) diff --git a/evap/staff/templates/staff_semester_view.html b/evap/staff/templates/staff_semester_view.html index ceeeb397c3..bb2afcff1d 100644 --- a/evap/staff/templates/staff_semester_view.html +++ b/evap/staff/templates/staff_semester_view.html @@ -6,8 +6,8 @@ {% block content %} {{ block.super }} -
- {# FIXME #} + + {% csrf_token %}

@@ -554,7 +554,7 @@

{% include 'confirmation_modal.html' with modal_id='activateRewardPointsModal' title=title question=question action_text=action_text btn_type='primary' %} {% endblock %} From 809bfa137a9619d6932c80bfa3587f8d928e320b Mon Sep 17 00:00:00 2001 From: Florian Strehl Date: Mon, 16 Jan 2023 17:58:13 +0100 Subject: [PATCH 7/7] Formatting changes + added test for semester activation without parameter --- evap/rewards/tests/test_views.py | 2 ++ evap/staff/templates/staff_semester_view.html | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/evap/rewards/tests/test_views.py b/evap/rewards/tests/test_views.py index 6b826b935d..489b84f5f4 100644 --- a/evap/rewards/tests/test_views.py +++ b/evap/rewards/tests/test_views.py @@ -211,6 +211,8 @@ def test_invalid(self): baker.make(SemesterActivation, semester=self.semester, is_active=False) self.app.post(self.url, user=self.manager, params={"activation_status": "invalid"}, status=400) self.assertFalse(is_semester_activated(self.semester)) + self.app.post(self.url, user=self.manager, status=400) + self.assertFalse(is_semester_activated(self.semester)) def test_activate(self): baker.make(SemesterActivation, semester=self.semester, is_active=False) diff --git a/evap/staff/templates/staff_semester_view.html b/evap/staff/templates/staff_semester_view.html index bb2afcff1d..4e8f249841 100644 --- a/evap/staff/templates/staff_semester_view.html +++ b/evap/staff/templates/staff_semester_view.html @@ -4,8 +4,8 @@ {% load evaluation_filters %} {% block content %} - {{ block.super }} +
{% csrf_token %}