Skip to content

Commit

Permalink
Display log 8 most recent entries before publishing
Browse files Browse the repository at this point in the history
  • Loading branch information
Nigel2392 committed Apr 10, 2024
1 parent 4c5dfaf commit 147dcdc
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 29 deletions.
58 changes: 34 additions & 24 deletions wagtail_fedit/templates/wagtail_fedit/editor/action_confirm.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,42 @@
{% include "wagtailadmin/shared/header.html" with title=action_title icon=view.get_header_icon %}

<div class="nice-padding">
<div class="wagtail-fedit-form-wrapper{% if form.block.block.meta.fedit_full %} fedit-full{% endif %}">
<div class="wagtail-fedit-form-wrapper">
<form id="wagtail-fedit-form" method="post">
{% csrf_token %}

{% panel id="wagtail-fedit-help-text" icon="help" heading=action_help_text_title %}
{% help_block status="warning" %}
{% for text in action_help_text %}
<p>{{ text }}</p>
{% endfor %}
{% endhelp_block %}
{% endpanel %}

<div class="wagtail-fedit-form-help">
<h2>{% translate "Are you sure you want to continue?" %}</h2>
</div>

<input type="hidden" name="action" value="{{ action }}" />

<div class="wagtail-fedit-form-buttons">
<button type="submit" class="button button-primary button-large">
{{ action_text }}
</button>
<a href="{{ cancel_url }}" class="button no button-large">
{% translate "Cancel" %}
</a>
</div>

{% block form_content %}
{% block help_panel %}
{% panel id="wagtail-fedit-help-text" icon="help" heading=action_help_text_title %}
{% help_block status="warning" %}
{% for text in action_help_text %}
<p>{{ text }}</p>
{% endfor %}
{% endhelp_block %}
{% endpanel %}
{% endblock %}

{% block confirm_text %}
<div class="wagtail-fedit-form-help">
<h2>{% translate "Are you sure you want to continue?" %}</h2>
</div>
{% endblock %}

<input type="hidden" name="action" value="{{ action }}" />

{% block buttons_wrapper %}
<div class="wagtail-fedit-form-buttons">
{% block buttons %}
<button type="submit" class="button button-primary button-large">
{{ action_text }}
</button>
<a href="{{ cancel_url }}" class="button no button-large">
{% translate "Cancel" %}
</a>
{% endblock %}
</div>
{% endblock %}
{% endblock %}

</form>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{% extends "./action_confirm.html" %}

{% load i18n wagtailadmin_tags %}

{% block form_content %}

<div class="wagtail-fedit-log-entries">
{% translate "Changes since last publish" as panel_heading %}
{% panel id="wagtail-fedit-log-actions" icon="draft" classname="collapsed" heading=panel_heading %}

{% if log_entries %}
<table class="listing listing--inline-actions">
<thead>
<tr>
<th>
{% trans 'Action' %}
</th>
<th>
{% trans 'User' %}
</th>
<th class="updated">
{% trans 'Date / Time' %}
</th>
</tr>
</thead>
<tbody>
{% page_permissions view.object as page_perms %}
{% for entry in log_entries %}
<tr>
<td>
{% if entry.revision %}<span class="report__results--text">{% endif %}
{{ entry.message }}
{% if entry.revision %}</span>{% endif %}
{% if entry.comment %}
<span class="report__results--comment">{% trans "Comment" %}: <em>{{ entry.comment }}</em></span>
{% endif %}
{% if entry.revision and entry.content_changed and is_page %}
{% if entry.revision == page_latest_revision %}{% trans 'Current draft' as status_label %}{% status status_label classname="w-status--primary" %}{% endif %}
{% include "wagtailadmin/pages/revisions/_actions.html" with revision=entry.revision page=view.object %}
{% endif %}
</td>
<td>
{% include "wagtailadmin/shared/user_avatar.html" with user=entry.user username=entry.user_display_name %}
</td>
<td class="updated">{% human_readable_date entry.timestamp %}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% else %}
<p>{% trans "No log entries found." %}</p>
{% endif %}


{% endpanel %}
</div>

{{ block.super }}

{% endblock %}
10 changes: 5 additions & 5 deletions wagtail_fedit/templatetags/fedit.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ def render(self, context):
block, block_id, field_name, model =\
_resolve_expressions(context, block, block_id, field_name, model)

if not block_id and "block_id" not in context and not block:
raise ValueError("Block ID is required")
# if not block_id and "block_id" not in context and not block:
# raise ValueError("Block ID is required")

# `wagtail_fedit_instance` is provided after the form is saved.
# This allows us to easily use the same instance across multiple views.
Expand Down Expand Up @@ -120,15 +120,15 @@ def render(self, context):
block_id = context["block_id"]
elif not block_id and block and hasattr(block, "id"):
block_id = block.id
elif not block_id:
raise ValueError("Block ID is required")
# elif not block_id: # Commented out; just return rendered if block_id is not available.
# raise ValueError("Block ID is required")

if not rendered:
rendered = mark_safe("")

# Check if the user has permission to edit the block.
request = context.get("request")
if not _can_edit(request, model):
if not _can_edit(request, model) or not block_id:
return rendered

if self.has_block:
Expand Down
45 changes: 45 additions & 0 deletions wagtail_fedit/views/editable.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Any, Type
from django.db import models
from django.conf import settings
from django.utils.translation import gettext_lazy as _
from django.utils.decorators import method_decorator
Expand All @@ -15,6 +16,9 @@
HttpResponse,
)
from wagtail.admin import messages
from wagtail.log_actions import (
registry,
)
from wagtail.admin.admin_url_finder import AdminURLFinder
from wagtail.actions.publish_page_revision import PublishPageRevisionAction
from wagtail.actions.publish_revision import PublishRevisionAction
Expand All @@ -27,6 +31,8 @@
DraftStateMixin,
WorkflowMixin,
WorkflowState,
PageLogEntry,
ModelLogEntry,
Page,
)
from .. import forms as block_forms
Expand All @@ -47,6 +53,9 @@
)


MAX_LOG_ENTRIES_DISPLAYED = 8


def get_unpublish_action(object):
if isinstance(object, Page):
return UnpublishPageAction
Expand Down Expand Up @@ -221,6 +230,7 @@ def post(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse:


class PublishView(BaseActionView):
template_name = "wagtail_fedit/editor/action_publish_confirm.html"
required_superclasses = [DraftStateMixin, RevisionMixin]
action_text = _("Publish")

Expand All @@ -240,6 +250,41 @@ def get_action_help_text(self):
s.append(_("You can always choose to unpublish it."))

return s

def get_context_data(self, **kwargs: Any) -> dict[str, Any]:
log_entry_count = 0
log_entry_model = registry.get_log_model_for_model(self.object.__class__)
if issubclass(log_entry_model, PageLogEntry):
log_entries = log_entry_model.objects\
.filter(page=self.object)\
.order_by("-timestamp")

elif issubclass(log_entry_model, ModelLogEntry):
log_entries = log_entry_model.objects\
.filter(object_id=self.object.pk)\
.order_by("-timestamp")

else:
log_entries = None

if log_entries:
log_entries = log_entries.filter(
timestamp__gt=models.Subquery(
log_entries.filter(action="wagtail.publish")\
.values("timestamp")\
.order_by("-timestamp")[:1]
)
)
log_entry_count = log_entries.count()
log_entries = log_entries[:MAX_LOG_ENTRIES_DISPLAYED]

return super().get_context_data(**kwargs) | {
"log_entries": log_entries,
"has_more_entries": log_entry_count > MAX_LOG_ENTRIES_DISPLAYED,
"log_entry_count": log_entry_count,
"last_published_at": self.object.last_published_at,
"is_page": isinstance(self.object, Page),
}

def check_policy(self, request: HttpRequest, policy: FeditPermissionCheck) -> None:
if not policy.can_publish():
Expand Down

0 comments on commit 147dcdc

Please sign in to comment.