我可以限制Wagtail管理员图像选择器中的可见集合吗?

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

我有一些“分组和收藏夹”设置,以利用w的收藏夹功能。

我仅将收藏夹A限制为管理员

[以非管理员身份登录并单击“选择图像”按钮以调出图像选择器后,下拉菜单中出现“收藏夹”,其中包括我的所有收藏夹,包括受限制的收藏夹A

是否可以仅显示用户拥有的收藏夹和图像,类似于“图像”菜单项的工作方式?

Wagtail:1.12.2Django:1.8.18

django wagtail
1个回答
0
投票

我知道问这个问题已经有一段时间了,但是希望这个答案对某些人有所帮助。

解决方案概述

Wagtail 1.10(在本文中,2.10正在开发中)引入了一个有用的钩子,称为construct_image_chooser_queryset。该挂钩将截取图像,然后将其返回给图像选择器模态(在所有Wagtail管理员中使用),并使开发人员可以自定义返回的图像。

挂钩也可以访问请求,从那里您可以计算出用户并相应地建立图像查询。

您问题的另一部分涉及collections下拉字段,这需要一些Django模板工作,但是可以实现,而无需太多额外的复杂性。任何具有自定义模板的construct_image_chooser_queryset,只需使用现有模板路径将其命名即可。

用于呈现集合下拉列表的Django共享模板(包括),在发布时为Wagtail admin template can be overridden

示例代码

注:代码不是完整的解决方案,因为问题没有更改收集模型,但希望它将是一个很好的开始。

1。 g图像选择器查询集挂钩

wagtail/admin/templates/wagtailadmin/shared/collection_chooser.html

2。集合选择器模板替代

  • 2a文件:../ templates / wagtailadmin / shared / collection_chooser.html
  • 我们要覆盖模板并将其重定向到自定义一个模板,前提是模板的上下文中存在图像
hooks documentation
  • 2b文件:../ templates / base / include / images_collection_chooser.html
  • 仅当上下文中有图像时才使用此模板
  • 这里我们修改上下文以删除图像和filter集合,然后渲染原始的Wagtail管理模板
from wagtail.core import hooks


@hooks.register('construct_image_chooser_queryset')
def show_my_uploaded_images_only(images, request):
    # Example: Only show uploaded images based on current user
    # actual filtering will need to be based on the collection's linked user group
    images = images.filter(uploaded_by_user=request.user)

    return images
  • 2c文件:bakerydemo / base / templatetags / filter_with_permissions.py
  • 这里我们制作执行集合过滤的Django {% extends images|yesno:"base/include/images_collection_chooser.html,wagtailadmin/shared/collection_chooser.html" %}
{% load filter_with_permissions %}

{% with images=None %}
    {% with collections=collections|filter_with_permissions:user %}
      Images Only Collection Template:
      {% include "wagtailadmin/shared/collection_chooser.html" %}
    {% endwith %}
{% endwith %}
© www.soinside.com 2019 - 2024. All rights reserved.