jazzband/django-permission

Stay at model level

LucasBerbesson opened this issue · 1 comments

Hello,
I love this package, it's awesome thank you.
Is there a way to use the permission_required decorator on a ListView (or any CBV with a get_queryset method) on a Model level and thus checking the has_perm only once.

Right now I'm using the django.contrib.auth.mixins.PermissionRequiredMixin, but I love the permission_required decorator.

Basically I would love to do something like

from permission.decorators import permission_required

@permission_required('blog.view_post',stay_at_model_level=True)
class PostList(ListView):
    model = Post

Instead of doing :

from django.contrib.auth.mixins import PermissionRequiredMixin

class PostList(PermissionRequiredMixin, ListView):
    model = Post
    permission_required = 'blog.view_post'

No. But it is not so difficult to implement. For example

https://github.com/lambdalisue/django-permission/blob/master/src/permission/decorators/classbase.py#L45

If obj here is None then it would check the model level permission.

Currently I don't have enough time to implement that feature to the existing decorators so if you are in hurry, send me a PR for all decorators in https://github.com/lambdalisue/django-permission/tree/master/src/permission/decorators 👍