# Path: rest/model_tools.py
# def get_ct(model, for_concrete_model=False):
#     from .models import ContentType
#     if isinstance(model, str):
#         ctype = ContentType.objects.get_by_identifier(model)
#     else:
#         ctype = ContentType.objects.get_for_model(
#             model, for_concrete_model=for_concrete_model
#         )
#         # get_for_model sometimes returns a DjangoContentType - caching issue?
#         if not isinstance(ctype, ContentType):
#             ctype = ContentType.objects.get(pk=ctype.pk)
#             ContentType.objects._add_to_cache(ContentType.objects.db, ctype)
#     return ctype


from rest_framework.permissions import BasePermission
from .model_tools import get_ct
        from django.conf import settings


class ModelPermissions(BasePermission):
    METHOD_PERM = {
        'GET': 'view',
        'HEAD': 'view',
        'OPTIONS': 'view',
        'POST': 'add',
        'PUT': 'change',
        'PATCH': 'change',
        'DELETE': 'delete',
    }

    def has_permission(self, request, view):
        if getattr(view, 'model', None) is None:
            return True
        user = request.user
        ct = get_ct(view.model)
        result = has_perm(user, ct, self.METHOD_PERM[request.method])
        return result


def has_perm(user, ct, perm):
    if perm == 'view':
        return True
    if isinstance(ct, str):
        perm = '%s_%s' % (ct, perm)

# Path: rest/model_tools.py
# def get_ct(model, for_concrete_model=False):
#     from .models import ContentType
#     if isinstance(model, str):
#         ctype = ContentType.objects.get_by_identifier(model)
#     else:
#         ctype = ContentType.objects.get_for_model(
#             model, for_concrete_model=for_concrete_model
#         )
#         # get_for_model sometimes returns a DjangoContentType - caching issue?
#         if not isinstance(ctype, ContentType):
#             ctype = ContentType.objects.get(pk=ctype.pk)
#             ContentType.objects._add_to_cache(ContentType.objects.db, ctype)
#     return ctype


# Path: rest/permissions.py
from rest_framework.permissions import BasePermission
from .model_tools import get_ct
        from django.conf import settings



class ModelPermissions(BasePermission):
    METHOD_PERM = {
        'GET': 'view',
        'HEAD': 'view',
        'OPTIONS': 'view',
        'POST': 'add',
        'PUT': 'change',
        'PATCH': 'change',
        'DELETE': 'delete',
    }

    def has_permission(self, request, view):
        if getattr(view, 'model', None) is None:
            return True
        user = request.user
        ct = get_ct(view.model)
        result = has_perm(user, ct, self.METHOD_PERM[request.method])
        return result


def has_perm(user, ct, perm):
    if perm == 'view':
        return True
    if isinstance(ct, str):
        perm = '%s_%s' % (ct, perm)

    else: