decorators.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:habilitacion 作者: GabrielBD 项目源码 文件源码
def group_required(group_name, login_url=None, raise_exception=False):
    """
    Decorator for views that checks whether a user belongs to a particular
    group, redirecting to the log-in page if necessary.
    If the raise_exception parameter is given the PermissionDenied exception
    is raised.
    """
    def check_group(user):
        # First check if the user belongs to the group
        if user.groups.filter(name=group_name).exists():
            return True
        # In case the 403 handler should be called raise the exception
        if raise_exception:
            raise PermissionDenied
        # As the last resort, show the login form
        return False
    return user_passes_test(check_group, login_url=login_url)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号