Merge pull request #93 from Skeptiker88/master

added permission check for all child types
fix_request_path_info
Diederik van der Boor 2014-07-29 12:13:45 +02:00
commit bbb1cd068a
1 changed files with 13 additions and 4 deletions

View File

@ -65,7 +65,7 @@ class PolymorphicChildModelFilter(admin.SimpleListFilter):
parameter_name = 'polymorphic_ctype' parameter_name = 'polymorphic_ctype'
def lookups(self, request, model_admin): def lookups(self, request, model_admin):
return model_admin.get_child_type_choices() return model_admin.get_child_type_choices(request, 'change')
def queryset(self, request, queryset): def queryset(self, request, queryset):
try: try:
@ -176,12 +176,21 @@ class PolymorphicParentModelAdmin(admin.ModelAdmin):
return self.child_models return self.child_models
def get_child_type_choices(self): def get_child_type_choices(self, request, action):
""" """
Return a list of polymorphic types which can be added. Return a list of polymorphic types for which the user has the permission to perform the given action.
""" """
try:
from django.contrib.auth import get_permission_codename
except ImportError:
# Django < 1.6
from django.contrib.auth.management import _get_permission_codename as get_permission_codename
choices = [] choices = []
for model, _ in self.get_child_models(): for model, _ in self.get_child_models():
if not request.user.has_perm('%s.%s' % (model._meta.app_label,
get_permission_codename(action, model._meta))):
continue
ct = ContentType.objects.get_for_model(model, for_concrete_model=False) ct = ContentType.objects.get_for_model(model, for_concrete_model=False)
choices.append((ct.id, model._meta.verbose_name)) choices.append((ct.id, model._meta.verbose_name))
return choices return choices
@ -325,7 +334,7 @@ class PolymorphicParentModelAdmin(admin.ModelAdmin):
if request.META['QUERY_STRING']: if request.META['QUERY_STRING']:
extra_qs = '&' + request.META['QUERY_STRING'] extra_qs = '&' + request.META['QUERY_STRING']
choices = self.get_child_type_choices() choices = self.get_child_type_choices(request, 'add')
if len(choices) == 1: if len(choices) == 1:
return HttpResponseRedirect('?ct_id={0}{1}'.format(choices[0][0], extra_qs)) return HttpResponseRedirect('?ct_id={0}{1}'.format(choices[0][0], extra_qs))