Last active
March 8, 2019 02:31
-
-
Save bradmontgomery/5657267 to your computer and use it in GitHub Desktop.
Updated code for an old blog post: https://bradmontgomery.net/blog/restricting-access-by-group-in-django/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def not_in_student_group(user): | |
"""Use with a ``user_passes_test`` decorator to restrict access to | |
authenticated users who are not in the "Student" group.""" | |
return user.is_authenticated() and not user.groups.filter(name='Student').exists() | |
# Use the above with: | |
@user_passes_test(not_in_student_group, login_url='/elsewhere/') | |
def some_view(request): | |
# ... | |
# Another, less verbose, option: | |
def not_student(function=None): | |
actual_decorator = user_passes_test( | |
lambda u: u.is_authenticated() and not user.groups.filter(name='Student').exists() | |
) | |
return actual_decorator(function) | |
# Use the above with: | |
@not_student | |
def some_view(request): | |
# ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
is probably more pythonic©