# Path: tutelary/decorators.py
# def permissioned_model(cls, perm_type=None, path_fields=None, actions=None):
#     """Function to set up a model for permissioning.  Can either be called
#     directly, passing a class and suitable values for ``perm_type``,
#     ``path_fields`` and ``actions``, or can be used as a class
#     decorator, taking values for ``perm_type``, ``path_fields`` and
#     ``actions`` from the ``TutelaryMeta`` subclass of the decorated
#     class.
# 
#     """
#     if not issubclass(cls, models.Model):
#         raise DecoratorException(
#             'permissioned_model',
#             "class '" + cls.__name__ + "' is not a Django model"
#         )
#     added = False
#     try:
#         if not hasattr(cls, 'TutelaryMeta'):
#             if perm_type is None or path_fields is None or actions is None:
#                 raise DecoratorException(
#                     'permissioned_model',
#                     ("missing argument: all of perm_type, path_fields and " +
#                      "actions must be supplied")
#                 )
#             added = True
#             cls.TutelaryMeta = type('TutelaryMeta', (object,),
#                                     dict(perm_type=perm_type,
#                                          path_fields=path_fields,
#                                          actions=actions))
#         cls.TutelaryMeta.pfs = ([cls.TutelaryMeta.perm_type] +
#                                 get_path_fields(cls))
#         perms_objs = {}
#         for a in cls.TutelaryMeta.actions:
#             an = a
#             ap = {}
#             if isinstance(a, tuple):
#                 an = a[0]
#                 ap = a[1]
#             Action.register(an)
#             if isinstance(ap, dict) and 'permissions_object' in ap:
#                 po = ap['permissions_object']
#                 if po is not None:
#                     try:
#                         t = cls._meta.get_field(po).__class__
#                         if t not in [models.ForeignKey,
#                                      models.OneToOneField]:
#                             raise PermissionObjectException(po)
#                     except:
#                         raise PermissionObjectException(po)
#                 perms_objs[an] = po
#         if len(perms_objs) == 0:
#             cls.get_permissions_object = get_perms_object
#         else:
#             cls.get_permissions_object = make_get_perms_object(perms_objs)
#         return cls
#     except:
#         if added:
#             del cls.TutelaryMeta
#         raise


from tutelary.decorators import permissioned_model
from django.db import models
    def __str__(self):
        return self.name


class CheckModel2(models.Model):
    name = models.CharField(max_length=100)
    container = models.ForeignKey(CheckModel1)

    def __str__(self):
        return self.name


permissioned_model(
    CheckModel2, perm_type='check2',
    path_fields=('container', 'pk'),
    actions=[('check2.list', {'permissions_object': 'container'}),
             ('check2.create', {'permissions_object': 'container'}),
             'check2.detail',
             'check2.delete']
)


class CheckModel3(models.Model):
    name = models.CharField(max_length=100)
    container = models.ForeignKey(CheckModel1)

    def __str__(self):
        return self.name



# Path: tutelary/decorators.py
# def permissioned_model(cls, perm_type=None, path_fields=None, actions=None):
#     """Function to set up a model for permissioning.  Can either be called
#     directly, passing a class and suitable values for ``perm_type``,
#     ``path_fields`` and ``actions``, or can be used as a class
#     decorator, taking values for ``perm_type``, ``path_fields`` and
#     ``actions`` from the ``TutelaryMeta`` subclass of the decorated
#     class.
# 
#     """
#     if not issubclass(cls, models.Model):
#         raise DecoratorException(
#             'permissioned_model',
#             "class '" + cls.__name__ + "' is not a Django model"
#         )
#     added = False
#     try:
#         if not hasattr(cls, 'TutelaryMeta'):
#             if perm_type is None or path_fields is None or actions is None:
#                 raise DecoratorException(
#                     'permissioned_model',
#                     ("missing argument: all of perm_type, path_fields and " +
#                      "actions must be supplied")
#                 )
#             added = True
#             cls.TutelaryMeta = type('TutelaryMeta', (object,),
#                                     dict(perm_type=perm_type,
#                                          path_fields=path_fields,
#                                          actions=actions))
#         cls.TutelaryMeta.pfs = ([cls.TutelaryMeta.perm_type] +
#                                 get_path_fields(cls))
#         perms_objs = {}
#         for a in cls.TutelaryMeta.actions:
#             an = a
#             ap = {}
#             if isinstance(a, tuple):
#                 an = a[0]
#                 ap = a[1]
#             Action.register(an)
#             if isinstance(ap, dict) and 'permissions_object' in ap:
#                 po = ap['permissions_object']
#                 if po is not None:
#                     try:
#                         t = cls._meta.get_field(po).__class__
#                         if t not in [models.ForeignKey,
#                                      models.OneToOneField]:
#                             raise PermissionObjectException(po)
#                     except:
#                         raise PermissionObjectException(po)
#                 perms_objs[an] = po
#         if len(perms_objs) == 0:
#             cls.get_permissions_object = get_perms_object
#         else:
#             cls.get_permissions_object = make_get_perms_object(perms_objs)
#         return cls
#     except:
#         if added:
#             del cls.TutelaryMeta
#         raise


# Path: tests/check_models.py
from tutelary.decorators import permissioned_model
from django.db import models

    def __str__(self):
        return self.name


class CheckModel2(models.Model):
    name = models.CharField(max_length=100)
    container = models.ForeignKey(CheckModel1)

    def __str__(self):
        return self.name


permissioned_model(
    CheckModel2, perm_type='check2',
    path_fields=('container', 'pk'),
    actions=[('check2.list', {'permissions_object': 'container'}),
             ('check2.create', {'permissions_object': 'container'}),
             'check2.detail',
             'check2.delete']
)


class CheckModel3(models.Model):
    name = models.CharField(max_length=100)
    container = models.ForeignKey(CheckModel1)

    def __str__(self):
        return self.name



@permissioned_model