Marc LaBelle
Marc LaBelle

Reputation: 320

Limit Wagtail Custom Site Settings to Group or Site

I am trying to create custom settings on a site by site basis for head/footer information. I have registered the settings and I'm able to edit the settings for each site, but other sites can see/edit these settings:

enter image description here

The code I have is:

@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')
    ]

I tried overriding SiteSwitchForm, but it doesn't seem to get called.

# 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

I also tried:

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

But that doesn't apply to BaseSettings.

Is there a way to make custom settings only visible to the group/site that owns the site?

Upvotes: 2

Views: 437

Answers (1)

allcaps
allcaps

Reputation: 11228

Register the SiteSettings as ModelAdmin and override ModelAdmin.get_queryset receives the request. So you can lookup the current site.

Now you only need a way to disable SiteSettings in settings menu.

Upvotes: 2

Related Questions