Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customizing the feincms page admin based on user

I'm trying to find a way to filter the admin queryset for page objects based up on the user provided, what I've considered (pseudo code):

from feincms... Page

class MyPageAdmin(PageAdmin):
    def __init__(self, *args, **kwargs):
        'monkey business'
        super(MyPageAdmin, self).__init__(*args, **kwargs)

admin.site.unregister(Page)
admin.site.register(Page, MyPageAdmin)

This won't work because feincms checks for a completely loaded django instance. A verbose solution would probably be not to load the page module at all, and either override the page model object or admin, e.g.:

from feincms... PageAdmin

class MyPage(Page):
    objects = CustomManager()

admin.site.register(MyPage, PageAdmin)

The documentation states it is possible to setup your own page module in a similar way, but it seems a lot of configuration for a simple requirement.

Is there any easier way to override the admin queryset or model admin for feincms modules?

like image 225
Hedde van der Heide Avatar asked Feb 09 '13 13:02

Hedde van der Heide


2 Answers

FeinCMS v1.7 also allows you to set FEINCMS_USE_PAGE_ADMIN=False in your Django settings.

Then, just subclass PageAdmin as you normally would, and register the model admin with the model yourself.

You should also start importing PageAdmin from feincms.module.page.modeladmins if you're using v1.7.

like image 176
Matthias Kestenholz Avatar answered Oct 02 '22 20:10

Matthias Kestenholz


The trick here is you cannot unregister the feincms modules because of some magic performed. Instead of registering your own feincms Page object, you can patch the methods like this:

from django.conf import settings
from feincms.module.page.models import PageAdmin

def queryset(self, request):
    qs = super(PageAdmin, self).queryset(request)
    if request.user.is_superuser:
        return qs
    return qs.filter(site__id__exact=settings.SITE_ID)
PageAdmin.queryset = queryset
like image 23
Hedde van der Heide Avatar answered Oct 02 '22 20:10

Hedde van der Heide