[viewlib] Extract a function to display entities under an alternative
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 06 Jan 2017 14:11:23 +0100
changeset 2225 f905983dfc5e
parent 2224 c0d56eed71cd
child 2226 58b4ad23290e
[viewlib] Extract a function to display entities under an alternative
views/viewlib.py
--- a/views/viewlib.py	Thu Jan 05 16:19:28 2017 +0100
+++ b/views/viewlib.py	Fri Jan 06 14:11:23 2017 +0100
@@ -1,4 +1,4 @@
-# copyright 2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+# copyright 2016-2017 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
 # contact http://www.logilab.fr -- mailto:contact@logilab.fr
 #
 # This program is free software: you can redistribute it and/or modify it under
@@ -77,21 +77,32 @@
         self.w(u'</div>')
 
 
+def alternative_values(entity, parent_rtype):
+    """Display entities under the given alternative `entity`.
+
+    Those are found by introspecting the schema, and skipping the relation from the alternative to
+    its parent (`parent_rtype`).
+    """
+    req = entity._cw
+    alternatives = []
+    for rschema, _targets, role in entity.e_schema.relation_definitions():
+        rtype = rschema.type
+        if rtype.startswith('seda_') and rtype != parent_rtype:
+            target_rset = entity.related(rtype, role)
+            if target_rset:
+                alternatives.append(req.view('seda.type_meta', rset=target_rset))
+    return (' <b>%s</b> ' % req._(' ALT_I18N ')).join(alternatives)
+
+
 class AlternativeEntityAttributeView(EntityView):
     """Attribute view for SEDA alternative entities"""
     __regid__ = 'seda.reledit.alternative'
 
     def entity_call(self, entity):
-        alternatives = []
-        for rschema, _targets, role in entity.e_schema.relation_definitions():
-            rtype = rschema.type
-            if rtype.startswith('seda_') and rtype != self.cw_extra_kwargs['rtype']:
-                target_rset = entity.related(rtype, role)
-                if target_rset:
-                    alternatives.append(self._cw.view('seda.type_meta', rset=target_rset))
+        alternatives = alternative_values(entity, self.cw_extra_kwargs['rtype'])
         self.w(u'<div class="alternative">')
         if alternatives:
-            self.w((' <b>%s</b> ' % self._cw._(' ALT_I18N ')).join(alternatives))
+            self.w(alternatives)
         else:
             self.wdata(self._cw._('<no value specified>'))
         entity.view('seda.xsdmeta', w=self.w)