每组片段的权限

时间:2018-09-04 09:43:28

标签: wagtail wagtail-snippet

我有一个Wagtail网站,每个小组都可以在具有不同图像和文档权限的不同页面树上工作。 那是一个多站点设置,我试图将站点真正分开。

是否可以按组限制摘要权限? 我希望我的小组只看到其中一部分。

1 个答案:

答案 0 :(得分:1)

当我想使用网站设置时,我面临着类似的事情。

我发现的唯一解决方案是创建一个自定义模型并使用ModelAdmin。

一些“摘要”可助您一臂之力:

class SiteSettings(models.Model):

    base_form_class = SiteSettingsForm

    COMPANY_FORM_CHOICES = (
        ('BED', 'Bedrijf'),
        ('ORG', 'Organisatie'),
        ('STI', 'Stichting'),
        ('VER', 'Vereniging'),
    )

    site = models.OneToOneField(
        Site,
        unique = True,
        db_index = True,
        on_delete = models.CASCADE,
        verbose_name = _('site'),
        related_name = 'site_settings',
        help_text = _('The sites these setting belong to.')
    )
    company_name = models.CharField(
        _('company name'),
        blank = True,
        max_length = 50,
        help_text = _('De naam van het bedrijf of de organisatie.')
    )
    company_form = models.CharField(
        _('company form'),
        max_length = 3,
        blank = True,
        default = 'COM',
        choices = COMPANY_FORM_CHOICES
    )
    ...



class MyPermissionHelper(PermissionHelper):

    def user_can_edit_obj(self, user, obj):
        result = super().user_can_edit_obj(user, obj)
        if not user.is_superuser:
            user_site = get_user_site(user)
            result = user_site and user_site == obj.site
        return result



class SiteSettingsAdmin(ThumbnailMixin, ModelAdmin):

    model = SiteSettings
    menu_label = _('Site settings')
    menu_icon = 'folder-open-inverse'
    add_to_settings_menu = True

    list_display = ['admin_thumb', 'company_name', 'get_categories']
    list_select_related = True
    list_display_add_buttons = 'site'
    thumb_image_field_name = 'logo'
    thumb_col_header_text = _('logo')

    permission_helper_class = MyPermissionHelper

    create_view_class = CreateSiteSettingsView
    ...


class CreateSiteSettingsView(SiteSettingsViewMixin, CreateView):

    @cached_property
    def sites_without_settings(self):
        sites = get_sites_without_settings()
        if not sites:
            messages.info(
                self.request,
                _('No sites without settings found.')
            )
        return sites

    def dispatch(self, request, *args, **kwargs):
        if request.user.is_superuser and not self.sites_without_settings:
            return redirect(self.url_helper.get_action_url('index'))
        return super().dispatch(request, *args, **kwargs)

    def get_initial(self):
        initial = super().get_initial().copy()
        current_site = self.request.site
        initial.update({
            'company_name': current_site.site_name}
        )
        if self.request.user.is_superuser:
            initial.update({
                'site': current_site}
            )
        return initial

    def get_form(self):
        form = super().get_form()
        flds = form.fields
        if self.request.user.is_superuser:
            fld = form.fields['site']
            fld.queryset = self.sites_without_settings.order_by(
                Lower('site_name')
            )
        return form

    def form_valid(self, form):
        instance = form.save(commit=False)
        if not self.request.user.is_superuser:
            instance.site = self.request.site
        instance.save()
        messages.success(
            self.request, self.get_success_message(instance),
            buttons=self.get_success_message_buttons(instance)
        )
        return redirect(self.get_success_url())