kopia lustrzana https://github.com/wagtail/wagtail
Extract drafts-related fields, methods, and properties from `Page` to `DraftStateMixin` (#8612)
rodzic
920c0afdf4
commit
d40cf331d0
|
@ -1,16 +1,17 @@
|
|||
import logging
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.exceptions import PermissionDenied
|
||||
from django.utils import timezone
|
||||
|
||||
from wagtail.log_actions import log
|
||||
from wagtail.actions.publish_revision import (
|
||||
PublishPermissionError,
|
||||
PublishRevisionAction,
|
||||
)
|
||||
from wagtail.signals import page_published
|
||||
|
||||
logger = logging.getLogger("wagtail")
|
||||
|
||||
|
||||
class PublishPagePermissionError(PermissionDenied):
|
||||
class PublishPagePermissionError(PublishPermissionError):
|
||||
"""
|
||||
Raised when the page publish cannot be performed due to insufficient permissions.
|
||||
"""
|
||||
|
@ -18,7 +19,7 @@ class PublishPagePermissionError(PermissionDenied):
|
|||
pass
|
||||
|
||||
|
||||
class PublishPageRevisionAction:
|
||||
class PublishPageRevisionAction(PublishRevisionAction):
|
||||
"""
|
||||
Publish or schedule revision for publishing.
|
||||
|
||||
|
@ -33,189 +34,38 @@ class PublishPageRevisionAction:
|
|||
:param previous_revision: indicates a revision reversal. Should be set to the previous revision instance
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self, revision, user=None, changed=True, log_action=True, previous_revision=None
|
||||
):
|
||||
self.revision = revision
|
||||
self.page = self.revision.as_object()
|
||||
self.user = user
|
||||
self.changed = changed
|
||||
self.log_action = log_action
|
||||
self.previous_revision = previous_revision
|
||||
|
||||
def check(self, skip_permission_checks=False):
|
||||
if (
|
||||
self.user
|
||||
and not skip_permission_checks
|
||||
and not self.page.permissions_for_user(self.user).can_publish()
|
||||
and not self.object.permissions_for_user(self.user).can_publish()
|
||||
):
|
||||
raise PublishPagePermissionError(
|
||||
"You do not have permission to publish this page"
|
||||
)
|
||||
|
||||
def log_scheduling_action(self):
|
||||
log(
|
||||
instance=self.page,
|
||||
action="wagtail.publish.schedule",
|
||||
user=self.user,
|
||||
data={
|
||||
"revision": {
|
||||
"id": self.revision.id,
|
||||
"created": self.revision.created_at.strftime("%d %b %Y %H:%M"),
|
||||
"go_live_at": self.page.go_live_at.strftime("%d %b %Y %H:%M"),
|
||||
"has_live_version": self.page.live,
|
||||
}
|
||||
},
|
||||
revision=self.revision,
|
||||
content_changed=self.changed,
|
||||
)
|
||||
def _after_publish(self):
|
||||
from wagtail.models import COMMENTS_RELATION_NAME
|
||||
|
||||
def _publish_page_revision(
|
||||
self, revision, page, user, changed, log_action, previous_revision
|
||||
):
|
||||
from wagtail.models import COMMENTS_RELATION_NAME, Revision
|
||||
|
||||
if page.go_live_at and page.go_live_at > timezone.now():
|
||||
page.has_unpublished_changes = True
|
||||
# Instead set the approved_go_live_at of this revision
|
||||
revision.approved_go_live_at = page.go_live_at
|
||||
revision.save()
|
||||
# And clear the the approved_go_live_at of any other revisions
|
||||
page.revisions.exclude(id=revision.id).update(approved_go_live_at=None)
|
||||
# if we are updating a currently live page skip the rest
|
||||
if page.live_revision:
|
||||
# Log scheduled publishing
|
||||
if log_action:
|
||||
self.log_scheduling_action()
|
||||
|
||||
return
|
||||
# if we have a go_live in the future don't make the page live
|
||||
page.live = False
|
||||
else:
|
||||
page.live = True
|
||||
# at this point, the page has unpublished changes if and only if there are newer revisions than this one
|
||||
page.has_unpublished_changes = not revision.is_latest_revision()
|
||||
# If page goes live clear the approved_go_live_at of all revisions
|
||||
page.revisions.update(approved_go_live_at=None)
|
||||
page.expired = False # When a page is published it can't be expired
|
||||
|
||||
# Set first_published_at, last_published_at and live_revision
|
||||
# if the page is being published now
|
||||
if page.live:
|
||||
now = timezone.now()
|
||||
page.last_published_at = now
|
||||
page.live_revision = revision
|
||||
|
||||
if page.first_published_at is None:
|
||||
page.first_published_at = now
|
||||
|
||||
if previous_revision:
|
||||
previous_revision_page = previous_revision.as_object()
|
||||
old_page_title = (
|
||||
previous_revision_page.title
|
||||
if page.title != previous_revision_page.title
|
||||
else None
|
||||
)
|
||||
else:
|
||||
try:
|
||||
previous = revision.get_previous()
|
||||
except Revision.DoesNotExist:
|
||||
previous = None
|
||||
old_page_title = (
|
||||
previous.content_object.title
|
||||
if previous and page.title != previous.content_object.title
|
||||
else None
|
||||
)
|
||||
else:
|
||||
# Unset live_revision if the page is going live in the future
|
||||
page.live_revision = None
|
||||
|
||||
page.save()
|
||||
|
||||
for comment in getattr(page, COMMENTS_RELATION_NAME).all().only("position"):
|
||||
for comment in (
|
||||
getattr(self.object, COMMENTS_RELATION_NAME).all().only("position")
|
||||
):
|
||||
comment.save(update_fields=["position"])
|
||||
|
||||
revision.submitted_for_moderation = False
|
||||
page.revisions.update(submitted_for_moderation=False)
|
||||
page_published.send(
|
||||
sender=self.object.specific_class,
|
||||
instance=self.object.specific,
|
||||
revision=self.revision,
|
||||
)
|
||||
|
||||
workflow_state = page.current_workflow_state
|
||||
super()._after_publish()
|
||||
|
||||
workflow_state = self.object.current_workflow_state
|
||||
if workflow_state and getattr(
|
||||
settings, "WAGTAIL_WORKFLOW_CANCEL_ON_PUBLISH", True
|
||||
):
|
||||
workflow_state.cancel(user=user)
|
||||
workflow_state.cancel(user=self.user)
|
||||
|
||||
if page.live:
|
||||
page_published.send(
|
||||
sender=page.specific_class, instance=page.specific, revision=revision
|
||||
)
|
||||
|
||||
# Update alias pages
|
||||
page.update_aliases(revision=revision, user=user, _content=revision.content)
|
||||
|
||||
if log_action:
|
||||
data = None
|
||||
if previous_revision:
|
||||
data = {
|
||||
"revision": {
|
||||
"id": previous_revision.id,
|
||||
"created": previous_revision.created_at.strftime(
|
||||
"%d %b %Y %H:%M"
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
if old_page_title:
|
||||
data = data or {}
|
||||
data["title"] = {
|
||||
"old": old_page_title,
|
||||
"new": page.title,
|
||||
}
|
||||
|
||||
log(
|
||||
instance=page,
|
||||
action="wagtail.rename",
|
||||
user=user,
|
||||
data=data,
|
||||
revision=revision,
|
||||
)
|
||||
|
||||
log(
|
||||
instance=page,
|
||||
action=log_action
|
||||
if isinstance(log_action, str)
|
||||
else "wagtail.publish",
|
||||
user=user,
|
||||
data=data,
|
||||
revision=revision,
|
||||
content_changed=changed,
|
||||
)
|
||||
|
||||
logger.info(
|
||||
'Page published: "%s" id=%d revision_id=%d',
|
||||
page.title,
|
||||
page.id,
|
||||
revision.id,
|
||||
)
|
||||
elif page.go_live_at:
|
||||
logger.info(
|
||||
'Page scheduled for publish: "%s" id=%d revision_id=%d go_live_at=%s',
|
||||
page.title,
|
||||
page.id,
|
||||
revision.id,
|
||||
page.go_live_at.isoformat(),
|
||||
)
|
||||
|
||||
if log_action:
|
||||
self.log_scheduling_action()
|
||||
|
||||
def execute(self, skip_permission_checks=False):
|
||||
self.check(skip_permission_checks=skip_permission_checks)
|
||||
|
||||
return self._publish_page_revision(
|
||||
self.revision,
|
||||
self.page,
|
||||
user=self.user,
|
||||
changed=self.changed,
|
||||
log_action=self.log_action,
|
||||
previous_revision=self.previous_revision,
|
||||
self.object.update_aliases(
|
||||
revision=self.revision, user=self.user, _content=self.revision.content
|
||||
)
|
||||
|
|
|
@ -0,0 +1,215 @@
|
|||
import logging
|
||||
|
||||
from django.core.exceptions import PermissionDenied
|
||||
from django.utils import timezone
|
||||
|
||||
from wagtail.log_actions import log
|
||||
from wagtail.permission_policies.base import ModelPermissionPolicy
|
||||
from wagtail.signals import published
|
||||
|
||||
logger = logging.getLogger("wagtail")
|
||||
|
||||
|
||||
class PublishPermissionError(PermissionDenied):
|
||||
"""
|
||||
Raised when the publish cannot be performed due to insufficient permissions.
|
||||
"""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
class PublishRevisionAction:
|
||||
"""
|
||||
Publish or schedule revision for publishing.
|
||||
|
||||
:param revision: revision to publish
|
||||
:param user: the publishing user
|
||||
:param changed: indicated whether content has changed
|
||||
:param log_action:
|
||||
flag for the logging action. Pass False to skip logging. Cannot pass an action string as the method
|
||||
performs several actions: "publish", "revert" (and publish the reverted revision),
|
||||
"schedule publishing with a live revision", "schedule revision reversal publishing, with a live revision",
|
||||
"schedule publishing", "schedule revision reversal publishing"
|
||||
:param previous_revision: indicates a revision reversal. Should be set to the previous revision instance
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self, revision, user=None, changed=True, log_action=True, previous_revision=None
|
||||
):
|
||||
self.revision = revision
|
||||
self.object = self.revision.as_object()
|
||||
self.permission_policy = ModelPermissionPolicy(type(self.object))
|
||||
self.user = user
|
||||
self.changed = changed
|
||||
self.log_action = log_action
|
||||
self.previous_revision = previous_revision
|
||||
|
||||
def check(self, skip_permission_checks=False):
|
||||
if (
|
||||
self.user
|
||||
and not skip_permission_checks
|
||||
and not self.permission_policy.user_has_permission(self.user, "publish")
|
||||
):
|
||||
raise PublishPermissionError(
|
||||
"You do not have permission to publish this object"
|
||||
)
|
||||
|
||||
def log_scheduling_action(self):
|
||||
log(
|
||||
instance=self.object,
|
||||
action="wagtail.publish.schedule",
|
||||
user=self.user,
|
||||
data={
|
||||
"revision": {
|
||||
"id": self.revision.id,
|
||||
"created": self.revision.created_at.strftime("%d %b %Y %H:%M"),
|
||||
"go_live_at": self.object.go_live_at.strftime("%d %b %Y %H:%M"),
|
||||
"has_live_version": self.object.live,
|
||||
}
|
||||
},
|
||||
revision=self.revision,
|
||||
content_changed=self.changed,
|
||||
)
|
||||
|
||||
def _after_publish(self):
|
||||
published.send(
|
||||
sender=type(self.object),
|
||||
instance=self.object,
|
||||
revision=self.revision,
|
||||
)
|
||||
|
||||
def _publish_revision(
|
||||
self, revision, object, user, changed, log_action, previous_revision
|
||||
):
|
||||
from wagtail.models import Revision
|
||||
|
||||
if object.go_live_at and object.go_live_at > timezone.now():
|
||||
object.has_unpublished_changes = True
|
||||
# Instead set the approved_go_live_at of this revision
|
||||
revision.approved_go_live_at = object.go_live_at
|
||||
revision.save()
|
||||
# And clear the the approved_go_live_at of any other revisions
|
||||
object.revisions.exclude(id=revision.id).update(approved_go_live_at=None)
|
||||
# if we are updating a currently live object skip the rest
|
||||
if object.live_revision:
|
||||
# Log scheduled publishing
|
||||
if log_action:
|
||||
self.log_scheduling_action()
|
||||
|
||||
return
|
||||
# if we have a go_live in the future don't make the object live
|
||||
object.live = False
|
||||
else:
|
||||
object.live = True
|
||||
# at this point, the object has unpublished changes if and only if there are newer revisions than this one
|
||||
object.has_unpublished_changes = not revision.is_latest_revision()
|
||||
# If object goes live clear the approved_go_live_at of all revisions
|
||||
object.revisions.update(approved_go_live_at=None)
|
||||
object.expired = False # When a object is published it can't be expired
|
||||
|
||||
# Set first_published_at, last_published_at and live_revision
|
||||
# if the object is being published now
|
||||
if object.live:
|
||||
now = timezone.now()
|
||||
object.last_published_at = now
|
||||
object.live_revision = revision
|
||||
|
||||
if object.first_published_at is None:
|
||||
object.first_published_at = now
|
||||
|
||||
if previous_revision:
|
||||
previous_revision_object = previous_revision.as_object()
|
||||
old_object_title = (
|
||||
str(previous_revision_object)
|
||||
if str(object) != str(previous_revision_object)
|
||||
else None
|
||||
)
|
||||
else:
|
||||
try:
|
||||
previous = revision.get_previous()
|
||||
except Revision.DoesNotExist:
|
||||
previous = None
|
||||
old_object_title = (
|
||||
str(previous.content_object)
|
||||
if previous and str(object) != str(previous.content_object)
|
||||
else None
|
||||
)
|
||||
else:
|
||||
# Unset live_revision if the object is going live in the future
|
||||
object.live_revision = None
|
||||
|
||||
object.save()
|
||||
|
||||
revision.submitted_for_moderation = False
|
||||
object.revisions.update(submitted_for_moderation=False)
|
||||
|
||||
self._after_publish()
|
||||
|
||||
if object.live:
|
||||
if log_action:
|
||||
data = None
|
||||
if previous_revision:
|
||||
data = {
|
||||
"revision": {
|
||||
"id": previous_revision.id,
|
||||
"created": previous_revision.created_at.strftime(
|
||||
"%d %b %Y %H:%M"
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
if old_object_title:
|
||||
data = data or {}
|
||||
data["title"] = {
|
||||
"old": old_object_title,
|
||||
"new": str(object),
|
||||
}
|
||||
|
||||
log(
|
||||
instance=object,
|
||||
action="wagtail.rename",
|
||||
user=user,
|
||||
data=data,
|
||||
revision=revision,
|
||||
)
|
||||
|
||||
log(
|
||||
instance=object,
|
||||
action=log_action
|
||||
if isinstance(log_action, str)
|
||||
else "wagtail.publish",
|
||||
user=user,
|
||||
data=data,
|
||||
revision=revision,
|
||||
content_changed=changed,
|
||||
)
|
||||
|
||||
logger.info(
|
||||
'Published: "%s" id=%d revision_id=%d',
|
||||
str(object),
|
||||
object.id,
|
||||
revision.id,
|
||||
)
|
||||
elif object.go_live_at:
|
||||
logger.info(
|
||||
'Scheduled for publish: "%s" id=%d revision_id=%d go_live_at=%s',
|
||||
str(object),
|
||||
object.id,
|
||||
revision.id,
|
||||
object.go_live_at.isoformat(),
|
||||
)
|
||||
|
||||
if log_action:
|
||||
self.log_scheduling_action()
|
||||
|
||||
def execute(self, skip_permission_checks=False):
|
||||
self.check(skip_permission_checks=skip_permission_checks)
|
||||
|
||||
return self._publish_revision(
|
||||
self.revision,
|
||||
self.object,
|
||||
user=self.user,
|
||||
changed=self.changed,
|
||||
log_action=self.log_action,
|
||||
previous_revision=self.previous_revision,
|
||||
)
|
|
@ -0,0 +1,89 @@
|
|||
import logging
|
||||
|
||||
from django.core.exceptions import PermissionDenied
|
||||
|
||||
from wagtail.log_actions import log
|
||||
from wagtail.signals import unpublished
|
||||
|
||||
logger = logging.getLogger("wagtail")
|
||||
|
||||
|
||||
class UnpublishPermissionError(PermissionDenied):
|
||||
"""
|
||||
Raised when the object unpublish cannot be performed due to insufficient permissions.
|
||||
"""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
class UnpublishAction:
|
||||
def __init__(
|
||||
self,
|
||||
object,
|
||||
set_expired=False,
|
||||
commit=True,
|
||||
user=None,
|
||||
log_action=True,
|
||||
):
|
||||
self.object = object
|
||||
self.set_expired = set_expired
|
||||
self.commit = commit
|
||||
self.user = user
|
||||
self.log_action = log_action
|
||||
|
||||
def check(self, skip_permission_checks=False):
|
||||
if (
|
||||
self.user
|
||||
and not skip_permission_checks
|
||||
and not self.object.permissions_for_user(self.user).can_unpublish()
|
||||
):
|
||||
raise UnpublishPermissionError(
|
||||
"You do not have permission to unpublish this object"
|
||||
)
|
||||
|
||||
def _after_unpublish(self):
|
||||
unpublished.send(sender=type(object), instance=object)
|
||||
|
||||
def _unpublish_object(self, object, set_expired, commit, user, log_action):
|
||||
"""
|
||||
Unpublish the object by setting ``live`` to ``False``. Does nothing if ``live`` is already ``False``
|
||||
:param log_action: flag for logging the action. Pass False to skip logging. Can be passed an action string.
|
||||
Defaults to 'wagtail.unpublish'
|
||||
"""
|
||||
if object.live:
|
||||
object.live = False
|
||||
object.has_unpublished_changes = True
|
||||
object.live_revision = None
|
||||
|
||||
if set_expired:
|
||||
object.expired = True
|
||||
|
||||
if commit:
|
||||
# using clean=False to bypass validation
|
||||
object.save(clean=False)
|
||||
|
||||
if log_action:
|
||||
log(
|
||||
instance=object,
|
||||
action=log_action
|
||||
if isinstance(log_action, str)
|
||||
else "wagtail.unpublish",
|
||||
user=user,
|
||||
)
|
||||
|
||||
logger.info('Unpublished: "%s" id=%d', str(object), object.id)
|
||||
|
||||
object.revisions.update(approved_go_live_at=None)
|
||||
|
||||
self._after_unpublish()
|
||||
|
||||
def execute(self, skip_permission_checks=False):
|
||||
self.check(skip_permission_checks=skip_permission_checks)
|
||||
|
||||
self._unpublish_object(
|
||||
self.object,
|
||||
set_expired=self.set_expired,
|
||||
commit=self.commit,
|
||||
user=self.user,
|
||||
log_action=self.log_action,
|
||||
)
|
|
@ -1,14 +1,12 @@
|
|||
import logging
|
||||
|
||||
from django.core.exceptions import PermissionDenied
|
||||
|
||||
from wagtail.log_actions import log
|
||||
from wagtail.actions.unpublish import UnpublishAction, UnpublishPermissionError
|
||||
from wagtail.signals import page_unpublished
|
||||
|
||||
logger = logging.getLogger("wagtail")
|
||||
|
||||
|
||||
class UnpublishPagePermissionError(PermissionDenied):
|
||||
class UnpublishPagePermissionError(UnpublishPermissionError):
|
||||
"""
|
||||
Raised when the page unpublish cannot be performed due to insufficient permissions.
|
||||
"""
|
||||
|
@ -16,7 +14,7 @@ class UnpublishPagePermissionError(PermissionDenied):
|
|||
pass
|
||||
|
||||
|
||||
class UnpublishPageAction:
|
||||
class UnpublishPageAction(UnpublishAction):
|
||||
def __init__(
|
||||
self,
|
||||
page,
|
||||
|
@ -26,77 +24,42 @@ class UnpublishPageAction:
|
|||
log_action=True,
|
||||
include_descendants=False,
|
||||
):
|
||||
self.page = page
|
||||
self.set_expired = set_expired
|
||||
self.commit = commit
|
||||
self.user = user
|
||||
self.log_action = log_action
|
||||
super().__init__(
|
||||
page,
|
||||
set_expired=set_expired,
|
||||
commit=commit,
|
||||
user=user,
|
||||
log_action=log_action,
|
||||
)
|
||||
self.include_descendants = include_descendants
|
||||
|
||||
def check(self, skip_permission_checks=False):
|
||||
if (
|
||||
self.user
|
||||
and not skip_permission_checks
|
||||
and not self.page.permissions_for_user(self.user).can_unpublish()
|
||||
):
|
||||
try:
|
||||
super().check(skip_permission_checks)
|
||||
except UnpublishPermissionError as error:
|
||||
raise UnpublishPagePermissionError(
|
||||
"You do not have permission to unpublish this page"
|
||||
)
|
||||
) from error
|
||||
|
||||
def _unpublish_page(self, page, set_expired, commit, user, log_action):
|
||||
"""
|
||||
Unpublish the page by setting ``live`` to ``False``. Does nothing if ``live`` is already ``False``
|
||||
:param log_action: flag for logging the action. Pass False to skip logging. Can be passed an action string.
|
||||
Defaults to 'wagtail.unpublish'
|
||||
"""
|
||||
if page.live:
|
||||
page.live = False
|
||||
page.has_unpublished_changes = True
|
||||
page.live_revision = None
|
||||
def _after_unpublish(self):
|
||||
for alias in self.object.aliases.all():
|
||||
alias.unpublish()
|
||||
|
||||
if set_expired:
|
||||
page.expired = True
|
||||
page_unpublished.send(
|
||||
sender=self.object.specific_class, instance=self.object.specific
|
||||
)
|
||||
|
||||
if commit:
|
||||
# using clean=False to bypass validation
|
||||
page.save(clean=False)
|
||||
|
||||
page_unpublished.send(sender=page.specific_class, instance=page.specific)
|
||||
|
||||
if log_action:
|
||||
log(
|
||||
instance=page,
|
||||
action=log_action
|
||||
if isinstance(log_action, str)
|
||||
else "wagtail.unpublish",
|
||||
user=user,
|
||||
)
|
||||
|
||||
logger.info('Page unpublished: "%s" id=%d', page.title, page.id)
|
||||
|
||||
page.revisions.update(approved_go_live_at=None)
|
||||
|
||||
# Unpublish aliases
|
||||
for alias in page.aliases.all():
|
||||
alias.unpublish()
|
||||
super()._after_unpublish()
|
||||
|
||||
def execute(self, skip_permission_checks=False):
|
||||
self.check(skip_permission_checks=skip_permission_checks)
|
||||
|
||||
self._unpublish_page(
|
||||
self.page,
|
||||
set_expired=self.set_expired,
|
||||
commit=self.commit,
|
||||
user=self.user,
|
||||
log_action=self.log_action,
|
||||
)
|
||||
super().execute(skip_permission_checks)
|
||||
|
||||
if self.include_descendants:
|
||||
from wagtail.models import UserPagePermissionsProxy
|
||||
|
||||
user_perms = UserPagePermissionsProxy(self.user)
|
||||
for live_descendant_page in (
|
||||
self.page.get_descendants()
|
||||
self.object.get_descendants()
|
||||
.live()
|
||||
.defer_streamfields()
|
||||
.specific()
|
||||
|
|
|
@ -57,6 +57,8 @@ from wagtail.actions.create_alias import CreatePageAliasAction
|
|||
from wagtail.actions.delete_page import DeletePageAction
|
||||
from wagtail.actions.move_page import MovePageAction
|
||||
from wagtail.actions.publish_page_revision import PublishPageRevisionAction
|
||||
from wagtail.actions.publish_revision import PublishRevisionAction
|
||||
from wagtail.actions.unpublish import UnpublishAction
|
||||
from wagtail.actions.unpublish_page import UnpublishPageAction
|
||||
from wagtail.coreutils import (
|
||||
WAGTAIL_APPEND_SLASH,
|
||||
|
@ -309,6 +311,10 @@ class RevisionMixin(models.Model):
|
|||
|
||||
return obj
|
||||
|
||||
def _update_from_revision(self, revision, changed=True):
|
||||
self.latest_revision = revision
|
||||
self.save(update_fields=["latest_revision"])
|
||||
|
||||
def save_revision(
|
||||
self,
|
||||
user=None,
|
||||
|
@ -344,8 +350,7 @@ class RevisionMixin(models.Model):
|
|||
object_str=str(self),
|
||||
)
|
||||
|
||||
self.latest_revision = revision
|
||||
self.save(update_fields=["latest_revision"])
|
||||
self._update_from_revision(revision, changed)
|
||||
|
||||
logger.info(
|
||||
'Edited: "%s" pk=%d revision_id=%d', str(self), self.pk, revision.id
|
||||
|
@ -386,7 +391,164 @@ class RevisionMixin(models.Model):
|
|||
abstract = True
|
||||
|
||||
|
||||
class AbstractPage(RevisionMixin, TranslatableMixin, TreebeardPathFixMixin, MP_Node):
|
||||
class DraftStateMixin(models.Model):
|
||||
live = models.BooleanField(verbose_name=_("live"), default=True, editable=False)
|
||||
has_unpublished_changes = models.BooleanField(
|
||||
verbose_name=_("has unpublished changes"), default=False, editable=False
|
||||
)
|
||||
|
||||
first_published_at = models.DateTimeField(
|
||||
verbose_name=_("first published at"), blank=True, null=True, db_index=True
|
||||
)
|
||||
last_published_at = models.DateTimeField(
|
||||
verbose_name=_("last published at"), null=True, editable=False
|
||||
)
|
||||
live_revision = models.ForeignKey(
|
||||
"wagtailcore.Revision",
|
||||
related_name="+",
|
||||
verbose_name=_("live revision"),
|
||||
on_delete=models.SET_NULL,
|
||||
null=True,
|
||||
blank=True,
|
||||
editable=False,
|
||||
)
|
||||
|
||||
go_live_at = models.DateTimeField(
|
||||
verbose_name=_("go live date/time"), blank=True, null=True
|
||||
)
|
||||
expire_at = models.DateTimeField(
|
||||
verbose_name=_("expiry date/time"), blank=True, null=True
|
||||
)
|
||||
expired = models.BooleanField(
|
||||
verbose_name=_("expired"), default=False, editable=False
|
||||
)
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
@classmethod
|
||||
def check(cls, **kwargs):
|
||||
return [
|
||||
*super().check(**kwargs),
|
||||
*cls._check_revision_mixin(),
|
||||
]
|
||||
|
||||
@classmethod
|
||||
def _check_revision_mixin(cls):
|
||||
mro = cls.mro()
|
||||
error = checks.Error(
|
||||
"DraftStateMixin requires RevisionMixin to be applied after DraftStateMixin.",
|
||||
hint="Add RevisionMixin to the model's base classes after DraftStateMixin.",
|
||||
obj=cls,
|
||||
id="wagtailcore.E004",
|
||||
)
|
||||
|
||||
try:
|
||||
if mro.index(RevisionMixin) < mro.index(DraftStateMixin):
|
||||
return [error]
|
||||
except ValueError:
|
||||
return [error]
|
||||
|
||||
return []
|
||||
|
||||
@property
|
||||
def status_string(self):
|
||||
if not self.live:
|
||||
if self.expired:
|
||||
return _("expired")
|
||||
else:
|
||||
return _("draft")
|
||||
else:
|
||||
if self.has_unpublished_changes:
|
||||
return _("live + draft")
|
||||
else:
|
||||
return _("live")
|
||||
|
||||
def publish(
|
||||
self, revision, user=None, changed=True, log_action=True, previous_revision=None
|
||||
):
|
||||
return PublishRevisionAction(
|
||||
revision,
|
||||
user=user,
|
||||
changed=changed,
|
||||
log_action=log_action,
|
||||
previous_revision=previous_revision,
|
||||
).execute()
|
||||
|
||||
def unpublish(self, set_expired=False, commit=True, user=None, log_action=True):
|
||||
return UnpublishAction(
|
||||
self,
|
||||
set_expired=set_expired,
|
||||
commit=commit,
|
||||
user=user,
|
||||
log_action=log_action,
|
||||
).execute()
|
||||
|
||||
def with_content_json(self, content):
|
||||
"""
|
||||
Returns a new version of the object with field values updated to reflect changes
|
||||
in the provided ``content`` (which usually comes from a previously-saved revision).
|
||||
|
||||
Certain field values are preserved in order to prevent errors if the returned
|
||||
object is saved, such as ``id``. The following field values are also preserved,
|
||||
as they are considered to be meaningful to the object as a whole, rather than
|
||||
to a specific revision:
|
||||
|
||||
* ``latest_revision``
|
||||
* ``live``
|
||||
* ``has_unpublished_changes``
|
||||
* ``first_published_at``
|
||||
|
||||
If ``TranslatableMixin`` is applied, the following field values are also preserved:
|
||||
|
||||
* ``translation_key``
|
||||
* ``locale``
|
||||
"""
|
||||
obj = super().with_content_json(content)
|
||||
|
||||
# Ensure other values that are meaningful for the object as a whole (rather than
|
||||
# to a specific revision) are preserved
|
||||
obj.live = self.live
|
||||
obj.has_unpublished_changes = self.has_unpublished_changes
|
||||
obj.first_published_at = self.first_published_at
|
||||
|
||||
return obj
|
||||
|
||||
def get_latest_revision_as_object(self):
|
||||
if not self.has_unpublished_changes:
|
||||
# Use the live database copy in preference to the revision record, as:
|
||||
# 1) this will pick up any changes that have been made directly to the model,
|
||||
# such as automated data imports;
|
||||
# 2) it ensures that inline child objects pick up real database IDs even if
|
||||
# those are absent from the revision data. (If this wasn't the case, the child
|
||||
# objects would be recreated with new IDs on next publish - see #1853)
|
||||
return self
|
||||
|
||||
latest_revision = self.get_latest_revision()
|
||||
|
||||
if latest_revision:
|
||||
return latest_revision.as_object()
|
||||
else:
|
||||
return self
|
||||
|
||||
def _update_from_revision(self, revision, changed=True):
|
||||
update_fields = ["latest_revision"]
|
||||
self.latest_revision = revision
|
||||
|
||||
if changed:
|
||||
self.has_unpublished_changes = True
|
||||
update_fields.append("has_unpublished_changes")
|
||||
|
||||
self.save(update_fields=update_fields)
|
||||
|
||||
|
||||
class AbstractPage(
|
||||
DraftStateMixin,
|
||||
RevisionMixin,
|
||||
TranslatableMixin,
|
||||
TreebeardPathFixMixin,
|
||||
MP_Node,
|
||||
):
|
||||
"""
|
||||
Abstract superclass for Page. According to Django's inheritance rules, managers set on
|
||||
abstract models are inherited by subclasses, but managers set on concrete models that are extended
|
||||
|
@ -422,10 +584,6 @@ class Page(AbstractPage, index.Indexed, ClusterableModel, metaclass=PageBase):
|
|||
related_name="pages",
|
||||
on_delete=models.SET(get_default_page_content_type),
|
||||
)
|
||||
live = models.BooleanField(verbose_name=_("live"), default=True, editable=False)
|
||||
has_unpublished_changes = models.BooleanField(
|
||||
verbose_name=_("has unpublished changes"), default=False, editable=False
|
||||
)
|
||||
url_path = models.TextField(verbose_name=_("URL path"), blank=True, editable=False)
|
||||
owner = models.ForeignKey(
|
||||
settings.AUTH_USER_MODEL,
|
||||
|
@ -462,16 +620,6 @@ class Page(AbstractPage, index.Indexed, ClusterableModel, metaclass=PageBase):
|
|||
),
|
||||
)
|
||||
|
||||
go_live_at = models.DateTimeField(
|
||||
verbose_name=_("go live date/time"), blank=True, null=True
|
||||
)
|
||||
expire_at = models.DateTimeField(
|
||||
verbose_name=_("expiry date/time"), blank=True, null=True
|
||||
)
|
||||
expired = models.BooleanField(
|
||||
verbose_name=_("expired"), default=False, editable=False
|
||||
)
|
||||
|
||||
locked = models.BooleanField(
|
||||
verbose_name=_("locked"), default=False, editable=False
|
||||
)
|
||||
|
@ -488,24 +636,10 @@ class Page(AbstractPage, index.Indexed, ClusterableModel, metaclass=PageBase):
|
|||
related_name="locked_pages",
|
||||
)
|
||||
|
||||
first_published_at = models.DateTimeField(
|
||||
verbose_name=_("first published at"), blank=True, null=True, db_index=True
|
||||
)
|
||||
last_published_at = models.DateTimeField(
|
||||
verbose_name=_("last published at"), null=True, editable=False
|
||||
)
|
||||
latest_revision_created_at = models.DateTimeField(
|
||||
verbose_name=_("latest revision created at"), null=True, editable=False
|
||||
)
|
||||
live_revision = models.ForeignKey(
|
||||
"wagtailcore.Revision",
|
||||
related_name="+",
|
||||
verbose_name=_("live revision"),
|
||||
on_delete=models.SET_NULL,
|
||||
null=True,
|
||||
blank=True,
|
||||
editable=False,
|
||||
)
|
||||
|
||||
_revisions = GenericRelation("wagtailcore.Revision", related_query_name="page")
|
||||
|
||||
# If non-null, this page is an alias of the linked page
|
||||
|
@ -1005,42 +1139,6 @@ class Page(AbstractPage, index.Indexed, ClusterableModel, metaclass=PageBase):
|
|||
else:
|
||||
return self.specific_class.get_verbose_name()
|
||||
|
||||
@property
|
||||
def localized_draft(self):
|
||||
"""
|
||||
Finds the translation in the current active language.
|
||||
|
||||
If there is no translation in the active language, self is returned.
|
||||
|
||||
Note: This will return translations that are in draft. If you want to exclude
|
||||
these, use the ``.localized`` attribute.
|
||||
"""
|
||||
try:
|
||||
locale = Locale.get_active()
|
||||
except (LookupError, Locale.DoesNotExist):
|
||||
return self
|
||||
|
||||
if locale.id == self.locale_id:
|
||||
return self
|
||||
|
||||
return self.get_translation_or_none(locale) or self
|
||||
|
||||
@property
|
||||
def localized(self):
|
||||
"""
|
||||
Finds the translation in the current active language.
|
||||
|
||||
If there is no translation in the active language, self is returned.
|
||||
|
||||
Note: This will not return the translation if it is in draft.
|
||||
If you want to include drafts, use the ``.localized_draft`` attribute instead.
|
||||
"""
|
||||
localized = self.localized_draft
|
||||
if not localized.live:
|
||||
return self
|
||||
|
||||
return localized
|
||||
|
||||
def route(self, request, path_components):
|
||||
if path_components:
|
||||
# request is for a child of this page
|
||||
|
@ -1338,6 +1436,17 @@ class Page(AbstractPage, index.Indexed, ClusterableModel, metaclass=PageBase):
|
|||
|
||||
update_aliases.alters_data = True
|
||||
|
||||
def publish(
|
||||
self, revision, user=None, changed=True, log_action=True, previous_revision=None
|
||||
):
|
||||
return PublishPageRevisionAction(
|
||||
revision,
|
||||
user=user,
|
||||
changed=changed,
|
||||
log_action=log_action,
|
||||
previous_revision=previous_revision,
|
||||
).execute()
|
||||
|
||||
def unpublish(self, set_expired=False, commit=True, user=None, log_action=True):
|
||||
return UnpublishPageAction(
|
||||
self,
|
||||
|
@ -2580,13 +2689,13 @@ class Revision(models.Model):
|
|||
return super().delete()
|
||||
|
||||
def publish(self, user=None, changed=True, log_action=True, previous_revision=None):
|
||||
return PublishPageRevisionAction(
|
||||
return self.content_object.publish(
|
||||
self,
|
||||
user=user,
|
||||
changed=changed,
|
||||
log_action=log_action,
|
||||
previous_revision=previous_revision,
|
||||
).execute()
|
||||
)
|
||||
|
||||
def get_previous(self):
|
||||
return self.get_previous_by_created_at(
|
||||
|
|
|
@ -132,6 +132,27 @@ class TranslatableMixin(models.Model):
|
|||
Finds the translation in the current active language.
|
||||
|
||||
If there is no translation in the active language, self is returned.
|
||||
|
||||
Note: This will not return the translation if it is in draft.
|
||||
If you want to include drafts, use the ``.localized_draft`` attribute instead.
|
||||
"""
|
||||
from wagtail.models import DraftStateMixin
|
||||
|
||||
localized = self.localized_draft
|
||||
if isinstance(self, DraftStateMixin) and not localized.live:
|
||||
return self
|
||||
|
||||
return localized
|
||||
|
||||
@property
|
||||
def localized_draft(self):
|
||||
"""
|
||||
Finds the translation in the current active language.
|
||||
|
||||
If there is no translation in the active language, self is returned.
|
||||
|
||||
Note: This will return translations that are in draft. If you want to exclude
|
||||
these, use the ``.localized`` attribute.
|
||||
"""
|
||||
try:
|
||||
locale = Locale.get_active()
|
||||
|
|
|
@ -1,5 +1,14 @@
|
|||
from django.dispatch import Signal
|
||||
|
||||
# Generic object signals
|
||||
|
||||
# provides args: instance, revision
|
||||
published = Signal()
|
||||
|
||||
# provides args: instance
|
||||
unpublished = Signal()
|
||||
|
||||
|
||||
# Page signals
|
||||
|
||||
# provides args: instance, revision
|
||||
|
|
|
@ -0,0 +1,64 @@
|
|||
from django.apps import apps
|
||||
from django.core import checks
|
||||
from django.db import models
|
||||
from django.test import TestCase
|
||||
|
||||
from wagtail.models import DraftStateMixin, RevisionMixin
|
||||
|
||||
|
||||
class TestDraftStateMixin(TestCase):
|
||||
def tearDown(self):
|
||||
# Unregister the models from the overall model registry
|
||||
# so that it doesn't break tests elsewhere.
|
||||
# We can probably replace this with Django's @isolate_apps decorator.
|
||||
for package in ("wagtailcore", "wagtail.tests"):
|
||||
try:
|
||||
for model in (
|
||||
"draftstatewithoutrevisionmodel",
|
||||
"draftstateincorrectrevisionmodel",
|
||||
"draftstatewithrevisionmodel",
|
||||
):
|
||||
del apps.all_models[package][model]
|
||||
except KeyError:
|
||||
pass
|
||||
apps.clear_cache()
|
||||
|
||||
def test_missing_revision_mixin(self):
|
||||
class DraftStateWithoutRevisionModel(DraftStateMixin, models.Model):
|
||||
pass
|
||||
|
||||
self.assertEqual(
|
||||
DraftStateWithoutRevisionModel.check(),
|
||||
[
|
||||
checks.Error(
|
||||
"DraftStateMixin requires RevisionMixin to be applied after DraftStateMixin.",
|
||||
hint="Add RevisionMixin to the model's base classes after DraftStateMixin.",
|
||||
obj=DraftStateWithoutRevisionModel,
|
||||
id="wagtailcore.E004",
|
||||
)
|
||||
],
|
||||
)
|
||||
|
||||
def test_incorrect_revision_mixin_order(self):
|
||||
class DraftStateIncorrectRevisionModel(
|
||||
RevisionMixin, DraftStateMixin, models.Model
|
||||
):
|
||||
pass
|
||||
|
||||
self.assertEqual(
|
||||
DraftStateIncorrectRevisionModel.check(),
|
||||
[
|
||||
checks.Error(
|
||||
"DraftStateMixin requires RevisionMixin to be applied after DraftStateMixin.",
|
||||
hint="Add RevisionMixin to the model's base classes after DraftStateMixin.",
|
||||
obj=DraftStateIncorrectRevisionModel,
|
||||
id="wagtailcore.E004",
|
||||
)
|
||||
],
|
||||
)
|
||||
|
||||
def test_correct_model(self):
|
||||
class DraftStateWithRevisionModel(DraftStateMixin, RevisionMixin, models.Model):
|
||||
pass
|
||||
|
||||
self.assertEqual(DraftStateWithRevisionModel.check(), [])
|
Ładowanie…
Reference in New Issue