Traverse master class hierarchy to collect all defined labels

i.e. for forms and grids
This commit is contained in:
Lance Edgar 2018-07-15 16:22:30 -05:00
parent 0853fac66a
commit 259d123876

View file

@ -193,13 +193,50 @@ class MasterView(View):
return getattr(cls, 'mobile_row_grid_factory', grids.MobileGrid)
def set_labels(self, obj):
for key, label in self.labels.items():
labels = self.collect_labels()
for key, label in six.iteritems(labels):
obj.set_label(key, label)
def collect_labels(self):
"""
Collect all labels defined within the master class hierarchy.
"""
labels = {}
hierarchy = self.get_class_hierarchy()
for cls in hierarchy:
if hasattr(cls, 'labels'):
labels.update(cls.labels)
return labels
def get_class_hierarchy(self):
hierarchy = []
def traverse(cls):
if cls is not object:
hierarchy.append(cls)
for parent in cls.__bases__:
traverse(parent)
traverse(self.__class__)
hierarchy.reverse()
return hierarchy
def set_row_labels(self, obj):
for key, label in self.row_labels.items():
labels = self.collect_row_labels()
for key, label in six.iteritems(labels):
obj.set_label(key, label)
def collect_row_labels(self):
"""
Collect all row labels defined within the master class hierarchy.
"""
labels = {}
hierarchy = self.get_class_hierarchy()
for cls in hierarchy:
if hasattr(cls, 'row_labels'):
labels.update(cls.row_labels)
return labels
##############################
# Available Views
##############################