将Wagtail自定义站点设置限制为组或站点

时间:2018-12-24 16:51:38

标签: django wagtail

我正在尝试在每个站点的站点上创建自定义设置,以获取页眉/页脚信息。我已经注册了设置,并且可以编辑每个站点的设置,但是其他站点可以查看/编辑这些设置:

enter image description here

我的代码是:

@register_setting
class SiteSettings(BaseSetting, ClusterableModel):
    site_name = models.CharField(max_length=50)
    site_logo = models.ForeignKey(
        'wagtailimages.Image',
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name='+',
    )
    banner_color = models.CharField(
        max_length=6,
        null=True,
        blank=True,
        help_text="Fill in a hex colour value"
    )
    include_footer = models.BooleanField(null=True)

    panels = [
        FieldPanel('site_name'),
        ImageChooserPanel('site_logo'),
        FieldPanel('banner_color'),
        FieldPanel('include_footer'),
        InlinePanel('footer', label="Footer",
                help_text='Select your contact/social media type and enter the phone number, email, or URL')
    ]

我尝试覆盖SiteSwitchForm,但似乎没有被调用。

# Override SiteSwitchForm

class SiteSwitchForm(SingleSiteSwitchForm):
    site = forms.ChoiceField(choices=[])

    class Media:
        js = [
            'wagtailmenus/js/site-switcher.js',
        ]

    def __init__(self, current_site, url_helper, **kwargs):
        initial = {'site': url_helper.get_action_url('edit', current_site.pk)}
        super().__init__(initial=initial, **kwargs)
        sites = []
        for site in Site.objects.filter(site_name__exact=self.site_name):
            sites.append((url_helper.get_action_url('edit', site.pk), site))
        self.fields['site'].choices = sites

我也尝试过:

def get_queryset(self, request):
    qs = super().get_queryset(request)
    return qs.filter(managed_by=request.user)

但这不适用于BaseSettings。

是否有一种方法可以使自定义设置仅对拥有该站点的组/站点可见?

1 个答案:

答案 0 :(得分:1)

将SiteSettings注册为ModelAdmin,并覆盖ModelAdmin.get_queryset接收请求。这样您就可以查找当前站点。

现在,您只需要在设置菜单中禁用SiteSettings的方法即可。