Use BaseListingView with breadcrumbs and header buttons for workflow history view

pull/12314/head
Sage Abdullah 2024-08-27 17:01:08 +01:00 zatwierdzone przez Matt Westcott
rodzic 816ec2e6c5
commit 98bbded2a8
10 zmienionych plików z 152 dodań i 89 usunięć

Wyświetl plik

@ -1,17 +0,0 @@
{% extends "wagtailadmin/base.html" %}
{% load i18n wagtailadmin_tags %}
{% block titletag %}{% latest_str object as object_str %}{% blocktrans trimmed with title=object_str %}Workflow history for {{ title }}{% endblocktrans %}{% endblock %}
{% block content %}
{% latest_str object as object_str %}
{% include "wagtailadmin/shared/header.html" with title=_("Workflow history for") subtitle=object_str icon=header_icon %}
<div class="nice-padding">
<p style="margin-bottom: 30px;">
<a href="{% admin_edit_url object %}" class="button button-small button-secondary">{% trans "Edit" %}</a>
</p>
{% include "wagtailadmin/shared/workflow_history/results.html" %}
</div>
{% endblock %}

Wyświetl plik

@ -0,0 +1 @@
{% extends "wagtailadmin/generic/listing.html" %}

Wyświetl plik

@ -1,25 +1,25 @@
{% extends "wagtailadmin/generic/listing_results.html" %}
{% load i18n wagtailadmin_tags %}
{% load l10n %}
<table class="listing">
<thead>
<tr>
<th>
{% trans 'Status' %}
</th>
<th>
{% trans 'Tasks' %}
</th>
<th>
{% trans 'Requested by' %}
</th>
<th>
{% trans 'Started at' %}
</th>
</tr>
</thead>
<tbody>
{% if workflow_states %}
{% block results %}
<table class="listing">
<thead>
<tr>
<th>
{% trans 'Status' %}
</th>
<th>
{% trans 'Tasks' %}
</th>
<th>
{% trans 'Requested by' %}
</th>
<th>
{% trans 'Started at' %}
</th>
</tr>
</thead>
<tbody>
{% for workflow_state in workflow_states %}
<tr>
<td>
@ -46,20 +46,18 @@
<td>{% human_readable_date workflow_state.created_at %}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}
{% block no_results_message %}
<p>
{% if page %}
{% trans "This page hasn't been submitted for moderation yet" %}
{% else %}
{% block no_results %}
<tr><td class="no-results-message">
<p>
{% if page %}
{% trans "This page hasn't been submitted for moderation yet" %}
{% else %}
{% blocktrans trimmed with model_name=model_opts.verbose_name %}
This {{ model_name }} hasn't been submitted for moderation yet
{% endblocktrans %}
{% endif %}
</p>
</td></tr>
{% endblock %}
{% blocktrans trimmed with model_name=model_opts.verbose_name %}
This {{ model_name }} hasn't been submitted for moderation yet
{% endblocktrans %}
{% endif %}
</tbody>
</table>
</p>
{% endblock %}

Wyświetl plik

@ -1,17 +0,0 @@
{% load i18n wagtailadmin_tags %}
{% if workflow_states %}
{% include "wagtailadmin/shared/workflow_history/list.html" %}
{% include "wagtailadmin/shared/pagination_nav.html" with items=workflow_states linkurl=workflow_history_url_name %}
{% else %}
<p>
{% if page %}
{% blocktrans trimmed %}This page hasn't been submitted for moderation yet{% endblocktrans %}
{% else %}
{% blocktrans trimmed with model_name=model_opts.verbose_name %}
This {{ model_name }} hasn't been submitted for moderation yet
{% endblocktrans %}
{% endif %}
</p>
{% endif %}

Wyświetl plik

@ -4,10 +4,12 @@ from django.urls import reverse
from wagtail.models import Page
from wagtail.test.utils import WagtailTestUtils
from wagtail.test.utils.template_tests import AdminTemplateTestUtils
class TestWorkflowHistoryDetail(WagtailTestUtils, TestCase):
class TestWorkflowHistoryDetail(AdminTemplateTestUtils, WagtailTestUtils, TestCase):
fixtures = ["test.json"]
base_breadcrumb_items = []
def setUp(self):
self.user = self.create_test_user()
@ -40,6 +42,34 @@ class TestWorkflowHistoryDetail(WagtailTestUtils, TestCase):
),
)
site_root = Page.objects.get(id=2)
events_page = self.christmas_event.get_parent()
items = [
{
"url": reverse("wagtailadmin_explore_root"),
"label": "Root",
},
{
"url": reverse("wagtailadmin_explore", args=(site_root.id,)),
"label": site_root.get_admin_display_title(),
},
{
"url": reverse("wagtailadmin_explore", args=(events_page.id,)),
"label": events_page.get_admin_display_title(),
},
{
"url": reverse("wagtailadmin_explore", args=(self.christmas_event.id,)),
"label": self.christmas_event.get_admin_display_title(),
},
{
"url": "",
"label": "Workflow history",
"sublabel": self.christmas_event.get_admin_display_title(),
},
]
self.assertBreadcrumbsItemsRendered(items, response.content)
def test_get_index_with_bad_permissions(self):
# Remove privileges from user
self.user.is_superuser = False

Wyświetl plik

@ -2,7 +2,6 @@ from datetime import timedelta
import django_filters
from django.contrib.admin.utils import quote
from django.core.paginator import Paginator
from django.forms import CheckboxSelectMultiple
from django.shortcuts import get_object_or_404
from django.urls import reverse
@ -325,27 +324,77 @@ class HistoryView(PermissionCheckedMixin, BaseObjectMixin, BaseListingView):
return kwargs
class WorkflowHistoryView(BaseObjectMixin, WagtailAdminTemplateMixin, TemplateView):
template_name = "wagtailadmin/shared/workflow_history/index.html"
page_kwarg = "p"
workflow_history_url_name = None
class WorkflowHistoryView(BaseObjectMixin, BaseListingView):
template_name = "wagtailadmin/shared/workflow_history/listing.html"
results_template_name = "wagtailadmin/shared/workflow_history/listing_results.html"
paginate_by = 20
index_url_name = None
edit_url_name = None
workflow_history_detail_url_name = None
page_title = gettext_lazy("Workflow history")
context_object_name = "workflow_states"
@cached_property
def workflow_states(self):
return WorkflowState.objects.for_instance(self.object).order_by("-created_at")
def index_url(self):
if self.index_url_name:
return reverse(self.index_url_name)
@cached_property
def edit_url(self):
if self.edit_url_name:
return reverse(self.edit_url_name, args=(quote(self.object.pk),))
@cached_property
def header_buttons(self):
buttons = []
if self.edit_url:
buttons.append(
HeaderButton(gettext("Edit"), url=self.edit_url, icon_name="edit")
)
return buttons
def get_page_subtitle(self):
return get_latest_str(self.object)
def get_breadcrumbs_items(self):
items = []
if self.index_url:
items.append(
{
"url": self.index_url,
"label": capfirst(self.model._meta.verbose_name_plural),
}
)
if self.edit_url:
items.append(
{
"url": self.edit_url,
"label": self.get_page_subtitle(),
}
)
items.append(
{
"url": "",
"label": self.get_page_title(),
"sublabel": self.get_page_subtitle(),
}
)
return self.breadcrumbs_items + items
def get_base_queryset(self):
return (
WorkflowState.objects.for_instance(self.object)
.select_related("workflow", "requested_by")
.order_by("-created_at")
)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
paginator = Paginator(self.workflow_states, per_page=20)
workflow_states = paginator.get_page(self.request.GET.get(self.page_kwarg))
context.update(
{
"object": self.object,
"workflow_states": workflow_states,
"workflow_history_url_name": self.workflow_history_url_name,
"workflow_history_detail_url_name": self.workflow_history_detail_url_name,
"model_opts": self.object._meta,
}

Wyświetl plik

@ -34,6 +34,7 @@ class PageHistoryFilterSet(history.HistoryFilterSet):
class PageWorkflowHistoryViewMixin:
model = Page
pk_url_kwarg = "page_id"
edit_url_name = "wagtailadmin_pages:edit"
def dispatch(self, request, *args, **kwargs):
if not self.object.permissions_for_user(request.user).can_edit():
@ -44,9 +45,12 @@ class PageWorkflowHistoryViewMixin:
return super().get_context_data(**kwargs, page=self.object)
class WorkflowHistoryView(PageWorkflowHistoryViewMixin, history.WorkflowHistoryView):
class WorkflowHistoryView(
PageWorkflowHistoryViewMixin,
GenericPageBreadcrumbsMixin,
history.WorkflowHistoryView,
):
header_icon = "doc-empty-inverse"
workflow_history_url_name = "wagtailadmin_pages:workflow_history"
workflow_history_detail_url_name = "wagtailadmin_pages:workflow_history_detail"

Wyświetl plik

@ -90,7 +90,7 @@ class TestCustomIcon(BaseSnippetViewSetTests):
("delete", [pk], "header.html"),
("usage", [pk], "headers/slim_header.html"),
("unpublish", [pk], "header.html"),
("workflow_history", [pk], "header.html"),
("workflow_history", [pk], "headers/slim_header.html"),
("revisions_revert", [pk, self.revision_1.id], "headers/slim_header.html"),
(
"revisions_compare",
@ -1557,6 +1557,23 @@ class TestBreadcrumbs(AdminTemplateTestUtils, BaseSnippetViewSetTests):
]
self.assertBreadcrumbsItemsRendered(items, response.content)
def test_workflow_history_view(self):
response = self.client.get(
self.get_url("workflow_history", args=(self.object.pk,))
)
items = [
{
"url": self.get_url("list"),
"label": "Full-featured snippets",
},
{
"url": self.get_url("edit", args=(self.object.pk,)),
"label": str(self.object),
},
{"url": "", "label": "Workflow history", "sublabel": str(self.object)},
]
self.assertBreadcrumbsItemsRendered(items, response.content)
class TestCustomMethods(BaseSnippetViewSetTests):
model = FullFeaturedSnippet

Wyświetl plik

@ -164,7 +164,7 @@ class TestWorkflowHistory(BaseWorkflowsTestCase):
response = self.client.get(self.get_url("workflow_history"))
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(
response, "wagtailadmin/shared/workflow_history/index.html"
response, "wagtailadmin/shared/workflow_history/listing.html"
)
self.assertContains(response, self.get_url("edit"))

Wyświetl plik

@ -761,11 +761,9 @@ class SnippetViewSet(ModelViewSet):
"workflow_history/index",
fallback=self.workflow_history_view_class.template_name,
),
workflow_history_url_name=self.get_url_name("workflow_history"),
workflow_history_detail_url_name=self.get_url_name(
"workflow_history_detail"
),
_show_breadcrumbs=False,
)
@property