saxix/django-admin-extra-buttons

permission property hides the button permanently

Opened this issue · 1 comments

I've tried permission property and it seems it hides the button no matter what value is provided from the permission function.

@register(MyModel)
class MyModelAdmin(ExtraButtonsMixin, admin.ModelAdmin):

    @button()
    def delete_all(self, request):
        pass

a simple button

@register(MyModel)
class MyModelAdmin(ExtraButtonsMixin, admin.ModelAdmin):

    @button(permission=lambda request, obj: False)
    def delete_all(self, request):
        pass

the button is hidden.

@register(MyModel)
class MyModelAdmin(ExtraButtonsMixin, admin.ModelAdmin):

    @button(permission=lambda request, obj: True)
    def delete_all(self, request):
        pass

the button is still hidden (why?)

I figured out why.
The problem is that the callable expects an extra argument namely handler.

The check_permission function in admin_extra_buttons.utils is called and your permission callable is passed to this function. However the check_permissionfunction calls the callable not only with the request and object as arguments, but also with handler as an argument. This raises an error, but this is somehow caught somewhere and not raised so we don't know it is actually failing.

def check_permission(handler, permission, request, obj=None):
    if callable(permission):
        if not permission(request, obj, handler=handler):
            raise PermissionDenied
    elif not request.user.has_perm(permission):
        raise PermissionDenied
    return True

If you add the handler to the kwargs of your callable it will work as intended
@button(permission=lambda request, obj, handler: True)

I'm not sure why the check_permission function passes the handler to the callable. It should either be removed or the documentation should be updated accordingly