[profile gen] Drop undesired elements when attribute of a complex path doesn't exist
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 20 Jul 2016 11:53:51 +0200
changeset 1437 f8446b60db7f
parent 1436 9d3aae1b9c60
child 1466 a89ff4412318
[profile gen] Drop undesired elements when attribute of a complex path doesn't exist In this case, return the 'intermediary' entity if any, it appears to be the end of the path anyway. This avoid allowing undefined elements in the profile.
entities/profile_generation.py
test/test_profile_generation.py
--- a/entities/profile_generation.py	Wed Jul 20 11:47:19 2016 +0200
+++ b/entities/profile_generation.py	Wed Jul 20 11:53:51 2016 +0200
@@ -26,6 +26,7 @@
 from cubicweb.view import EntityAdapter
 
 from cubes.seda.xsd import BASE_TYPES, XSDM_MAPPING, JUMP_ELEMENTS
+from cubes.seda.xsd2yams import SKIP_ATTRS
 
 
 JUMPED_OPTIONAL_ELEMENTS = set(('FileInfo', 'PhysicalDimensions', 'Coverage'))
@@ -752,14 +753,16 @@
 def _complex_path_target_values(entity, path):
     rschema = entity._cw.vreg.schema.rschema
     entities = (entity,)
+    rtype_targets = []
     for rtype, role, target_etype, _ in path:
-        rtype_targets = []
         try:
             rdefschema = next(iter(rschema(rtype).rdefs.values()))
         except KeyError:
+            if rtype in SKIP_ATTRS:
+                return rtype_targets
             # element is still in the intermediary representation but not in the schema
-            rtype_targets.append((None, None))
-            return rtype_targets
+            return [(None, None)]
+        rtype_targets = []
         for entity in entities:
             if target_etype in BASE_TYPES:
                 rtype_targets.append((entity, getattr(entity, rtype, None)))
--- a/test/test_profile_generation.py	Wed Jul 20 11:47:19 2016 +0200
+++ b/test/test_profile_generation.py	Wed Jul 20 11:53:51 2016 +0200
@@ -621,6 +621,8 @@
                                mda_scheme_url=mda_scheme.absolute_url())
         # ensure jumped element without content are not there
         self.assertEqual(len(self.get_elements(root, 'Gps')), 0)
+        # ensure element with skipped value are not there
+        self.assertEqual(len(self.get_elements(root, 'TransactedDate')), 0)
         self.assertProfileDetails(root)