Files
mayan-edms/mayan/apps/document_states/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

76 lines
2.0 KiB
Python

from __future__ import unicode_literals
import logging
from importlib import import_module
from django.apps import apps
from django.utils import six
from django.utils.encoding import force_text
__all__ = ('WorkflowAction',)
logger = logging.getLogger(__name__)
class WorkflowActionMetaclass(type):
_registry = {}
def __new__(mcs, name, bases, attrs):
new_class = super(WorkflowActionMetaclass, mcs).__new__(
mcs, name, bases, attrs
)
if not new_class.__module__ == __name__:
mcs._registry[
'{}.{}'.format(new_class.__module__, name)
] = new_class
return new_class
class WorkflowActionBase(object):
fields = ()
class WorkflowAction(six.with_metaclass(WorkflowActionMetaclass, WorkflowActionBase)):
@classmethod
def clean(cls, request, form_data=None):
return form_data
@classmethod
def get(cls, name):
return cls._registry[name]
@classmethod
def get_all(cls):
return sorted(cls._registry.values(), key=lambda x: x.label)
@classmethod
def id(cls):
return '{}.{}'.format(cls.__module__, cls.__name__)
@staticmethod
def initialize():
for app in apps.get_app_configs():
try:
import_module('{}.workflow_actions'.format(app.name))
except ImportError as exception:
if force_text(exception) not in ('No module named workflow_actions', 'No module named \'{}.workflow_actions\''.format(app.name)):
logger.error(
'Error importing %s workflow_actions.py file; %s',
app.name, exception
)
def __init__(self, form_data=None):
self.form_data = form_data
def get_form_schema(self, request=None):
result = {
'fields': self.fields or (),
'widgets': getattr(self, 'widgets', {})
}
if hasattr(self, 'field_order'):
result['field_order'] = self.field_order
return result