Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion api_app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,21 @@ def check_prior_unpublished_update_exists(self):
)
return False

def check_prior_unpublished_delete_exists(self):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you move this into the check_prior_unpublished_update_exists function? I don't know if we need the overhead of an entire separate function

"""This checks to see there is an existing Delete draft which has not yet been published
and links to the same data_model as the current proposed draft. The intention is to allow
a check to prevent two simultaneous delete drafts

Returns:
bool: True if there is existing delete draft"""
if self.action == self.Actions.DELETE:
return bool(
Change.objects.filter(model_instance_uuid=self.model_instance_uuid).exclude(
uuid=self.uuid
)
)
return False

def save(self, *args, post_save=False, **kwargs):
# do not check for validity of model_name and uuid if it has been approved or rejected.
# Check is done for the first time only
Expand All @@ -468,6 +483,13 @@ def save(self, *args, post_save=False, **kwargs):
{"model_instance_uuid": "Unpublished draft already exists for this model uuid."}
)

if self.check_prior_unpublished_delete_exists():
raise ValidationError(
{
"model_instance_uuid": "Unpublished Delete draft already exists for this model uuid."
}
)

return super().save(*args, **kwargs)

def _run_validator(self, partial):
Expand Down Expand Up @@ -843,7 +865,10 @@ def set_change_updated_at(sender, instance, **kwargs):
Set `updated_at` on the related Change object to the value of
the ApprovalLog's `date` field.
"""
Change.objects.filter(pk=instance.change.pk).update(updated_at=instance.date)
try:
Change.objects.filter(pk=instance.change.pk).update(updated_at=instance.date)
except Change.DoesNotExist:
pass


class Recommendation(models.Model):
Expand Down