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>
This commit is contained in:
Roberto Rosario
2018-12-15 04:49:40 -04:00
parent 8c63ef4c69
commit 0e86f2ad8a
42 changed files with 434 additions and 282 deletions

View File

@@ -226,13 +226,27 @@ class ModelAttribute(object):
def __str__(self):
return self.get_display()
def get_display(self, show_name=False):
def get_label(self):
if self.label:
return self.label
else:
return getattr(
getattr(self.model, self.name), 'short_description', self.name
)
def get_description(self):
if self.description:
return self.description
else:
return getattr(getattr(self.model, self.name), 'help_text', None)
def get_display(self, show_name=False):
if self.get_description():
return '{} - {}'.format(
self.name if show_name else self.label, self.description
self.name if show_name else self.get_label(), self.get_description()
)
else:
return force_text(self.name if show_name else self.label)
return force_text(self.name if show_name else self.get_label())
class ModelField(ModelAttribute):
@@ -344,31 +358,6 @@ class Package(object):
self.__class__._registry.append(self)
class PropertyHelper(object):
"""
Makes adding fields using __class__.add_to_class easier.
Each subclass must implement the `constructor` and the `get_result`
method.
"""
@staticmethod
@property
def constructor(source_object):
return PropertyHelper(source_object)
def __init__(self, instance):
self.instance = instance
def __getattr__(self, name):
return self.get_result(name=name)
def get_result(self, name):
"""
The method that produces the actual result. Must be implemented
by each subclass.
"""
raise NotImplementedError
class Template(object):
_registry = {}