Skip to content

Commit

Permalink
Make sure resulting grants are attached to the right point in the cha…
Browse files Browse the repository at this point in the history
…in. (#151)
  • Loading branch information
amanning9 authored Sep 25, 2024
1 parent 920c09e commit 381387d
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions jasmin_services/models/request.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import inspect

import django.db.models.signals
import django.dispatch
from django.conf import settings
from django.core.exceptions import ObjectDoesNotExist, ValidationError
from django.db import models
Expand Down Expand Up @@ -260,3 +262,27 @@ def clean(self):
pass
if errors:
raise ValidationError(errors)


@django.dispatch.receiver(django.db.models.signals.pre_save, sender=Request)
def ensure_previous_grant(sender, instance, **kwargs):
"""Make sure requests have a previous_request and/or previous_grant.
This makes sure that request, and the resulting grants, are attached to the right point in the chain.
"""
if (instance.pk is None) and (not settings.MULTIPLE_REQUESTS_ALLOWED):
if not instance.previous_request:
active_request = sender.objects.filter(
access=instance.access,
resulting_grant__isnull=True,
next_request__isnull=True,
).first()
if active_request:
instance.previous_request = active_request

if not instance.previous_grant:
active_grant = Grant.objects.filter(
access=instance.access, next_grant__isnull=True
).first()
if active_grant:
instance.previous_grant = active_grant

0 comments on commit 381387d

Please sign in to comment.