Commit c65ffa22 authored by Vladimir Popescu's avatar Vladimir Popescu
Browse files

[views] Enable hiding relations with undefined objects / subjects

Also enable hiding attributes whose value is the empty string.

To the latter end, the ``hide_empty_string_value`` class attribute
should be set in the application.

Closes #4142557.
parent de414cb01e6b
......@@ -35,6 +35,20 @@ from cubicweb import NoSelectableObject
class BrainomicsPrimaryView(PrimaryView):
""" Redefine primary view for html customization """
hide_undefined_value = False
hide_empty_string_value = False
def filter_attributes(self, entity, rschema):
val_attr = getattr(entity, rschema.type, None)
if self.hide_undefined_value and val_attr is None:
return True
if self.hide_empty_string_value and val_attr == u'':
return True
return False
def filter_relations(self, entity, rschema, role):
if self.hide_undefined_value and not entity.related(rschema.type, role=role):
return True
return False
def render_entity_attributes(self, entity):
"""Renders all attributes and relations in the 'attributes' section.
......@@ -43,8 +57,9 @@ class BrainomicsPrimaryView(PrimaryView):
for rschema, _, role, dispctrl in self._section_def(entity, 'attributes'):
vid = dispctrl.get('vid', 'reledit')
if rschema.final or vid == 'reledit' or dispctrl.get('rtypevid'):
if (rschema.final and self.hide_undefined_value
and getattr(entity, rschema.type, None) is None):
if ((rschema.final and self.filter_attributes(entity, rschema)) or
(not rschema.final and self.filter_relations(
entity, rschema, role=role))):
continue
value = entity.view(vid, rtype=rschema.type, role=role,
initargs={'dispctrl': dispctrl})
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment