From 42d80668ba309a1b20c7f941853657336b1f2053 Mon Sep 17 00:00:00 2001 From: Alex Manning Date: Mon, 2 Dec 2024 13:16:24 +0000 Subject: [PATCH] Fix erroring-responses on oddly-used endpoints. (#167) * Fix bug where approver appeared to be able to aprove a service twice. Then got an error if they actually did it. * Handle request_decide POST when request has already been actioned. * post not get. * Pass in service to redirect_to_service. * Return the redirect correctly. --- jasmin_services/views/grant_review.py | 2 +- jasmin_services/views/request_decide.py | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/jasmin_services/views/grant_review.py b/jasmin_services/views/grant_review.py index c782435..c9eeb61 100644 --- a/jasmin_services/views/grant_review.py +++ b/jasmin_services/views/grant_review.py @@ -41,7 +41,7 @@ def grant_review(request, pk): and not request.user.has_perm(permission, grant.access.role) ): messages.error(request, "Grant does not exist") - return redirect_to_service(grant.role.service, "service_details") + return redirect_to_service(grant.access.role.service, "service_details") # If the grant is expired or revoked, redirect to the list of users if not grant.active or grant.expired or grant.revoked: messages.info(request, "This grant has already been rekoved or expired") diff --git a/jasmin_services/views/request_decide.py b/jasmin_services/views/request_decide.py index f45d4aa..7320c50 100644 --- a/jasmin_services/views/request_decide.py +++ b/jasmin_services/views/request_decide.py @@ -1,6 +1,8 @@ import asgiref.sync import django.contrib.auth.mixins +import django.contrib.messages import django.db +import django.http import django.urls import django.views.generic.edit @@ -32,6 +34,27 @@ def setup(self, request, *args, **kwargs): self.object.access.role.service.category.name, self.object.access.role.service.name ) + def request_already_actioned(self, request): + """Generate a response for the case where the request has already been actioned.""" + django.contrib.messages.add_message( + request, django.contrib.messages.INFO, "The request has already been approved." + ) + return django.http.HttpResponseRedirect( + f"/services/{self.service.category.name}/{self.service.name}/requests/" + ) + + def get(self, request, *args, **kwargs): + """Override get to deal with case where request has been actioned already.""" + if self.object.state != "PENDING": + return self.request_already_actioned(request) + return super().get(request, *args, **kwargs) + + def post(self, request, *args, **kwargs): + """Override post to deal with case where request has been actioned already.""" + if self.object.state != "PENDING": + return self.request_already_actioned(request) + return super().post(request, *args, **kwargs) + def test_func(self): """Define the test for the UserPassesTestMixin.