|
@@ -1,10 +1,12 @@
|
|
|
__author__ = 'marcos.medeiros'
|
|
|
|
|
|
+
|
|
|
class Permission:
|
|
|
def __init__(self, model, instances):
|
|
|
self.model = model
|
|
|
self.instances = instances
|
|
|
|
|
|
+
|
|
|
def all_instances(model):
|
|
|
def i(request):
|
|
|
if model(request):
|
|
@@ -13,6 +15,7 @@ def all_instances(model):
|
|
|
return None
|
|
|
return i
|
|
|
|
|
|
+
|
|
|
def apply_instances_permission(model, perm):
|
|
|
if perm is None:
|
|
|
return []
|
|
@@ -24,6 +27,7 @@ def apply_instances_permission(model, perm):
|
|
|
return perm
|
|
|
return []
|
|
|
|
|
|
+
|
|
|
def has_instance(model, perm, instance):
|
|
|
p = apply_instances_permission(model, perm)
|
|
|
if hasattr(p, 'filter'):
|
|
@@ -32,6 +36,7 @@ def has_instance(model, perm, instance):
|
|
|
return bool([f for f in p if f.pk == instance.pk])
|
|
|
return False
|
|
|
|
|
|
+
|
|
|
def to_profile(profile):
|
|
|
if hasattr(profile, "__iter__"):
|
|
|
def m(request):
|
|
@@ -40,18 +45,19 @@ def to_profile(profile):
|
|
|
up = [p.pk for p in request.user.profile_set]
|
|
|
for p in up:
|
|
|
if p in profile:
|
|
|
- True
|
|
|
+ return True
|
|
|
return False
|
|
|
else:
|
|
|
def m(request):
|
|
|
if not request.user.is_authenticated():
|
|
|
return False
|
|
|
- up = [p.pk for p in request.user.profile_set]
|
|
|
+ up = [p.pk for p in request.user.profile_set.all()]
|
|
|
if profile in up:
|
|
|
return True
|
|
|
return False
|
|
|
return Permission(m, all_instances(m))
|
|
|
|
|
|
+
|
|
|
def to_staff():
|
|
|
def m(request):
|
|
|
if request.user.is_authenticated() and request.user.is_staff:
|
|
@@ -59,14 +65,27 @@ def to_staff():
|
|
|
return False
|
|
|
return Permission(m, all_instances(m))
|
|
|
|
|
|
+
|
|
|
def to_all():
|
|
|
def m(request):
|
|
|
return True
|
|
|
return Permission(m, all_instances(m))
|
|
|
|
|
|
+
|
|
|
def to_superusers():
|
|
|
def m(request):
|
|
|
if request.user.is_authenticated() and request.user.is_superuser:
|
|
|
return True
|
|
|
return False
|
|
|
return Permission(m, all_instances(m))
|
|
|
+
|
|
|
+
|
|
|
+def to_application_managers(app):
|
|
|
+ def m(request):
|
|
|
+ if not request.user.is_authenticated():
|
|
|
+ return False
|
|
|
+ up = [a.pk for a in request.user.managed_applications.all()]
|
|
|
+ if app in up:
|
|
|
+ return True
|
|
|
+ return False
|
|
|
+ return Permission(m, all_instances(m))
|