【问题标题】:Django/Wagtail: How to check if the user has permissions to access a given Page?Django/Wagtail:如何检查用户是否有权访问给定页面?
【发布时间】:2020-01-12 20:07:05
【问题描述】:

我是 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']

【问题讨论】:

  • 看到你的models.py会很有帮助
  • 嗨 Dan,我目前有一个非常简单的模型:` ``` from django.db import models from wagtail.core.models import Page # 在此处创建模型。 class CoursePage(Page): """ A Page... """ description = models.TextField( help_text='描述课程的文本', blank=True) subpage_types = ['course_module.ModulePage'] ```

标签: python django wagtail


【解决方案1】:

您将需要请求对象。这是一种方法:检查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 标志来确定是否应显示锁定图标。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-19
    • 2020-03-20
    • 2012-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多