text
stringlengths
0
828
def _wrapped_view(request, *args, **kwargs):
if checker(request.user):
return view_func(request, *args, **kwargs)
return _wrapped_view
return decorator"
1915,"def get_path_fields(cls, base=[]):
""""""Get object fields used for calculation of django-tutelary object
paths.
""""""
pfs = []
for pf in cls.TutelaryMeta.path_fields:
if pf == 'pk':
pfs.append(base + ['pk'])
else:
f = cls._meta.get_field(pf)
if isinstance(f, models.ForeignKey):
pfs += get_path_fields(f.target_field.model, base=base + [pf])
else:
pfs.append(base + [f.name])
return pfs"
1916,"def get_perms_object(obj, action):
""""""Get the django-tutelary path for an object, based on the fields
listed in ``TutelaryMeta.pfs``.
""""""
def get_one(pf):
if isinstance(pf, str):
return pf
else:
return str(reduce(lambda o, f: getattr(o, f), pf, obj))
return Object([get_one(pf) for pf in obj.__class__.TutelaryMeta.pfs])"
1917,"def make_get_perms_object(perms_objs):
""""""Make a function to delegate permission object rendering to some
other (foreign key) field of an object.
""""""
def retfn(obj, action):
if action in perms_objs:
if perms_objs[action] is None:
return None
else:
return get_perms_object(getattr(obj, perms_objs[action]),
action)
else:
return get_perms_object(obj, action)
return retfn"
1918,"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)