Files
mayan-edms/mayan/apps/acls/classes.py
Roberto Rosario 0e86f2ad8a Refactor the model accesors
Refactor the accesors to behave like methods instead of properties.
This means all accesors will be prepended with the string
"get_" and will include a set of parenthesis.

Improve the ModeAttribute class to use the method's
short_description. This commit also adds support for a
new method .help_text attribute has been added.

Move accessors to their own module, named "methods.py".

Remove the PropertyHelper class as the accessors no longer
need it.

Signed-off-by: Roberto Rosario <Roberto.Rosario@mayan-edms.com>
2018-12-15 04:49:40 -04:00

87 lines
2.3 KiB
Python

from __future__ import absolute_import, unicode_literals
import logging
from django.apps import apps
logger = logging.getLogger(__name__)
class ModelPermission(object):
_inheritances = {}
_proxies = {}
_registry = {}
@classmethod
def get_classes(cls, as_content_type=False):
ContentType = apps.get_model(
app_label='contenttypes', model_name='ContentType'
)
if as_content_type:
content_type_dictionary = ContentType.objects.get_for_models(
*cls._registry.keys()
)
content_type_ids = [
content_type.pk for content_type in content_type_dictionary.values()
]
return ContentType.objects.filter(pk__in=content_type_ids)
else:
return cls._registry.keys()
@classmethod
def get_for_class(cls, klass):
return cls._registry.get(klass, ())
@classmethod
def get_for_instance(cls, instance):
StoredPermission = apps.get_model(
app_label='permissions', model_name='StoredPermission'
)
permissions = []
class_permissions = cls.get_for_class(klass=type(instance))
if class_permissions:
permissions.extend(class_permissions)
proxy = cls._proxies.get(type(instance))
if proxy:
permissions.extend(cls._registry.get(proxy))
pks = [
permission.stored_permission.pk for permission in set(permissions)
]
return StoredPermission.objects.filter(pk__in=pks)
@classmethod
def get_inheritance(cls, model):
return cls._inheritances[model]
@classmethod
def register(cls, model, permissions):
from django.contrib.contenttypes.fields import GenericRelation
cls._registry.setdefault(model, [])
for permission in permissions:
cls._registry[model].append(permission)
AccessControlList = apps.get_model(
app_label='acls', model_name='AccessControlList'
)
model.add_to_class(
name='acls', value=GenericRelation(to=AccessControlList)
)
@classmethod
def register_inheritance(cls, model, related):
cls._inheritances[model] = related
@classmethod
def register_proxy(cls, source, model):
cls._proxies[model] = source