Django的监护人。如何过滤权限列表?

时间:2013-12-11 14:24:21

标签: django django-admin django-guardian

我正在使用django-guardian来管理我的权限。默认每个模型有3个perms:addchangedelete。如果我添加自己的许可,例如view,我将在“用户管理”选项卡中显示全部4个,在模型对象管理选项卡中包含所有4个。

我知道可以过滤权限:Django admin - change permissions list

但我不知道如何在模型对象管理选项卡中过滤权限。它甚至可能吗?

1 个答案:

答案 0 :(得分:2)

我做了一些工作。也许这不是最佳解决方案,但有效:

admin_utils.py

from __future__ import unicode_literals
from django import forms
from django.conf import settings
from guardian.compat import url, patterns
from django.contrib import admin
from django.contrib import messages
from django.contrib.admin.widgets import FilteredSelectMultiple
from django.core.urlresolvers import reverse
from django.shortcuts import render_to_response, get_object_or_404, redirect
from django.template import RequestContext
from django.utils.datastructures import SortedDict
from django.utils.translation import ugettext, ugettext_lazy as _
from django.db.models import Q
from django.contrib.auth.models import Group, Permission

from guardian.compat import get_user_model
from guardian.forms import UserObjectPermissionsForm
from guardian.forms import GroupObjectPermissionsForm
from guardian.shortcuts import get_perms
from guardian.shortcuts import get_users_with_perms
from guardian.shortcuts import get_groups_with_perms
from guardian.models import Group
from django.contrib.contenttypes.models import ContentType
from guardian.admin import AdminUserObjectPermissionsForm, AdminGroupObjectPermissionsForm
from guardian.admin import GuardedModelAdmin


class MyGuardedModelAdmin(GuardedModelAdmin):

    allowed_perms = []

    def get_perms_for_model(self, cls):
        if isinstance(cls, basestring):
            app_label, model_name = cls.split('.')
            model = models.get_model(app_label, model_name)
        else:
            model = cls
        ctype = ContentType.objects.get_for_model(model)
        return Permission.objects.filter(Q(content_type=ctype), Q(codename__in=self.allowed_perms) )

    def get_obj_perms_base_context(self, request, obj):
        context = {
            'adminform': {'model_admin': self},
            'media': self.media,
            'object': obj,
            'app_label': self.model._meta.app_label,
            'opts': self.model._meta,
            'original': hasattr(obj, '__unicode__') and obj.__unicode__() or\
                str(obj),
            'has_change_permission': self.has_change_permission(request, obj),
            'model_perms': self.get_perms_for_model(obj),
            'title': _("Object permissions"),
        }
        return context


    class MyAdminUserObjectPermissionsForm(AdminUserObjectPermissionsForm):

        def get_perms_for_model(self, cls):
            if isinstance(cls, basestring):
                app_label, model_name = cls.split('.')
                model = models.get_model(app_label, model_name)
            else:
                model = cls
            ctype = ContentType.objects.get_for_model(model)
            return Permission.objects.filter(Q(content_type=ctype), Q(codename__in=self.allowed_perms) )

        def get_obj_perms_field_choices(self):
            choices = [(p.codename, p.name) for p in self.get_perms_for_model(self.obj)]
            return choices

    def get_obj_perms_manage_user_form(self):
        setattr(self.MyAdminUserObjectPermissionsForm, 'allowed_perms', self.allowed_perms)
        return self.MyAdminUserObjectPermissionsForm


    class MyAdminGroupObjectPermissionsForm(GroupObjectPermissionsForm):
        def get_perms_for_model(self, cls):
            if isinstance(cls, basestring):
                app_label, model_name = cls.split('.')
                model = models.get_model(app_label, model_name)
            else:
                model = cls
            ctype = ContentType.objects.get_for_model(model)
            return Permission.objects.filter(Q(content_type=ctype), Q(codename__in=self.allowed_perms) )

        def get_obj_perms_field_choices(self):
            choices = [(p.codename, p.name) for p in self.get_perms_for_model(self.obj)]
            return choices

    def get_obj_perms_manage_group_form(self):
        setattr(self.MyAdminGroupObjectPermissionsForm, 'allowed_perms', self.allowed_perms)
        return self.MyAdminGroupObjectPermissionsForm

现在您可以使用allowed_perms

进行过滤

admin.py

class PollAdmin(MyGuardedModelAdmin):
    allowed_perms = ['view_poll']

admin.site.register(Poll, PollAdmin)