Skip to content

Commit

Permalink
Create delete draft workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
Tammo-Feldmann committed Nov 14, 2023
1 parent f8ce2dc commit 4e8870f
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 28 deletions.
12 changes: 8 additions & 4 deletions app/admin_ui/tables/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,14 @@ class Meta:
sequence = ("status", "updated_at")

def render_status(self, value, record):
css_class = get_draft_status_class(value)
# overwrite draft status if we have a published delete draft
css_class = get_draft_status_class(record.latest_status)
# overwrite draft status and display for published delete drafts
if record.latest_action == "Delete" and record.latest_status == Change.Statuses.PUBLISHED:
value = "DELETED"
return mark_safe(
f'<div class="badge badge-pill text-white badge-danger">'

Check failure on line 157 in app/admin_ui/tables/tables.py

View workflow job for this annotation

GitHub Actions / Flake8

app/admin_ui/tables/tables.py#L157

F-string is missing placeholders (F541)
+ record.__class__(status="Deleted").get_status_display()
+ '</div>'
)

return mark_safe(
f'<div class="badge badge-pill text-white {css_class}">'
Expand Down Expand Up @@ -583,7 +587,7 @@ class ChangeSummaryTable(DraftTableBase):
content_type__model = tables.Column(
verbose_name="Model Type", accessor="model_name", order_by="content_type__model"
)
status = tables.Column(verbose_name="Status", accessor="action")
status = tables.Column(verbose_name="Status", accessor="latest_status")
updated_at = tables.DateTimeColumn(verbose_name="Last Edit Date", accessor="updated_at")
last_published = tables.DateTimeColumn(
verbose_name="Last Published", accessor="latest_published_at"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ <h5 class="modal-title" id="deletePopupLongTitle">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<a href="{% url 'published-delete' view_model object.uuid %}?back={{ request.path }}" class="btn btn-danger">
<a href="{% url 'published-delete' model=view_model canonical_uuid=object.uuid %}" class="btn btn-danger">
Delete This {{ display_name }}
</a>
</div>
Expand Down
2 changes: 1 addition & 1 deletion app/admin_ui/templates/snippets/object_header_tabs.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
{% if draft_status == 'Published' and url_name == 'create-update' %}
<span class="badge badge-pill text-white badge-danger ml-1">Unsaved</span>
{% elif is_deleted %}
<span class="badge badge-pill text-white {{ draft_status_class}}">Deleted</span>
<span class="badge badge-pill text-white badge-danger">Deleted</span>
{% elif draft_status != 'Published' %}
<span class="badge badge-pill text-white {{ draft_status_class}}">{{ draft_status }}</span>
{% endif %}
Expand Down
2 changes: 1 addition & 1 deletion app/admin_ui/templatetags/template_extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def object_header_tabs(context, canonical_uuid: str):
uuid=canonical_uuid
).exists(),
"has_published_draft": Change.objects.related_drafts(canonical_uuid)
.filter(status=Change.Statuses.PUBLISHED)
.filter(action=Change.Statuses.CREATED, status=Change.Statuses.PUBLISHED)
.exists(),
"request": context.get("request"),
"view_model": context.get("view_model"),
Expand Down
10 changes: 7 additions & 3 deletions app/admin_ui/views/change.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,13 @@ def get_queryset(self):
We're getting a list of all created records (so we can link to them) for all models.
However, we want to display the most recent related draft in the table.
"""
related_drafts = Change.objects.filter(
Q(model_instance_uuid=OuterRef("uuid")) | Q(uuid=OuterRef("uuid"))
).order_by("status", "-updated_at")
# related_drafts = Change.objects.filter(
# Q(model_instance_uuid=OuterRef("uuid")) | Q(uuid=OuterRef("uuid"))
# ).order_by("status", "-updated_at")

related_drafts = Change.objects.related_drafts(OuterRef("uuid")).order_by(
"status", "-updated_at"
)

latest_published_draft = Change.objects.filter(
status=Change.Statuses.PUBLISHED,
Expand Down
14 changes: 3 additions & 11 deletions app/admin_ui/views/published.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from django.contrib.auth import get_user_model

from api_app.views.generic_views import NotificationSidebar
from api_app.models import Change, ApprovalLog
from api_app.models import Change

from .. import utils, forms, mixins

Expand Down Expand Up @@ -121,23 +121,15 @@ class PublishedDeleteView(mixins.DynamicModelMixin, View):
def dispatch(self, request, *args, **kwargs):
model_to_query = self._model_config["model"]
content_type = ContentType.objects.get_for_model(model_to_query)
change_object = Change.objects.create(
change_object = Change(
content_type=content_type,
status=Change.Statuses.CREATED,
action=Change.Actions.DELETE,
model_instance_uuid=kwargs["canonical_uuid"],
update={},
)
# directly publish delete draft without going through approval process
change_object.status = Change.Statuses.PUBLISHED
ApprovalLog.objects.create(
change=change_object,
user=request.user,
action=ApprovalLog.Actions.PUBLISH,
notes="",
)
change_object.save(post_save=True)

change_object.publish(request.user)
return redirect(
reverse("canonical-list", kwargs={'model': self._model_config['singular_snake_case']})
)
27 changes: 21 additions & 6 deletions app/admin_ui/views/v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,24 @@ def redirect_helper(request, canonical_uuid, model):
and not Change.objects.related_drafts(canonical_uuid).order_by("-updated_at").first().action
== Change.Actions.DELETE
)
return (
redirect(
is_deleted = (
Change.objects.related_drafts(canonical_uuid)
.filter(status=Change.Statuses.PUBLISHED, action=Change.Actions.DELETE)
.exists()
)

if is_deleted:
return redirect(
reverse(
"change-history",
kwargs={
"canonical_uuid": canonical_uuid,
"model": model,
},
)
)
elif has_active_progress_draft:
return redirect(
reverse(
"canonical-draft-edit",
kwargs={
Expand All @@ -58,8 +74,8 @@ def redirect_helper(request, canonical_uuid, model):
},
)
)
if has_active_progress_draft
else redirect(
else:
return redirect(
reverse(
"canonical-published-detail",
kwargs={
Expand All @@ -68,7 +84,6 @@ def redirect_helper(request, canonical_uuid, model):
},
)
)
)


# Lists all the canonical records for a given model type
Expand Down Expand Up @@ -137,7 +152,7 @@ class ChangeHistoryList(SingleTableView):
template_name = "api_app/canonical/change_history.html"

def get_queryset(self):
return Change.objects.related_drafts(self.kwargs[self.pk_url_kwarg])
return Change.objects.related_drafts(self.kwargs[self.pk_url_kwarg]).order_by("-updated_at")

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
Expand Down
2 changes: 1 addition & 1 deletion app/api_app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,7 @@ def set_change_updated_at(sender, instance, **kwargs):
try:
if instance.change: # Check if the 'change' field exists
instance.change.updated_at = instance.date
instance.change.save(check_status=False)
instance.change.save(check_status=False, post_save=post_save)
except Change.DoesNotExist:
pass

Expand Down

0 comments on commit 4e8870f

Please sign in to comment.