Skip to content
Snippets Groups Projects
custom_annotations.py 640 B
from django.contrib.auth.decorators import user_passes_test


"""I collect custom decorators here that I use for the simple premission system

Currently the following options are available
    - group_required
    - in_groups
"""


def in_groups(user, group_list):
    return bool(user.groups.filter(name__in=group_list)) or user.is_superuser


def group_required(*group_names):
    """Requires user membership in at least one of the groups passed in. Should
    only be used in annotations
    """
    def _in_groups(u):
        if u.is_authenticated():
            return in_groups(u, group_names)
    return user_passes_test(_in_groups)