Skip to content

Commit

Permalink
add validation check
Browse files Browse the repository at this point in the history
  • Loading branch information
shapiromatron committed Nov 22, 2024
1 parent c765bfa commit 59c343c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
23 changes: 23 additions & 0 deletions hawc/apps/assessment/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1391,6 +1391,29 @@ def get_edit_url(self):
def get_delete_url(self):
return reverse("assessment:label-htmx", args=(self.pk, "delete"))

def can_change_published(self) -> tuple[bool, str]:
"""Check that the item can be published or unpublished
Returns:
tuple[bool, str]: boolean, status message if false
"""
next = not self.published
if next:
if self.depth == 1:
# any depth of 1 tag can be published
return True, ""
parent_published = self.get_parent().published
return (
parent_published,
"" if parent_published else "Parent must be published to publish child",
)
else:
all_unpublished = all(child.published is False for child in self.get_children())
return (
all_unpublished,
"" if all_unpublished else "All children must be unpublished to unpublish",
)


class LabeledItem(models.Model):
objects = managers.LabeledItemManager()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
<span class="fa fa-spin fa-spinner float-right htmx-indicator"></span>
</div>
</form>
{% if can_update_message %}<span class="text-danger">{{can_update_message}}</span>{% endif %}
</td>
16 changes: 14 additions & 2 deletions hawc/apps/assessment/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -831,13 +831,25 @@ def list(self, request: HttpRequest, *args, **kwargs):
@action(permission=can_edit, methods={"post"})
def update_item(self, request: HttpRequest, *args, **kwargs):
instance = self.get_instance(request.item, kwargs["type"], kwargs["object_id"])
self.perform_update(request, instance)
can_update, can_update_message = self.can_update(instance)
if can_update:
self.perform_update(request, instance)
return render(
request,
"assessment/fragments/publish_item_td.html",
{"name": kwargs["type"], "object": instance, "assessment": request.item.assessment},
{
"name": kwargs["type"],
"object": instance,
"assessment": request.item.assessment,
"can_update_message": can_update_message,
},
)

def can_update(self, instance) -> tuple[bool, str]:
if hasattr(instance, "can_change_published"):
return instance.can_change_published()
return True, ""

def get_instance(self, item, type: str, object_id: int):
Model = self.model_lookups.get(type)
if not Model:
Expand Down

0 comments on commit 59c343c

Please sign in to comment.