Django / Wagtail:如何检查用户是否具有访问给定页面的权限?

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

我是Wagtail的新手,到目前为止,这是很棒的经历!

我正在尝试解决以下问题:用户可以看到所有可用子页面的列表,并且,如果他没有访问每个页面的权限,它将显示一个储物柜图标,如所附图片所示。

Wireframe/sketch

我正在使用私有页面,特定组中的用户可以访问。

基本上,我有以下代码:

{% for course in page.get_children %}
<h2> <a href="{{ course.url }} "> {{ course.title }}
    </a>
</h2>
{% endfor %}

是否可以检查属性以了解用户是否对循环中的每个course具有权限?

我的模特:

from django.db import models
from wagtail.core.models import Page

# Create your models here.
class CoursePage(Page):
    """
    A Page...
    """

    description = models.TextField(
        help_text='Text to describe the course',
        blank=True)

    subpage_types = ['course_module.ModulePage']


class ModulePage(Page):
    description = models.TextField(
        help_text='Text to describe the module',
        blank=True)


    subpage_types = ['lesson.LessonPage']
python django wagtail
1个回答
0
投票

您将需要请求对象。这是一种方法:在get_context中检查每个模块的视图限制,并将模块和授权列表添加到上下文中。

class CoursePage(Page):
    def get_context(self, request, *args, **kwargs):
        context = super().get_context(request, *args, **kwargs)
        modules_list = []
        for module in self.get_children():
            restrictions = module.get_view_restrictions()
            auth = all(restriction.accept_request(request)
                       for restriction in restrictions)
            modules_list.append((module, auth))
        context['modules_list'] = modules_list

        return context

然后您可以在模板中使用auth标志来确定是否应显示锁定图标。

© www.soinside.com 2019 - 2024. All rights reserved.