How to restrict a class based view to one user group?

146 views
Skip to first unread message

Daniel Grace

unread,
Nov 5, 2014, 2:14:35 PM11/5/14
to django...@googlegroups.com
Hi,
Say I have a function as follows:

def is_supervisor(user):
    return user.groups.filter(name='supervisor').exists()

...and a CBV for deleting records:

class DeleteFlow(DeleteView):
    # etc...

How can I restrict access to this view using the function?
I understand that this may be possible with a mixin, but how?
Or is there a better way?

Thanks

Bruno Barcarol Guimarães

unread,
Nov 6, 2014, 8:28:09 AM11/6/14
to django...@googlegroups.com
Hi Daniel.

You're looking for the `user_passes_test` decorator:

https://docs.djangoproject.com/en/1.7/topics/auth/default/#limiting-access-to-logged-in-users-that-pass-a-test

Remember you need to decorate the `dispatch` method of a CBV.

Daniel Grace

unread,
Nov 6, 2014, 8:31:55 AM11/6/14
to django...@googlegroups.com
I solved this using a "method_decorator" and "user_passes_test".

Daniel Grace

unread,
Nov 6, 2014, 9:36:52 AM11/6/14
to django...@googlegroups.com
Thanks Bruno, I was decorating the 'post' method.  I've got it right now.
Reply all
Reply to author
Forward
0 new messages