Skip to content

Commit

Permalink
Fix erroring-responses on oddly-used endpoints. (#167)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
amanning9 authored Dec 2, 2024
1 parent b61128d commit 42d8066
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion jasmin_services/views/grant_review.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
23 changes: 23 additions & 0 deletions jasmin_services/views/request_decide.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 42d8066

Please sign in to comment.