# Path: tutelary/models.py
# def check_perms(user, actions, objs, method=None):
#     ensure_permission_set_tree_cached(user)
#     if actions is False:
#         return False
#     if actions is not None:
#         for a in actions:
#             for o in objs:
#                 test_obj = None
#                 if o is not None:
#                     test_obj = o.get_permissions_object(a)
#                 if not user.has_perm(a, test_obj):
#                     return False
#     return True
# 
# Path: tutelary/decorators.py
# def action_error_message(actions, req_actions, default=None):
#     for req in req_actions:
#         for a in actions:
#             if isinstance(a, tuple) and a[0] == req:
#                 if 'error_message' in a[1]:
#                     return (a[1]['error_message'],)
#     if default is not None:
#         return (default,)
#     else:
#         return ()


from django.core.exceptions import ImproperlyConfigured, PermissionDenied
from collections.abc import Sequence
from django.contrib.auth.views import redirect_to_login
from django.contrib.auth.mixins import LoginRequiredMixin
from django.http.response import Http404
from .models import check_perms
from .decorators import action_error_message
class APIPermissionRequiredMixin(BasePermissionRequiredMixin):
    """Permission checking mixin for Django Rest Framework -- works just
    like the ``PermissionRequiredMixin`` in the default Django
    authentication system.

    """
    def check_permissions(self, request):
        """Permission checking for DRF."""
        objs = [None]
        if hasattr(self, 'get_perms_objects'):
            objs = self.get_perms_objects()
        else:
            if hasattr(self, 'get_object'):
                try:
                    objs = [self.get_object()]
                except Http404:
                    raise
                except:
                    pass
            if objs == [None]:
                objs = self.get_queryset()
            if len(objs) == 0:
                objs = [None]

        if (hasattr(self, 'permission_filter_queryset') and
           self.permission_filter_queryset is not False and
           self.request.method == 'GET'):
            if objs != [None]:
                self.perms_filter_queryset(objs)
        else:

# Path: tutelary/models.py
# def check_perms(user, actions, objs, method=None):
#     ensure_permission_set_tree_cached(user)
#     if actions is False:
#         return False
#     if actions is not None:
#         for a in actions:
#             for o in objs:
#                 test_obj = None
#                 if o is not None:
#                     test_obj = o.get_permissions_object(a)
#                 if not user.has_perm(a, test_obj):
#                     return False
#     return True
# 
# Path: tutelary/decorators.py
# def action_error_message(actions, req_actions, default=None):
#     for req in req_actions:
#         for a in actions:
#             if isinstance(a, tuple) and a[0] == req:
#                 if 'error_message' in a[1]:
#                     return (a[1]['error_message'],)
#     if default is not None:
#         return (default,)
#     else:
#         return ()


# Path: tutelary/mixins.py
from django.core.exceptions import ImproperlyConfigured, PermissionDenied
from collections.abc import Sequence
from django.contrib.auth.views import redirect_to_login
from django.contrib.auth.mixins import LoginRequiredMixin
from django.http.response import Http404
from .models import check_perms
from .decorators import action_error_message

class APIPermissionRequiredMixin(BasePermissionRequiredMixin):
    """Permission checking mixin for Django Rest Framework -- works just
    like the ``PermissionRequiredMixin`` in the default Django
    authentication system.

    """
    def check_permissions(self, request):
        """Permission checking for DRF."""
        objs = [None]
        if hasattr(self, 'get_perms_objects'):
            objs = self.get_perms_objects()
        else:
            if hasattr(self, 'get_object'):
                try:
                    objs = [self.get_object()]
                except Http404:
                    raise
                except:
                    pass
            if objs == [None]:
                objs = self.get_queryset()
            if len(objs) == 0:
                objs = [None]

        if (hasattr(self, 'permission_filter_queryset') and
           self.permission_filter_queryset is not False and
           self.request.method == 'GET'):
            if objs != [None]:
                self.perms_filter_queryset(objs)
        else:

            has_perm = check_perms(self.request.user,