Add 'subject' and 'object' properties to the various kind of relations
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 16 Mar 2017 15:34:19 +0100
changeset 202 045044bbc096
parent 199 11bb8cb5c00f
child 203 99d104baf729
Add 'subject' and 'object' properties to the various kind of relations so they can be accessed consistently.
cubicweb_eac/entities.py
--- a/cubicweb_eac/entities.py	Wed Mar 08 23:11:06 2017 +0100
+++ b/cubicweb_eac/entities.py	Thu Mar 16 15:34:19 2017 +0100
@@ -66,14 +66,50 @@
     fetch_attrs, cw_fetch_order = fetch_config(('name',))
 
 
-class ChronologicalRelation(AnyEntity):
-    __regid__ = 'ChronologicalRelation'
+class _Relation(AnyEntity):
+    __abstract__ = True
 
     def dc_description(self):
         if self.description:
             return self.description
 
 
+class ChronologicalRelation(_Relation):
+    __regid__ = 'ChronologicalRelation'
+
+    @property
+    def subject(self):
+        return self.chronological_predecessor[0]
+
+    @property
+    def object(self):
+        return self.chronological_successor[0]
+
+
+class AssociationRelation(_Relation):
+    __regid__ = 'AssociationRelation'
+
+    @property
+    def subject(self):
+        return self.association_from[0]
+
+    @property
+    def object(self):
+        return self.association_to[0]
+
+
+class HierarchicalRelation(_Relation):
+    __regid__ = 'HierarchicalRelation'
+
+    @property
+    def subject(self):
+        return self.hierarchical_parent[0]
+
+    @property
+    def object(self):
+        return self.hierarchical_child[0]
+
+
 class EACResourceRelation(AnyEntity):
     __regid__ = 'EACResourceRelation'
     fetch_attrs, cw_fetch_order = fetch_config(('agent_role', 'resource_role', 'description'))