Navigation: Related field support to SourceColumn
Add support to the SourceColumn class to resolve related fields using the double underscore as separator. Columns that use related no longer have to use throw away lambdas. Signed-off-by: Roberto Rosario <Roberto.Rosario@mayan-edms.com>
This commit is contained in:
@@ -11,7 +11,7 @@ from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from .classes import Package
|
||||
from .models import UserLocaleProfile
|
||||
from .utils import return_attrib
|
||||
from .utils import resolve_attribute
|
||||
from .widgets import DisableableSelectWidget, PlainWidget, TextAreaDiv
|
||||
|
||||
|
||||
@@ -43,7 +43,7 @@ class DetailForm(forms.ModelForm):
|
||||
super(DetailForm, self).__init__(*args, **kwargs)
|
||||
|
||||
for extra_field in self.extra_fields:
|
||||
result = return_attrib(self.instance, extra_field['field'])
|
||||
result = resolve_attribute(self.instance, extra_field['field'])
|
||||
label = 'label' in extra_field and extra_field['label'] or None
|
||||
# TODO: Add others result types <=> Field types
|
||||
if isinstance(result, models.query.QuerySet):
|
||||
@@ -53,7 +53,7 @@ class DetailForm(forms.ModelForm):
|
||||
else:
|
||||
self.fields[extra_field['field']] = forms.CharField(
|
||||
label=extra_field['label'],
|
||||
initial=return_attrib(
|
||||
initial=resolve_attribute(
|
||||
self.instance,
|
||||
extra_field['field'], None
|
||||
),
|
||||
|
||||
@@ -10,7 +10,7 @@ import mayan
|
||||
|
||||
from ..classes import Collection, Dashboard
|
||||
from ..literals import MESSAGE_SQLITE_WARNING
|
||||
from ..utils import check_for_sqlite, return_attrib
|
||||
from ..utils import check_for_sqlite, resolve_attribute
|
||||
|
||||
register = Library()
|
||||
|
||||
@@ -30,7 +30,7 @@ def get_collections():
|
||||
def get_encoded_parameter(item, parameters_dict):
|
||||
result = {}
|
||||
for attrib_name, attrib in parameters_dict.items():
|
||||
result[attrib_name] = return_attrib(item, attrib)
|
||||
result[attrib_name] = resolve_attribute(item, attrib)
|
||||
return dumps(result)
|
||||
|
||||
|
||||
@@ -41,7 +41,7 @@ def get_type(value):
|
||||
|
||||
@register.filter
|
||||
def object_property(value, arg):
|
||||
return return_attrib(value, arg)
|
||||
return resolve_attribute(value, arg)
|
||||
|
||||
|
||||
@register.simple_tag
|
||||
|
||||
@@ -7,6 +7,7 @@ import tempfile
|
||||
import types
|
||||
|
||||
from django.conf import settings
|
||||
from django.db.models.constants import LOOKUP_SEP
|
||||
from django.urls import resolve as django_resolve
|
||||
from django.urls.base import get_script_prefix
|
||||
from django.utils.datastructures import MultiValueDict
|
||||
@@ -129,27 +130,34 @@ def resolve(path, urlconf=None):
|
||||
return django_resolve(path=path, urlconf=urlconf)
|
||||
|
||||
|
||||
def return_attrib(obj, attrib, arguments=None):
|
||||
def resolve_attribute(obj, attrib, arguments=None):
|
||||
if isinstance(attrib, types.FunctionType):
|
||||
return attrib(obj)
|
||||
elif isinstance(
|
||||
obj, dict_type
|
||||
) or isinstance(obj, dictionary_type):
|
||||
elif isinstance(obj, dict_type) or isinstance(obj, dictionary_type):
|
||||
return obj[attrib]
|
||||
else:
|
||||
result = reduce_function(getattr, attrib.split('.'), obj)
|
||||
if isinstance(result, types.MethodType):
|
||||
if arguments:
|
||||
return result(**arguments)
|
||||
try:
|
||||
result = reduce_function(getattr, attrib.split('.'), obj)
|
||||
if isinstance(result, types.MethodType):
|
||||
if arguments:
|
||||
return result(**arguments)
|
||||
else:
|
||||
return result()
|
||||
else:
|
||||
return result()
|
||||
else:
|
||||
return result
|
||||
return result
|
||||
except AttributeError:
|
||||
if LOOKUP_SEP in attrib:
|
||||
attrib = attrib.replace(LOOKUP_SEP, '.')
|
||||
return resolve_attribute(
|
||||
obj=obj, attrib=attrib, arguments=arguments
|
||||
)
|
||||
else:
|
||||
raise
|
||||
|
||||
|
||||
def return_related(instance, related_field):
|
||||
"""
|
||||
This functions works in a similar method to return_attrib but is
|
||||
This functions works in a similar method to resolve_attribute but is
|
||||
meant for related models. Support multiple levels of relationship
|
||||
using double underscore.
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user