Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

permission property hides the button permanently #8

Open
gleniat opened this issue Sep 7, 2022 · 1 comment
Open

permission property hides the button permanently #8

gleniat opened this issue Sep 7, 2022 · 1 comment
Labels
documentation Improvements or additions to documentation

Comments

@gleniat
Copy link

gleniat commented Sep 7, 2022

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?)

@MatthijsvW
Copy link

MatthijsvW commented Sep 12, 2022

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

@saxix saxix added the documentation Improvements or additions to documentation label Oct 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

3 participants