Initial commit
This commit is contained in:
0
env/lib/python3.10/site-packages/wagtail/actions/__init__.py
vendored
Normal file
0
env/lib/python3.10/site-packages/wagtail/actions/__init__.py
vendored
Normal file
BIN
env/lib/python3.10/site-packages/wagtail/actions/__pycache__/__init__.cpython-310.pyc
vendored
Normal file
BIN
env/lib/python3.10/site-packages/wagtail/actions/__pycache__/__init__.cpython-310.pyc
vendored
Normal file
Binary file not shown.
BIN
env/lib/python3.10/site-packages/wagtail/actions/__pycache__/convert_alias.cpython-310.pyc
vendored
Normal file
BIN
env/lib/python3.10/site-packages/wagtail/actions/__pycache__/convert_alias.cpython-310.pyc
vendored
Normal file
Binary file not shown.
BIN
env/lib/python3.10/site-packages/wagtail/actions/__pycache__/copy_for_translation.cpython-310.pyc
vendored
Normal file
BIN
env/lib/python3.10/site-packages/wagtail/actions/__pycache__/copy_for_translation.cpython-310.pyc
vendored
Normal file
Binary file not shown.
BIN
env/lib/python3.10/site-packages/wagtail/actions/__pycache__/copy_page.cpython-310.pyc
vendored
Normal file
BIN
env/lib/python3.10/site-packages/wagtail/actions/__pycache__/copy_page.cpython-310.pyc
vendored
Normal file
Binary file not shown.
BIN
env/lib/python3.10/site-packages/wagtail/actions/__pycache__/create_alias.cpython-310.pyc
vendored
Normal file
BIN
env/lib/python3.10/site-packages/wagtail/actions/__pycache__/create_alias.cpython-310.pyc
vendored
Normal file
Binary file not shown.
BIN
env/lib/python3.10/site-packages/wagtail/actions/__pycache__/delete_page.cpython-310.pyc
vendored
Normal file
BIN
env/lib/python3.10/site-packages/wagtail/actions/__pycache__/delete_page.cpython-310.pyc
vendored
Normal file
Binary file not shown.
BIN
env/lib/python3.10/site-packages/wagtail/actions/__pycache__/move_page.cpython-310.pyc
vendored
Normal file
BIN
env/lib/python3.10/site-packages/wagtail/actions/__pycache__/move_page.cpython-310.pyc
vendored
Normal file
Binary file not shown.
BIN
env/lib/python3.10/site-packages/wagtail/actions/__pycache__/publish_page_revision.cpython-310.pyc
vendored
Normal file
BIN
env/lib/python3.10/site-packages/wagtail/actions/__pycache__/publish_page_revision.cpython-310.pyc
vendored
Normal file
Binary file not shown.
BIN
env/lib/python3.10/site-packages/wagtail/actions/__pycache__/publish_revision.cpython-310.pyc
vendored
Normal file
BIN
env/lib/python3.10/site-packages/wagtail/actions/__pycache__/publish_revision.cpython-310.pyc
vendored
Normal file
Binary file not shown.
BIN
env/lib/python3.10/site-packages/wagtail/actions/__pycache__/revert_to_page_revision.cpython-310.pyc
vendored
Normal file
BIN
env/lib/python3.10/site-packages/wagtail/actions/__pycache__/revert_to_page_revision.cpython-310.pyc
vendored
Normal file
Binary file not shown.
BIN
env/lib/python3.10/site-packages/wagtail/actions/__pycache__/unpublish.cpython-310.pyc
vendored
Normal file
BIN
env/lib/python3.10/site-packages/wagtail/actions/__pycache__/unpublish.cpython-310.pyc
vendored
Normal file
Binary file not shown.
BIN
env/lib/python3.10/site-packages/wagtail/actions/__pycache__/unpublish_page.cpython-310.pyc
vendored
Normal file
BIN
env/lib/python3.10/site-packages/wagtail/actions/__pycache__/unpublish_page.cpython-310.pyc
vendored
Normal file
Binary file not shown.
69
env/lib/python3.10/site-packages/wagtail/actions/convert_alias.py
vendored
Normal file
69
env/lib/python3.10/site-packages/wagtail/actions/convert_alias.py
vendored
Normal file
@@ -0,0 +1,69 @@
|
||||
from django.core.exceptions import PermissionDenied
|
||||
|
||||
from wagtail.log_actions import log
|
||||
|
||||
|
||||
class ConvertAliasPageError(RuntimeError):
|
||||
"""
|
||||
Raised when the page to convert is not an alias.
|
||||
"""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
class ConvertAliasPagePermissionError(PermissionDenied):
|
||||
"""
|
||||
Raised when the alias page conversion cannot be performed due to insufficient permissions.
|
||||
"""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
class ConvertAliasPageAction:
|
||||
def __init__(self, page, *, log_action="wagtail.convert_alias", user=None):
|
||||
self.page = page
|
||||
self.log_action = log_action
|
||||
self.user = user
|
||||
|
||||
def check(self, skip_permission_checks=False):
|
||||
if not self.page.alias_of_id:
|
||||
raise ConvertAliasPageError("Page must be an alias to be converted.")
|
||||
|
||||
if (
|
||||
not skip_permission_checks
|
||||
and self.user
|
||||
and not self.page.permissions_for_user(self.user).can_edit()
|
||||
):
|
||||
raise ConvertAliasPagePermissionError(
|
||||
"You do not have permission to edit this page."
|
||||
)
|
||||
|
||||
def _convert_alias(self, page, log_action, user):
|
||||
page.alias_of_id = None
|
||||
page.save(update_fields=["alias_of_id"], clean=False)
|
||||
|
||||
# Create an initial revision
|
||||
revision = page.save_revision(user=user, changed=False, clean=False)
|
||||
|
||||
if page.live:
|
||||
page.live_revision = revision
|
||||
page.save(update_fields=["live_revision"], clean=False)
|
||||
|
||||
# Log
|
||||
if log_action:
|
||||
log(
|
||||
instance=page,
|
||||
action=log_action,
|
||||
revision=revision,
|
||||
user=user,
|
||||
data={
|
||||
"page": {"id": page.id, "title": page.get_admin_display_title()},
|
||||
},
|
||||
)
|
||||
|
||||
return page
|
||||
|
||||
def execute(self, skip_permission_checks=False):
|
||||
self.check(skip_permission_checks=skip_permission_checks)
|
||||
|
||||
return self._convert_alias(self.page, self.log_action, self.user)
|
||||
245
env/lib/python3.10/site-packages/wagtail/actions/copy_for_translation.py
vendored
Normal file
245
env/lib/python3.10/site-packages/wagtail/actions/copy_for_translation.py
vendored
Normal file
@@ -0,0 +1,245 @@
|
||||
from django.core.exceptions import PermissionDenied
|
||||
from django.db import transaction
|
||||
|
||||
from wagtail.coreutils import find_available_slug
|
||||
from wagtail.models.copying import _copy
|
||||
from wagtail.signals import copy_for_translation_done
|
||||
|
||||
|
||||
class ParentNotTranslatedError(Exception):
|
||||
"""
|
||||
Raised when a call to Page.copy_for_translation is made but the
|
||||
parent page is not translated and copy_parents is False.
|
||||
"""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
class CopyForTranslationPermissionError(PermissionDenied):
|
||||
"""
|
||||
Raised when the object translation copy cannot be performed due to insufficient permissions.
|
||||
"""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
class CopyPageForTranslationPermissionError(CopyForTranslationPermissionError):
|
||||
pass
|
||||
|
||||
|
||||
class CopyPageForTranslationAction:
|
||||
"""
|
||||
Creates a copy of this page in the specified locale.
|
||||
|
||||
The new page will be created in draft as a child of this page's translated
|
||||
parent.
|
||||
|
||||
For example, if you are translating a blog post from English into French,
|
||||
this method will look for the French version of the blog index and create
|
||||
the French translation of the blog post under that.
|
||||
|
||||
If this page's parent is not translated into the locale, then a ``ParentNotTranslatedError``
|
||||
is raised. You can circumvent this error by passing ``copy_parents=True`` which
|
||||
copies any parents that are not translated yet.
|
||||
|
||||
The ``exclude_fields`` parameter can be used to set any fields to a blank value
|
||||
in the copy.
|
||||
|
||||
Note that this method calls the ``.copy()`` method internally so any fields that
|
||||
are excluded in ``.exclude_fields_in_copy`` will be excluded from the translation.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
page,
|
||||
locale,
|
||||
copy_parents=False,
|
||||
alias=False,
|
||||
exclude_fields=None,
|
||||
user=None,
|
||||
include_subtree=False,
|
||||
):
|
||||
self.page = page
|
||||
self.locale = locale
|
||||
self.copy_parents = copy_parents
|
||||
self.alias = alias
|
||||
self.exclude_fields = exclude_fields
|
||||
self.user = user
|
||||
self.include_subtree = include_subtree
|
||||
|
||||
def check(self, skip_permission_checks=False):
|
||||
# Permission checks
|
||||
if (
|
||||
self.user
|
||||
and not skip_permission_checks
|
||||
and not self.user.has_perms(["simple_translation.submit_translation"])
|
||||
):
|
||||
raise CopyPageForTranslationPermissionError(
|
||||
"You do not have permission to submit a translation for this page."
|
||||
)
|
||||
|
||||
def walk(self, current_page):
|
||||
for child_page in current_page.get_children():
|
||||
translated_page = self._copy_for_translation(
|
||||
child_page
|
||||
if child_page.live
|
||||
else child_page.get_latest_revision_as_object(),
|
||||
self.locale,
|
||||
self.copy_parents,
|
||||
self.alias,
|
||||
self.exclude_fields,
|
||||
)
|
||||
|
||||
copy_for_translation_done.send(
|
||||
sender=self.__class__,
|
||||
source_obj=child_page.specific,
|
||||
target_obj=translated_page,
|
||||
)
|
||||
|
||||
self.walk(child_page)
|
||||
|
||||
@transaction.atomic
|
||||
def _copy_for_translation(self, page, locale, copy_parents, alias, exclude_fields):
|
||||
# Find the translated version of the parent page to create the new page under
|
||||
parent = page.get_parent().specific
|
||||
slug = page.slug
|
||||
|
||||
if not parent.is_root():
|
||||
try:
|
||||
translated_parent = parent.get_translation(locale)
|
||||
except parent.__class__.DoesNotExist:
|
||||
if not copy_parents:
|
||||
raise ParentNotTranslatedError("Parent page is not translated.")
|
||||
|
||||
translated_parent = parent.copy_for_translation(
|
||||
locale, copy_parents=True, alias=True
|
||||
)
|
||||
else:
|
||||
# Don't duplicate the root page for translation. Create new locale as a sibling
|
||||
translated_parent = parent
|
||||
|
||||
# Append language code to slug as the new page
|
||||
# will be created in the same section as the existing one
|
||||
slug += "-" + locale.language_code
|
||||
|
||||
# Find available slug for new page
|
||||
slug = find_available_slug(translated_parent, slug)
|
||||
|
||||
if alias:
|
||||
return page.create_alias(
|
||||
parent=translated_parent,
|
||||
update_slug=slug,
|
||||
update_locale=locale,
|
||||
reset_translation_key=False,
|
||||
)
|
||||
|
||||
else:
|
||||
# Update locale on translatable child objects as well
|
||||
def process_child_object(
|
||||
original_page, page_copy, child_relation, child_object
|
||||
):
|
||||
from wagtail.models import TranslatableMixin
|
||||
|
||||
if isinstance(child_object, TranslatableMixin):
|
||||
child_object.locale = locale
|
||||
|
||||
return page.copy(
|
||||
to=translated_parent,
|
||||
update_attrs={
|
||||
"locale": locale,
|
||||
"slug": slug,
|
||||
},
|
||||
copy_revisions=False,
|
||||
keep_live=False,
|
||||
reset_translation_key=False,
|
||||
process_child_object=process_child_object,
|
||||
exclude_fields=exclude_fields,
|
||||
log_action="wagtail.copy_for_translation",
|
||||
)
|
||||
|
||||
def execute(self, skip_permission_checks=False):
|
||||
self.check(skip_permission_checks=skip_permission_checks)
|
||||
|
||||
translated_page = self._copy_for_translation(
|
||||
self.page if self.page.live else self.page.get_latest_revision_as_object(),
|
||||
self.locale,
|
||||
self.copy_parents,
|
||||
self.alias,
|
||||
self.exclude_fields,
|
||||
)
|
||||
|
||||
copy_for_translation_done.send(
|
||||
sender=self.__class__,
|
||||
source_obj=self.page,
|
||||
target_obj=translated_page,
|
||||
)
|
||||
|
||||
if self.include_subtree:
|
||||
self.walk(self.page)
|
||||
|
||||
return translated_page
|
||||
|
||||
|
||||
class CopyForTranslationAction:
|
||||
"""
|
||||
Creates a copy of this object in the specified locale.
|
||||
|
||||
The ``exclude_fields`` parameter can be used to set any fields to a blank value
|
||||
in the copy.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
object,
|
||||
locale,
|
||||
exclude_fields=None,
|
||||
user=None,
|
||||
):
|
||||
self.object = object
|
||||
self.locale = locale
|
||||
self.exclude_fields = exclude_fields
|
||||
self.user = user
|
||||
|
||||
def check(self, skip_permission_checks=False):
|
||||
# Permission checks
|
||||
if (
|
||||
self.user
|
||||
and not skip_permission_checks
|
||||
and not self.user.has_perms(["simple_translation.submit_translation"])
|
||||
):
|
||||
raise CopyForTranslationPermissionError(
|
||||
"You do not have permission to submit a translation for this object."
|
||||
)
|
||||
|
||||
@transaction.atomic
|
||||
def _copy_for_translation(self, object, locale, exclude_fields=None):
|
||||
from wagtail.models import DraftStateMixin, TranslatableMixin
|
||||
|
||||
# Make sure the copy includes the latest changes, including draft
|
||||
if isinstance(object, DraftStateMixin):
|
||||
object = object.get_latest_revision_as_object()
|
||||
|
||||
exclude_fields = (
|
||||
getattr(object, "default_exclude_fields_in_copy", [])
|
||||
+ getattr(object, "exclude_fields_in_copy", [])
|
||||
+ (exclude_fields or [])
|
||||
)
|
||||
translated, child_object_map = _copy(object, exclude_fields=exclude_fields)
|
||||
translated.locale = locale
|
||||
|
||||
# Update locale on any translatable child objects as well
|
||||
# Note: If this is not a subclass of ClusterableModel, child_object_map will always be '{}'
|
||||
for (_child_relation, _old_pk), child_object in child_object_map.items():
|
||||
if isinstance(child_object, TranslatableMixin):
|
||||
child_object.locale = locale
|
||||
|
||||
return translated
|
||||
|
||||
def execute(self, skip_permission_checks=False):
|
||||
self.check(skip_permission_checks=skip_permission_checks)
|
||||
|
||||
translated_object = self._copy_for_translation(
|
||||
self.object, self.locale, self.exclude_fields
|
||||
)
|
||||
|
||||
return translated_object
|
||||
376
env/lib/python3.10/site-packages/wagtail/actions/copy_page.py
vendored
Normal file
376
env/lib/python3.10/site-packages/wagtail/actions/copy_page.py
vendored
Normal file
@@ -0,0 +1,376 @@
|
||||
import logging
|
||||
import uuid
|
||||
|
||||
from django.core.exceptions import PermissionDenied
|
||||
from modelcluster.models import get_all_child_relations
|
||||
|
||||
from wagtail.log_actions import log
|
||||
from wagtail.models.copying import _copy, _copy_m2m_relations
|
||||
from wagtail.models.i18n import TranslatableMixin
|
||||
from wagtail.signals import page_published
|
||||
|
||||
logger = logging.getLogger("wagtail")
|
||||
|
||||
|
||||
class CopyPageIntegrityError(RuntimeError):
|
||||
"""
|
||||
Raised when the page copy cannot be performed for data integrity reasons.
|
||||
"""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
class CopyPagePermissionError(PermissionDenied):
|
||||
"""
|
||||
Raised when the page copy cannot be performed due to insufficient permissions.
|
||||
"""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
class CopyPageAction:
|
||||
"""
|
||||
Copies pages and page trees.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
page,
|
||||
to=None,
|
||||
update_attrs=None,
|
||||
exclude_fields=None,
|
||||
recursive=False,
|
||||
copy_revisions=True,
|
||||
keep_live=True,
|
||||
user=None,
|
||||
process_child_object=None,
|
||||
log_action="wagtail.copy",
|
||||
reset_translation_key=True,
|
||||
):
|
||||
# Note: These four parameters don't apply to any copied children
|
||||
self.page = page
|
||||
self.to = to
|
||||
self.update_attrs = update_attrs
|
||||
self.exclude_fields = exclude_fields
|
||||
|
||||
self.recursive = recursive
|
||||
self.copy_revisions = copy_revisions
|
||||
self.keep_live = keep_live
|
||||
self.user = user
|
||||
self.process_child_object = process_child_object
|
||||
self.log_action = log_action
|
||||
self.reset_translation_key = reset_translation_key
|
||||
self._uuid_mapping = {}
|
||||
|
||||
def generate_translation_key(self, old_uuid):
|
||||
"""
|
||||
Generates a new UUID if it isn't already being used.
|
||||
Otherwise it will return the same UUID if it's already in use.
|
||||
"""
|
||||
if old_uuid not in self._uuid_mapping:
|
||||
self._uuid_mapping[old_uuid] = uuid.uuid4()
|
||||
|
||||
return self._uuid_mapping[old_uuid]
|
||||
|
||||
def check(self, skip_permission_checks=False):
|
||||
# Essential data model checks
|
||||
if self.page._state.adding:
|
||||
raise CopyPageIntegrityError("Page.copy() called on an unsaved page")
|
||||
|
||||
if (
|
||||
self.to
|
||||
and self.recursive
|
||||
and (self.to.id == self.page.id or self.to.is_descendant_of(self.page))
|
||||
):
|
||||
raise CopyPageIntegrityError(
|
||||
"You cannot copy a tree branch recursively into itself"
|
||||
)
|
||||
|
||||
# Permission checks
|
||||
if self.user and not skip_permission_checks:
|
||||
to = self.to
|
||||
if to is None:
|
||||
to = self.page.get_parent()
|
||||
|
||||
if not self.page.permissions_for_user(self.user).can_copy_to(
|
||||
to, self.recursive
|
||||
):
|
||||
raise CopyPagePermissionError(
|
||||
"You do not have permission to copy this page"
|
||||
)
|
||||
|
||||
if self.keep_live:
|
||||
destination_perms = self.to.permissions_for_user(self.user)
|
||||
|
||||
if not destination_perms.can_publish_subpage():
|
||||
raise CopyPagePermissionError(
|
||||
"You do not have permission to publish a page at the destination"
|
||||
)
|
||||
|
||||
def _copy_page(
|
||||
self, page, to=None, update_attrs=None, exclude_fields=None, _mpnode_attrs=None
|
||||
):
|
||||
specific_page = page.specific
|
||||
exclude_fields = (
|
||||
specific_page.default_exclude_fields_in_copy
|
||||
+ specific_page.exclude_fields_in_copy
|
||||
+ (exclude_fields or [])
|
||||
)
|
||||
if self.keep_live:
|
||||
base_update_attrs = {
|
||||
"alias_of": None,
|
||||
}
|
||||
else:
|
||||
base_update_attrs = {
|
||||
"live": False,
|
||||
"has_unpublished_changes": True,
|
||||
"live_revision": None,
|
||||
"first_published_at": None,
|
||||
"last_published_at": None,
|
||||
"alias_of": None,
|
||||
}
|
||||
|
||||
if self.user:
|
||||
base_update_attrs["owner"] = self.user
|
||||
|
||||
# When we're not copying for translation, we should give the translation_key a new value
|
||||
if self.reset_translation_key:
|
||||
base_update_attrs["translation_key"] = uuid.uuid4()
|
||||
|
||||
if update_attrs:
|
||||
base_update_attrs.update(update_attrs)
|
||||
|
||||
page_copy, child_object_map = _copy(
|
||||
specific_page, exclude_fields=exclude_fields, update_attrs=base_update_attrs
|
||||
)
|
||||
# Save copied child objects and run process_child_object on them if we need to
|
||||
for (child_relation, old_pk), child_object in child_object_map.items():
|
||||
if self.process_child_object:
|
||||
self.process_child_object(
|
||||
specific_page, page_copy, child_relation, child_object
|
||||
)
|
||||
|
||||
if self.reset_translation_key and isinstance(
|
||||
child_object, TranslatableMixin
|
||||
):
|
||||
child_object.translation_key = self.generate_translation_key(
|
||||
child_object.translation_key
|
||||
)
|
||||
|
||||
# Save the new page
|
||||
if _mpnode_attrs:
|
||||
# We've got a tree position already reserved. Perform a quick save
|
||||
page_copy.path = _mpnode_attrs[0]
|
||||
page_copy.depth = _mpnode_attrs[1]
|
||||
page_copy.save(clean=False)
|
||||
|
||||
else:
|
||||
if to:
|
||||
page_copy = to.add_child(instance=page_copy)
|
||||
else:
|
||||
page_copy = page.add_sibling(instance=page_copy)
|
||||
|
||||
_mpnode_attrs = (page_copy.path, page_copy.depth)
|
||||
|
||||
_copy_m2m_relations(
|
||||
specific_page,
|
||||
page_copy,
|
||||
exclude_fields=exclude_fields,
|
||||
update_attrs=base_update_attrs,
|
||||
)
|
||||
|
||||
# Copy revisions
|
||||
if self.copy_revisions:
|
||||
for revision in page.revisions.all():
|
||||
use_as_latest_revision = revision.pk == page.latest_revision_id
|
||||
revision.pk = None
|
||||
revision.approved_go_live_at = None
|
||||
revision.object_id = page_copy.id
|
||||
|
||||
# Update ID fields in content
|
||||
revision_content = revision.content
|
||||
revision_content["pk"] = page_copy.pk
|
||||
|
||||
for child_relation in get_all_child_relations(specific_page):
|
||||
accessor_name = child_relation.get_accessor_name()
|
||||
try:
|
||||
child_objects = revision_content[accessor_name]
|
||||
except KeyError:
|
||||
# KeyErrors are possible if the revision was created
|
||||
# before this child relation was added to the database
|
||||
continue
|
||||
|
||||
for child_object in child_objects:
|
||||
child_object[child_relation.field.name] = page_copy.pk
|
||||
# Remap primary key to copied versions
|
||||
# If the primary key is not recognised (eg, the child object has been deleted from the database)
|
||||
# set the primary key to None
|
||||
copied_child_object = child_object_map.get(
|
||||
(child_relation, child_object["pk"])
|
||||
)
|
||||
child_object["pk"] = (
|
||||
copied_child_object.pk if copied_child_object else None
|
||||
)
|
||||
if (
|
||||
self.reset_translation_key
|
||||
and "translation_key" in child_object
|
||||
):
|
||||
child_object[
|
||||
"translation_key"
|
||||
] = self.generate_translation_key(
|
||||
child_object["translation_key"]
|
||||
)
|
||||
|
||||
for exclude_field in specific_page.exclude_fields_in_copy:
|
||||
if exclude_field in revision_content and hasattr(
|
||||
page_copy, exclude_field
|
||||
):
|
||||
revision_content[exclude_field] = getattr(
|
||||
page_copy, exclude_field, None
|
||||
)
|
||||
|
||||
revision.content = revision_content
|
||||
|
||||
# Save
|
||||
revision.save()
|
||||
# If this revision was designated the latest revision, update the page copy to point to the copied revision
|
||||
if use_as_latest_revision:
|
||||
page_copy.latest_revision = revision
|
||||
|
||||
# Create a new revision
|
||||
# This code serves a few purposes:
|
||||
# * It makes sure update_attrs gets applied to the latest revision
|
||||
# * It bumps the last_revision_created_at value so the new page gets ordered as if it was just created
|
||||
# * It sets the user of the new revision so it's possible to see who copied the page by looking at its history
|
||||
latest_revision = page_copy.get_latest_revision_as_object()
|
||||
|
||||
if update_attrs:
|
||||
for field, value in update_attrs.items():
|
||||
setattr(latest_revision, field, value)
|
||||
|
||||
latest_revision_as_page_revision = latest_revision.save_revision(
|
||||
user=self.user, changed=False, clean=False
|
||||
)
|
||||
|
||||
# save_revision should have updated this in the database - update the in-memory copy for consistency
|
||||
page_copy.latest_revision = latest_revision_as_page_revision
|
||||
|
||||
if self.keep_live:
|
||||
page_copy.live_revision = latest_revision_as_page_revision
|
||||
page_copy.last_published_at = latest_revision_as_page_revision.created_at
|
||||
page_copy.first_published_at = latest_revision_as_page_revision.created_at
|
||||
# The call to save_revision above will have updated several fields of the page record, including
|
||||
# draft_title and latest_revision. These changes are not reflected in page_copy, so we must only
|
||||
# update the specific fields set above to avoid overwriting them.
|
||||
page_copy.save(
|
||||
clean=False,
|
||||
update_fields=[
|
||||
"live_revision",
|
||||
"last_published_at",
|
||||
"first_published_at",
|
||||
],
|
||||
)
|
||||
|
||||
if page_copy.live:
|
||||
page_published.send(
|
||||
sender=page_copy.specific_class,
|
||||
instance=page_copy,
|
||||
revision=latest_revision_as_page_revision,
|
||||
)
|
||||
|
||||
# Log
|
||||
if self.log_action:
|
||||
parent = specific_page.get_parent()
|
||||
log(
|
||||
instance=page_copy,
|
||||
action=self.log_action,
|
||||
user=self.user,
|
||||
data={
|
||||
"page": {
|
||||
"id": page_copy.id,
|
||||
"title": page_copy.get_admin_display_title(),
|
||||
"locale": {
|
||||
"id": page_copy.locale_id,
|
||||
"language_code": page_copy.locale.language_code,
|
||||
},
|
||||
},
|
||||
"source": {
|
||||
"id": parent.id,
|
||||
"title": parent.specific_deferred.get_admin_display_title(),
|
||||
}
|
||||
if parent
|
||||
else None,
|
||||
"destination": {
|
||||
"id": to.id,
|
||||
"title": to.specific_deferred.get_admin_display_title(),
|
||||
}
|
||||
if to
|
||||
else None,
|
||||
"keep_live": page_copy.live and self.keep_live,
|
||||
"source_locale": {
|
||||
"id": page.locale_id,
|
||||
"language_code": page.locale.language_code,
|
||||
},
|
||||
},
|
||||
)
|
||||
if page_copy.live and self.keep_live:
|
||||
# Log the publish if the use chose to keep the copied page live
|
||||
log(
|
||||
instance=page_copy,
|
||||
action="wagtail.publish",
|
||||
user=self.user,
|
||||
revision=latest_revision_as_page_revision,
|
||||
)
|
||||
logger.info(
|
||||
'Page copied: "%s" id=%d from=%d', page_copy.title, page_copy.id, page.id
|
||||
)
|
||||
|
||||
# Copy child pages
|
||||
from wagtail.models import Page, PageViewRestriction
|
||||
|
||||
if self.recursive:
|
||||
numchild = 0
|
||||
|
||||
for child_page in page.get_children().specific().iterator():
|
||||
newdepth = _mpnode_attrs[1] + 1
|
||||
child_mpnode_attrs = (
|
||||
Page._get_path(_mpnode_attrs[0], newdepth, numchild),
|
||||
newdepth,
|
||||
)
|
||||
numchild += 1
|
||||
self._copy_page(
|
||||
child_page, to=page_copy, _mpnode_attrs=child_mpnode_attrs
|
||||
)
|
||||
|
||||
if numchild > 0:
|
||||
page_copy.numchild = numchild
|
||||
page_copy.save(clean=False, update_fields=["numchild"])
|
||||
|
||||
# Copy across any view restrictions defined directly on the page,
|
||||
# unless the destination page already has view restrictions defined
|
||||
if to:
|
||||
parent_page_restriction = to.get_view_restrictions()
|
||||
else:
|
||||
parent_page_restriction = self.page.get_parent().get_view_restrictions()
|
||||
|
||||
if not parent_page_restriction.exists():
|
||||
for view_restriction in self.page.view_restrictions.all():
|
||||
view_restriction_copy = PageViewRestriction(
|
||||
restriction_type=view_restriction.restriction_type,
|
||||
password=view_restriction.password,
|
||||
page=page_copy,
|
||||
)
|
||||
view_restriction_copy.save(user=self.user)
|
||||
view_restriction_copy.groups.set(view_restriction.groups.all())
|
||||
|
||||
return page_copy
|
||||
|
||||
def execute(self, skip_permission_checks=False):
|
||||
self.check(skip_permission_checks=skip_permission_checks)
|
||||
|
||||
return self._copy_page(
|
||||
self.page,
|
||||
to=self.to,
|
||||
update_attrs=self.update_attrs,
|
||||
exclude_fields=self.exclude_fields,
|
||||
)
|
||||
267
env/lib/python3.10/site-packages/wagtail/actions/create_alias.py
vendored
Normal file
267
env/lib/python3.10/site-packages/wagtail/actions/create_alias.py
vendored
Normal file
@@ -0,0 +1,267 @@
|
||||
import logging
|
||||
import uuid
|
||||
|
||||
from django.core.exceptions import PermissionDenied
|
||||
|
||||
from wagtail.log_actions import log
|
||||
from wagtail.models.copying import _copy, _copy_m2m_relations
|
||||
from wagtail.models.i18n import TranslatableMixin
|
||||
|
||||
logger = logging.getLogger("wagtail")
|
||||
|
||||
|
||||
class CreatePageAliasIntegrityError(RuntimeError):
|
||||
"""
|
||||
Raised when creating an alias of a page cannot be performed for data integrity reasons.
|
||||
"""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
class CreatePageAliasPermissionError(PermissionDenied):
|
||||
"""
|
||||
Raised when creating an alias of a page cannot be performed due to insufficient permissions.
|
||||
"""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
class CreatePageAliasAction:
|
||||
"""
|
||||
Creates an alias of the given page.
|
||||
|
||||
An alias is like a copy, but an alias remains in sync with the original page. They
|
||||
are not directly editable and do not have revisions.
|
||||
|
||||
You can convert an alias into a regular page by setting the .alias_of attribute to None
|
||||
and creating an initial revision.
|
||||
|
||||
:param recursive: create aliases of the page's subtree, defaults to False
|
||||
:type recursive: boolean, optional
|
||||
:param parent: The page to create the new alias under
|
||||
:type parent: Page, optional
|
||||
:param update_slug: The slug of the new alias page, defaults to the slug of the original page
|
||||
:type update_slug: string, optional
|
||||
:param update_locale: The locale of the new alias page, defaults to the locale of the original page
|
||||
:type update_locale: Locale, optional
|
||||
:param user: The user who is performing this action. This user would be assigned as the owner of the new page and appear in the audit log
|
||||
:type user: User, optional
|
||||
:param log_action: Override the log action with a custom one. or pass None to skip logging, defaults to 'wagtail.create_alias'
|
||||
:type log_action: string or None, optional
|
||||
:param reset_translation_key: Generate new translation_keys for the page and any translatable child objects, defaults to False
|
||||
:type reset_translation_key: boolean, optional
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
page,
|
||||
*,
|
||||
recursive=False,
|
||||
parent=None,
|
||||
update_slug=None,
|
||||
update_locale=None,
|
||||
user=None,
|
||||
log_action="wagtail.create_alias",
|
||||
reset_translation_key=True,
|
||||
_mpnode_attrs=None,
|
||||
):
|
||||
self.page = page
|
||||
self.recursive = recursive
|
||||
self.parent = parent
|
||||
self.update_slug = update_slug
|
||||
self.update_locale = update_locale
|
||||
self.user = user
|
||||
self.log_action = log_action
|
||||
self.reset_translation_key = reset_translation_key
|
||||
self._mpnode_attrs = _mpnode_attrs
|
||||
|
||||
def check(self, skip_permission_checks=False):
|
||||
parent = self.parent or self.page.get_parent()
|
||||
if self.recursive and (
|
||||
parent == self.page or parent.is_descendant_of(self.page)
|
||||
):
|
||||
raise CreatePageAliasIntegrityError(
|
||||
"You cannot copy a tree branch recursively into itself"
|
||||
)
|
||||
|
||||
if (
|
||||
self.user
|
||||
and not skip_permission_checks
|
||||
and not parent.permissions_for_user(self.user).can_publish_subpage()
|
||||
):
|
||||
raise CreatePageAliasPermissionError(
|
||||
"You do not have permission to publish a page at the destination"
|
||||
)
|
||||
|
||||
def _create_alias(
|
||||
self,
|
||||
page,
|
||||
*,
|
||||
recursive,
|
||||
parent,
|
||||
update_slug,
|
||||
update_locale,
|
||||
user,
|
||||
log_action,
|
||||
reset_translation_key,
|
||||
_mpnode_attrs,
|
||||
):
|
||||
specific_page = page.specific
|
||||
|
||||
# FIXME: Switch to the same fields that are excluded from copy
|
||||
# We can't do this right now because we can't exclude fields from with_content_json
|
||||
# which we use for updating aliases
|
||||
exclude_fields = [
|
||||
"id",
|
||||
"path",
|
||||
"depth",
|
||||
"numchild",
|
||||
"url_path",
|
||||
"path",
|
||||
"index_entries",
|
||||
"postgres_index_entries",
|
||||
]
|
||||
|
||||
update_attrs = {
|
||||
"alias_of": page,
|
||||
# Aliases don't have revisions so the draft title should always match the live title
|
||||
"draft_title": page.title,
|
||||
# Likewise, an alias page can't have unpublished changes if it's live
|
||||
"has_unpublished_changes": not page.live,
|
||||
}
|
||||
|
||||
if update_slug:
|
||||
update_attrs["slug"] = update_slug
|
||||
|
||||
if update_locale:
|
||||
update_attrs["locale"] = update_locale
|
||||
|
||||
if user:
|
||||
update_attrs["owner"] = user
|
||||
|
||||
# When we're not copying for translation, we should give the translation_key a new value
|
||||
if reset_translation_key:
|
||||
update_attrs["translation_key"] = uuid.uuid4()
|
||||
|
||||
alias, child_object_map = _copy(
|
||||
specific_page, update_attrs=update_attrs, exclude_fields=exclude_fields
|
||||
)
|
||||
|
||||
# Update any translatable child objects
|
||||
for child_object in child_object_map.values():
|
||||
if isinstance(child_object, TranslatableMixin):
|
||||
if update_locale:
|
||||
child_object.locale = update_locale
|
||||
|
||||
# When we're not copying for translation,
|
||||
# we should give the translation_key a new value for each child object as well.
|
||||
if reset_translation_key:
|
||||
child_object.translation_key = uuid.uuid4()
|
||||
|
||||
# Save the new page
|
||||
if _mpnode_attrs:
|
||||
# We've got a tree position already reserved. Perform a quick save.
|
||||
alias.path = _mpnode_attrs[0]
|
||||
alias.depth = _mpnode_attrs[1]
|
||||
alias.save(clean=False)
|
||||
|
||||
else:
|
||||
if parent:
|
||||
alias = parent.add_child(instance=alias)
|
||||
else:
|
||||
alias = page.add_sibling(instance=alias)
|
||||
|
||||
_mpnode_attrs = (alias.path, alias.depth)
|
||||
|
||||
_copy_m2m_relations(specific_page, alias, exclude_fields=exclude_fields)
|
||||
|
||||
# Log
|
||||
if log_action:
|
||||
source_parent = specific_page.get_parent()
|
||||
log(
|
||||
instance=alias,
|
||||
action=log_action,
|
||||
user=user,
|
||||
data={
|
||||
"page": {"id": alias.id, "title": alias.get_admin_display_title()},
|
||||
"source": {
|
||||
"id": source_parent.id,
|
||||
"title": source_parent.specific_deferred.get_admin_display_title(),
|
||||
}
|
||||
if source_parent
|
||||
else None,
|
||||
"destination": {
|
||||
"id": parent.id,
|
||||
"title": parent.specific_deferred.get_admin_display_title(),
|
||||
}
|
||||
if parent
|
||||
else None,
|
||||
},
|
||||
)
|
||||
|
||||
logger.info(
|
||||
'Page alias created: "%s" id=%d from=%d', alias.title, alias.id, page.id
|
||||
)
|
||||
|
||||
from wagtail.models import Page, PageViewRestriction
|
||||
|
||||
# Copy child pages
|
||||
if recursive:
|
||||
numchild = 0
|
||||
|
||||
for child_page in page.get_children().specific().iterator():
|
||||
newdepth = _mpnode_attrs[1] + 1
|
||||
child_mpnode_attrs = (
|
||||
Page._get_path(_mpnode_attrs[0], newdepth, numchild),
|
||||
newdepth,
|
||||
)
|
||||
numchild += 1
|
||||
self._create_alias(
|
||||
child_page,
|
||||
recursive=True,
|
||||
parent=alias,
|
||||
update_slug=None,
|
||||
update_locale=update_locale,
|
||||
user=user,
|
||||
log_action=log_action,
|
||||
reset_translation_key=reset_translation_key,
|
||||
_mpnode_attrs=child_mpnode_attrs,
|
||||
)
|
||||
|
||||
if numchild > 0:
|
||||
alias.numchild = numchild
|
||||
alias.save(clean=False, update_fields=["numchild"])
|
||||
|
||||
# Copy across any view restrictions defined directly on the page,
|
||||
# unless the destination page already has view restrictions defined
|
||||
if parent:
|
||||
parent_page_restriction = parent.get_view_restrictions()
|
||||
else:
|
||||
parent_page_restriction = page.get_parent().get_view_restrictions()
|
||||
|
||||
if not parent_page_restriction.exists():
|
||||
for view_restriction in page.view_restrictions.all():
|
||||
view_restriction_copy = PageViewRestriction(
|
||||
restriction_type=view_restriction.restriction_type,
|
||||
password=view_restriction.password,
|
||||
page=alias,
|
||||
)
|
||||
view_restriction_copy.save(user=self.user)
|
||||
view_restriction_copy.groups.set(view_restriction.groups.all())
|
||||
|
||||
return alias
|
||||
|
||||
def execute(self, skip_permission_checks=False):
|
||||
self.check(skip_permission_checks=skip_permission_checks)
|
||||
|
||||
return self._create_alias(
|
||||
self.page,
|
||||
recursive=self.recursive,
|
||||
parent=self.parent,
|
||||
update_slug=self.update_slug,
|
||||
update_locale=self.update_locale,
|
||||
user=self.user,
|
||||
log_action=self.log_action,
|
||||
reset_translation_key=self.reset_translation_key,
|
||||
_mpnode_attrs=self._mpnode_attrs,
|
||||
)
|
||||
59
env/lib/python3.10/site-packages/wagtail/actions/delete_page.py
vendored
Normal file
59
env/lib/python3.10/site-packages/wagtail/actions/delete_page.py
vendored
Normal file
@@ -0,0 +1,59 @@
|
||||
from django.core.exceptions import PermissionDenied
|
||||
|
||||
from wagtail.log_actions import log
|
||||
|
||||
|
||||
class DeletePagePermissionError(PermissionDenied):
|
||||
"""
|
||||
Raised when the page delete cannot be performed due to insufficient permissions.
|
||||
"""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
class DeletePageAction:
|
||||
def __init__(self, page, user):
|
||||
self.page = page
|
||||
self.user = user
|
||||
|
||||
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_delete()
|
||||
):
|
||||
raise DeletePagePermissionError(
|
||||
"You do not have permission to delete this page"
|
||||
)
|
||||
|
||||
def _delete_page(self, page, *args, **kwargs):
|
||||
from wagtail.models import Page
|
||||
|
||||
# Ensure that deletion always happens on an instance of Page, not a specific subclass. This
|
||||
# works around a bug in treebeard <= 3.0 where calling SpecificPage.delete() fails to delete
|
||||
# child pages that are not instances of SpecificPage
|
||||
if type(page) is Page:
|
||||
for child in page.get_descendants().specific().iterator():
|
||||
self.log_deletion(child)
|
||||
self.log_deletion(page.specific)
|
||||
|
||||
# this is a Page instance, so carry on as we were
|
||||
return super(Page, page).delete(*args, **kwargs)
|
||||
else:
|
||||
# retrieve an actual Page instance and delete that instead of page
|
||||
return DeletePageAction(
|
||||
Page.objects.get(id=page.id), user=self.user
|
||||
).execute(*args, **kwargs)
|
||||
|
||||
def execute(self, *args, skip_permission_checks=False, **kwargs):
|
||||
self.check(skip_permission_checks=skip_permission_checks)
|
||||
|
||||
return self._delete_page(self.page, *args, **kwargs)
|
||||
|
||||
def log_deletion(self, page):
|
||||
log(
|
||||
instance=page,
|
||||
action="wagtail.delete",
|
||||
user=self.user,
|
||||
deleted=True,
|
||||
)
|
||||
107
env/lib/python3.10/site-packages/wagtail/actions/move_page.py
vendored
Normal file
107
env/lib/python3.10/site-packages/wagtail/actions/move_page.py
vendored
Normal file
@@ -0,0 +1,107 @@
|
||||
import logging
|
||||
|
||||
from django.core.exceptions import PermissionDenied
|
||||
from django.db import transaction
|
||||
from treebeard.mp_tree import MP_MoveHandler
|
||||
|
||||
from wagtail.log_actions import log
|
||||
from wagtail.signals import post_page_move, pre_page_move
|
||||
|
||||
logger = logging.getLogger("wagtail")
|
||||
|
||||
|
||||
class MovePagePermissionError(PermissionDenied):
|
||||
"""
|
||||
Raised when the page move cannot be performed due to insufficient permissions.
|
||||
"""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
class MovePageAction:
|
||||
def __init__(self, page, target, pos=None, user=None):
|
||||
self.page = page
|
||||
self.target = target
|
||||
self.pos = pos
|
||||
self.user = user
|
||||
|
||||
def check(self, parent_after, skip_permission_checks=False):
|
||||
if self.user and not skip_permission_checks:
|
||||
if not self.page.permissions_for_user(self.user).can_move_to(parent_after):
|
||||
raise MovePagePermissionError(
|
||||
"You do not have permission to move the page to the target specified."
|
||||
)
|
||||
|
||||
def _move_page(self, page, target, parent_after):
|
||||
from wagtail.models import Page
|
||||
|
||||
# Determine old and new url_paths
|
||||
# Fetching new object to avoid affecting `page`
|
||||
parent_before = page.get_parent()
|
||||
old_page = Page.objects.get(id=page.id)
|
||||
old_url_path = old_page.url_path
|
||||
new_url_path = old_page.set_url_path(parent=parent_after)
|
||||
url_path_changed = old_url_path != new_url_path
|
||||
|
||||
# Emit pre_page_move signal
|
||||
pre_page_move.send(
|
||||
sender=page.specific_class or page.__class__,
|
||||
instance=page,
|
||||
parent_page_before=parent_before,
|
||||
parent_page_after=parent_after,
|
||||
url_path_before=old_url_path,
|
||||
url_path_after=new_url_path,
|
||||
)
|
||||
|
||||
# Only commit when all descendants are properly updated
|
||||
with transaction.atomic():
|
||||
# Allow treebeard to update `path` values
|
||||
MP_MoveHandler(page, target, self.pos).process()
|
||||
|
||||
# Treebeard's move method doesn't actually update the in-memory instance,
|
||||
# so we need to work with a freshly loaded one now
|
||||
new_page = Page.objects.get(id=page.id)
|
||||
new_page.url_path = new_url_path
|
||||
new_page.save()
|
||||
|
||||
# Update descendant paths if url_path has changed
|
||||
if url_path_changed:
|
||||
new_page._update_descendant_url_paths(old_url_path, new_url_path)
|
||||
|
||||
# Emit post_page_move signal
|
||||
post_page_move.send(
|
||||
sender=page.specific_class or page.__class__,
|
||||
instance=new_page,
|
||||
parent_page_before=parent_before,
|
||||
parent_page_after=parent_after,
|
||||
url_path_before=old_url_path,
|
||||
url_path_after=new_url_path,
|
||||
)
|
||||
|
||||
# Log
|
||||
log(
|
||||
instance=page,
|
||||
action="wagtail.move" if url_path_changed else "wagtail.reorder",
|
||||
user=self.user,
|
||||
data={
|
||||
"source": {
|
||||
"id": parent_before.id,
|
||||
"title": parent_before.specific_deferred.get_admin_display_title(),
|
||||
},
|
||||
"destination": {
|
||||
"id": parent_after.id,
|
||||
"title": parent_after.specific_deferred.get_admin_display_title(),
|
||||
},
|
||||
},
|
||||
)
|
||||
logger.info('Page moved: "%s" id=%d path=%s', page.title, page.id, new_url_path)
|
||||
|
||||
def execute(self, skip_permission_checks=False):
|
||||
if self.pos in ("first-child", "last-child", "sorted-child"):
|
||||
parent_after = self.target
|
||||
else:
|
||||
parent_after = self.target.get_parent()
|
||||
|
||||
self.check(parent_after, skip_permission_checks=skip_permission_checks)
|
||||
|
||||
return self._move_page(self.page, self.target, parent_after)
|
||||
63
env/lib/python3.10/site-packages/wagtail/actions/publish_page_revision.py
vendored
Normal file
63
env/lib/python3.10/site-packages/wagtail/actions/publish_page_revision.py
vendored
Normal file
@@ -0,0 +1,63 @@
|
||||
import logging
|
||||
|
||||
from wagtail.actions.publish_revision import (
|
||||
PublishPermissionError,
|
||||
PublishRevisionAction,
|
||||
)
|
||||
from wagtail.signals import page_published
|
||||
|
||||
logger = logging.getLogger("wagtail")
|
||||
|
||||
|
||||
class PublishPagePermissionError(PublishPermissionError):
|
||||
"""
|
||||
Raised when the page publish cannot be performed due to insufficient permissions.
|
||||
"""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
class PublishPageRevisionAction(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 check(self, skip_permission_checks: bool = False):
|
||||
if (
|
||||
self.user
|
||||
and not skip_permission_checks
|
||||
and not self.object.permissions_for_user(self.user).can_publish()
|
||||
):
|
||||
raise PublishPagePermissionError(
|
||||
"You do not have permission to publish this page"
|
||||
)
|
||||
|
||||
def _after_publish(self):
|
||||
from wagtail.models import COMMENTS_RELATION_NAME
|
||||
|
||||
for comment in (
|
||||
getattr(self.object, COMMENTS_RELATION_NAME).all().only("position")
|
||||
):
|
||||
comment.save(update_fields=["position"])
|
||||
|
||||
page_published.send(
|
||||
sender=self.object.specific_class,
|
||||
instance=self.object.specific,
|
||||
revision=self.revision,
|
||||
)
|
||||
|
||||
super()._after_publish()
|
||||
|
||||
self.object.update_aliases(
|
||||
revision=self.revision, _content=self.revision.content
|
||||
)
|
||||
238
env/lib/python3.10/site-packages/wagtail/actions/publish_revision.py
vendored
Normal file
238
env/lib/python3.10/site-packages/wagtail/actions/publish_revision.py
vendored
Normal file
@@ -0,0 +1,238 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from typing import TYPE_CHECKING, Optional
|
||||
|
||||
from django.conf import settings
|
||||
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
|
||||
from wagtail.utils.timestamps import ensure_utc
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from wagtail.models import Revision
|
||||
|
||||
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: Revision,
|
||||
user=None,
|
||||
changed: bool = True,
|
||||
log_action: bool = True,
|
||||
previous_revision: Optional[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": ensure_utc(self.revision.created_at),
|
||||
"go_live_at": ensure_utc(self.object.go_live_at),
|
||||
"has_live_version": self.object.live,
|
||||
}
|
||||
},
|
||||
revision=self.revision,
|
||||
content_changed=self.changed,
|
||||
)
|
||||
|
||||
def _after_publish(self):
|
||||
from wagtail.models import WorkflowMixin
|
||||
|
||||
published.send(
|
||||
sender=type(self.object),
|
||||
instance=self.object,
|
||||
revision=self.revision,
|
||||
)
|
||||
|
||||
if isinstance(self.object, WorkflowMixin):
|
||||
workflow_state = self.object.current_workflow_state
|
||||
if workflow_state and getattr(
|
||||
settings, "WAGTAIL_WORKFLOW_CANCEL_ON_PUBLISH", True
|
||||
):
|
||||
workflow_state.cancel(user=self.user)
|
||||
|
||||
def _publish_revision(
|
||||
self,
|
||||
revision: Revision,
|
||||
object,
|
||||
user,
|
||||
changed,
|
||||
log_action: bool,
|
||||
previous_revision: Optional[Revision] = None,
|
||||
):
|
||||
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 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()
|
||||
|
||||
self._after_publish()
|
||||
|
||||
if object.live:
|
||||
if log_action:
|
||||
data = None
|
||||
if previous_revision:
|
||||
data = {
|
||||
"revision": {
|
||||
"id": previous_revision.id,
|
||||
"created": ensure_utc(previous_revision.created_at),
|
||||
}
|
||||
}
|
||||
|
||||
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" pk=%s revision_id=%d',
|
||||
str(object),
|
||||
str(object.pk),
|
||||
revision.id,
|
||||
)
|
||||
elif object.go_live_at:
|
||||
logger.info(
|
||||
'Scheduled for publish: "%s" pk=%s revision_id=%d go_live_at=%s',
|
||||
str(object),
|
||||
str(object.pk),
|
||||
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,
|
||||
)
|
||||
65
env/lib/python3.10/site-packages/wagtail/actions/revert_to_page_revision.py
vendored
Normal file
65
env/lib/python3.10/site-packages/wagtail/actions/revert_to_page_revision.py
vendored
Normal file
@@ -0,0 +1,65 @@
|
||||
from django.core.exceptions import PermissionDenied
|
||||
|
||||
|
||||
class RevertToPageRevisionError(RuntimeError):
|
||||
"""
|
||||
Raised when the revision revert cannot be performed for data reasons.
|
||||
"""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
class RevertToPageRevisionPermissionError(PermissionDenied):
|
||||
"""
|
||||
Raised when the revision revert cannot be performed due to insufficient permissions.
|
||||
"""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
class RevertToPageRevisionAction:
|
||||
def __init__(
|
||||
self,
|
||||
page,
|
||||
revision,
|
||||
user=None,
|
||||
log_action="wagtail.revert",
|
||||
approved_go_live_at=None,
|
||||
changed=True,
|
||||
clean=True,
|
||||
):
|
||||
self.page = page
|
||||
self.revision = revision
|
||||
self.user = user
|
||||
self.log_action = log_action
|
||||
self.approved_go_live_at = approved_go_live_at
|
||||
self.changed = changed
|
||||
self.clean = clean
|
||||
|
||||
def check(self, skip_permission_checks=False):
|
||||
if self.page.alias_of_id:
|
||||
raise RevertToPageRevisionError(
|
||||
"Revisions are not required for alias pages as they are an exact copy of another page."
|
||||
)
|
||||
|
||||
# Permission checks
|
||||
if (
|
||||
self.user
|
||||
and not skip_permission_checks
|
||||
and not self.page.permissions_for_user(self.user).can_edit()
|
||||
):
|
||||
raise RevertToPageRevisionPermissionError(
|
||||
"You do not have permission to edit this page"
|
||||
)
|
||||
|
||||
def execute(self, skip_permission_checks=False):
|
||||
self.check(skip_permission_checks=skip_permission_checks)
|
||||
|
||||
return self.revision.as_object().save_revision(
|
||||
previous_revision=self.revision,
|
||||
user=self.user,
|
||||
log_action=self.log_action,
|
||||
approved_go_live_at=self.approved_go_live_at,
|
||||
changed=self.changed,
|
||||
clean=self.clean,
|
||||
)
|
||||
91
env/lib/python3.10/site-packages/wagtail/actions/unpublish.py
vendored
Normal file
91
env/lib/python3.10/site-packages/wagtail/actions/unpublish.py
vendored
Normal file
@@ -0,0 +1,91 @@
|
||||
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 _commit_unpublish(self, object):
|
||||
object.save()
|
||||
|
||||
def _after_unpublish(self, object):
|
||||
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:
|
||||
self._commit_unpublish(object)
|
||||
|
||||
if log_action:
|
||||
log(
|
||||
instance=object,
|
||||
action=log_action
|
||||
if isinstance(log_action, str)
|
||||
else "wagtail.unpublish",
|
||||
user=user,
|
||||
)
|
||||
|
||||
logger.info('Unpublished: "%s" pk=%s', str(object), str(object.pk))
|
||||
|
||||
object.revisions.update(approved_go_live_at=None)
|
||||
|
||||
self._after_unpublish(object)
|
||||
|
||||
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,
|
||||
)
|
||||
69
env/lib/python3.10/site-packages/wagtail/actions/unpublish_page.py
vendored
Normal file
69
env/lib/python3.10/site-packages/wagtail/actions/unpublish_page.py
vendored
Normal file
@@ -0,0 +1,69 @@
|
||||
import logging
|
||||
|
||||
from wagtail.actions.unpublish import UnpublishAction, UnpublishPermissionError
|
||||
from wagtail.signals import page_unpublished
|
||||
|
||||
logger = logging.getLogger("wagtail")
|
||||
|
||||
|
||||
class UnpublishPagePermissionError(UnpublishPermissionError):
|
||||
"""
|
||||
Raised when the page unpublish cannot be performed due to insufficient permissions.
|
||||
"""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
class UnpublishPageAction(UnpublishAction):
|
||||
def __init__(
|
||||
self,
|
||||
page,
|
||||
set_expired=False,
|
||||
commit=True,
|
||||
user=None,
|
||||
log_action=True,
|
||||
include_descendants=False,
|
||||
):
|
||||
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):
|
||||
try:
|
||||
super().check(skip_permission_checks)
|
||||
except UnpublishPermissionError as error:
|
||||
raise UnpublishPagePermissionError(
|
||||
"You do not have permission to unpublish this page"
|
||||
) from error
|
||||
|
||||
def _commit_unpublish(self, object):
|
||||
# using clean=False to bypass validation
|
||||
object.save(clean=False)
|
||||
|
||||
def _after_unpublish(self, object):
|
||||
for alias in object.aliases.all():
|
||||
alias.unpublish(log_action=False)
|
||||
|
||||
page_unpublished.send(sender=object.specific_class, instance=object.specific)
|
||||
|
||||
super()._after_unpublish(object)
|
||||
|
||||
def execute(self, skip_permission_checks=False):
|
||||
super().execute(skip_permission_checks)
|
||||
|
||||
if self.include_descendants:
|
||||
for live_descendant_page in (
|
||||
self.object.get_descendants()
|
||||
.live()
|
||||
.defer_streamfields()
|
||||
.specific()
|
||||
.iterator()
|
||||
):
|
||||
action = UnpublishPageAction(live_descendant_page)
|
||||
if live_descendant_page.permissions_for_user(self.user).can_unpublish():
|
||||
action.execute(skip_permission_checks=True)
|
||||
Reference in New Issue
Block a user