在Wagtail中,如何防止页面被编辑删除,或者设置最小页面数之类的

问题描述 投票:0回答:1

我们开发的网站有两种类型的页面,我们称它们为...

  • “静态”页面:主页、联系方式、关于我们、一般目录页面、一般博客页面...
  • “动态”的:目录中的每个集合,每个博客条目。

作为开发人员,我们为最终客户提供静态结构的所有结构,这意味着他们根本不必创建它们。这有很多原因,但主要是因为网站结构依赖于这些页面始终存在(顺便说一句,处于已发布状态,但让我们把它放在一边)。

我们让并教客户的编辑做其他事情..

  • 只需编辑现有的“静态”页面。
  • 在目录和博客中创建、编辑和删除子页面。

但是我们想阻止他们删除任何其他页面。事情可能会爆炸,比如,主页显示了从其他部分收集的一些信息等......但主要是因为它在我们的情况下没有意义,即:他们可能会删除其中一个的唯一情况“静态”页面是偶然的.

如果他们真的想删除这些页面中的任何一个,那是一个涉及其他人(经理、设计师和开发人员)的决策设计,并且需要许多步骤来执行,包括发布新版本。

我们已经通过将

max_count = 1
设置为这些页面来阻止用户创建他们不应该创建的页面。我们可以通过编辑组 ofc 中的“创建”权限实现相同的效果。

如果有一个

min_count
的财产会很棒,或者更好的是一个
min_published_count
的,但是没有。

根据this issue的讨论,在权限级别控制它不是也不会是一个选项。

我想一个解决方法是拦截模型的

delete()
方法,但这可能无法阻止我猜的批量删除,但好吧,我会试一试。虽然这感觉像是一个尴尬的解决方法......

有没有人有类似情况?或者关于那种情况最干净的方法是什么的任何想法?

提前非常感谢:拥抱:

作为旁注,澄清一下我们有意识地选择尽可能限制编辑的权力,因为我们相信由于这种限制带来的清晰性和安全性,这会给他们带来最好的体验。 相比之下,在其他系统中,他们面临着巨大的选择和权力,导致他们进入令人生畏的学习曲线,并且不断担心意外破坏网站。例如,删除页面具有极大的破坏性,会使他们公司的页面处于不良或无法使用的状态,唯一的解决方案通常是恢复数据库备份。

编辑:使用钩子的部分解决方案

感谢罗伯特的回答,我一直在尝试使用钩子来实现它。

# wagtail_hooks.py
from wagtail import hooks


@hooks.register('construct_page_action_menu')
def remove_disabled_actions(menu_items, request, context):
    specific_page = context["page"].specific
    if not specific_page.is_submitable:
        menu_items[:] = [item for item in menu_items if item.name != 'action-submit']
    if not specific_page.is_unpublishable:
        menu_items[:] = [item for item in menu_items if item.name != 'action-unpublish']


@hooks.register('construct_page_listing_buttons')
def remove_page_listing_button_item(buttons, page, page_perms, context=None):
    specific_page = page.specific
    if not specific_page.show_more_dropdown_in_list_actions:
        buttons.pop() # removes the last button, which is the 'more' dropdown.
# models.py
class BasePage(Page):
    [ ... fields etc. ]
    
    is_submitable = False
    is_unpublishable = False
    # Removing this dropdown is also removing the "Delete" page option that it
    # contains. If you enable it, make sure that you actually pretend to give
    # the editor access to every action it provides!
    show_more_dropdown_in_list_actions = False

此解决方案缺乏:

  • 要么从列表视图中完全删除批量操作按钮,要么决定包含哪些按钮。
  • 在页面编辑视图中,通过面包屑从页面顶部的三个点的上下文菜单中删除删除取消发布和其他选项。

因此到目前为止是无用的,因为编辑器仍然可以以某些方式执行操作,但这是向前迈出的一步。

django wagtail
1个回答
0
投票

试试这个。

#_____________________________________________________________________________
class HomePageMixin(
    MenuCaptionMixin,
    ShowTocMixin,
    ImageMixin,
    ShowImageMixin,
    SubTitleMixin,
    TitlesColorMixin,
    AsideMixin,
    Page,
    RevisionMixin
    ):

    max_count = MAX_SITES
    is_movable = False
    is_unpublishable = False
    @property
    def is_copyable(self):
        return self.get_parent().specific.can_add_homepage

    @property
    def is_deletable(self):
        return self.get_parent().get_children().count() > 1
    
    
    
#_____________________________________________________________________________
@hooks.register('register_custom_page_listing_more_buttons')
def custom_page_listing_more_buttons(page, page_perms, is_parent=False, next_url=None):
    specific_page = page.specific
    admin_display_title = page.get_admin_display_title()
    
    if page_perms.can_move() and specific_page.is_movable:
        yield Button(
            _('Move'),
            reverse('wagtailadmin_pages:move', args=[page.id]),
            attrs={"title": _("Move page '{}'").format(admin_display_title)},
            priority=10
        )
    if page_perms.can_copy() and specific_page.is_copyable:
        url = reverse('wagtailadmin_pages:copy', args=[page.id])
        if next_url:
            url += '?' + urlencode({'next': next_url})
        yield Button(
            _('Copy'),
            url,
            attrs={'title': _("Copy page '{}'").format(admin_display_title)},
            priority=20
        )
    if page_perms.can_delete() and specific_page.is_deletable:
        url = reverse('wagtailadmin_pages:delete', args=[page.id])
        # After deleting the page, it is impossible to redirect to it.
        if next_url == reverse('wagtailadmin_explore', args=[page.id]):
            next_url = None
        if next_url:
            url += '?' + urlencode({'next': next_url})
        yield Button(
            _('Delete'),
            url,
            attrs={'title': _("Delete page '{}'").format(admin_display_title)},
            priority=30
        )
    if page_perms.can_unpublish() and specific_page.is_unpublishable:
        url = reverse('wagtailadmin_pages:unpublish', args=[page.id])
        if next_url:
            url += '?' + urlencode({'next': next_url})
        yield Button(
            _('Unpublish'),
            url,
            attrs={'title': _("Unpublish page '{}'").format(admin_display_title)},
            priority=40
        )
    if page_perms.can_view_revisions():
        yield Button(
            _('Revisions'),
            reverse('wagtailadmin_pages:history', args=[page.id]),
            attrs={'title': _("View page history for '{}'").format(admin_display_title)},
            priority=50
        )
    if is_parent:
        yield Button(
            _('Sort menu order'),
            '?ordering=ord',
            attrs={'title': _("Change ordering of child pages of '{}'").format(admin_display_title)},
            priority=60
        )
© www.soinside.com 2019 - 2024. All rights reserved.