[PATCH 3 of 3 eac V3] Add JsonAttrsMixin class for entities with json string attributes

Guillaume Vandevelde guillaume.vandevelde at logilab.fr
Mon Aug 19 19:06:46 CEST 2019


# HG changeset patch
# User Guillaume Vandevelde <gvandevelde at logilab.fr>
# Date 1566220108 -7200
#      Mon Aug 19 15:08:28 2019 +0200
# Node ID ad79c53fe45c26447f34934f6645d2cb40d538a1
# Parent  d8f8eeefe63b3618d176db8dfb3717c23956f771
# Available At http://hg.logilab.org/review/cubes/eac
#              hg pull http://hg.logilab.org/review/cubes/eac -r ad79c53fe45c
Add JsonAttrsMixin class for entities with json string attributes

diff -r d8f8eeefe63b -r ad79c53fe45c cubicweb_eac/entities.py
--- a/cubicweb_eac/entities.py	Mon Aug 19 16:49:45 2019 +0200
+++ b/cubicweb_eac/entities.py	Mon Aug 19 15:08:28 2019 +0200
@@ -112,7 +112,14 @@
         return self.hierarchical_child[0]
 
 
-class EACResourceRelation(AnyEntity):
+class JsonAttrsMixin(object):
+
+    @property
+    def json_attrs(self):
+        return json.loads(self.xml_attributes)
+
+
+class EACResourceRelation(AnyEntity, JsonAttrsMixin):
     __regid__ = 'EACResourceRelation'
     fetch_attrs, cw_fetch_order = fetch_config(('agent_role',
                                                 'xml_attributes',
@@ -127,10 +134,6 @@
     def resource(self):
         return self.resource_relation_resource[0]
 
-    @property
-    def json_attrs(self):
-        return json.loads(self.xml_attributes)
-
     def dc_title(self):
         agent_title = self.record.dc_title()
         if self.agent_role:
@@ -143,7 +146,7 @@
                  'to': resource_title})
 
 
-class EACFunctionRelation(AnyEntity):
+class EACFunctionRelation(AnyEntity, JsonAttrsMixin):
     __regid__ = 'EACFunctionRelation'
     fetch_attrs, cw_fetch_order = fetch_config(('r_type',
                                                 'description',
diff -r d8f8eeefe63b -r ad79c53fe45c test/test_dataimport.py
--- a/test/test_dataimport.py	Mon Aug 19 16:49:45 2019 +0200
+++ b/test/test_dataimport.py	Mon Aug 19 15:08:28 2019 +0200
@@ -771,12 +771,12 @@
                                                 u"{http://www.w3.org/1999/xlink}type": u"simple"})
         rset = cnx.find('EACFunctionRelation', r_type=u'performs')
         func_relation = rset.one()
-        self.assertEqual(func_relation.xml_attributes,
-                         u'{"{http://www.w3.org/1999/xlink}actuate": "onLoad", '
-                         u'"{http://www.w3.org/1999/xlink}arcrole": '
-                         u'"http://test_arcrole.lol.com", '
-                         u'"{http://www.w3.org/1999/xlink}role": '
-                         u'"http://test_role.lmao.com"}')
+        self.assertEqual(func_relation.json_attrs,
+                         {u'{http://www.w3.org/1999/xlink}actuate': u'onLoad',
+                          u'{http://www.w3.org/1999/xlink}arcrole':
+                          u'http://test_arcrole.lol.com',
+                          u'{http://www.w3.org/1999/xlink}role':
+                          u'http://test_role.lmao.com'})
         self.assertEqual(func_relation.relation_entry,
                          u'Alumni communication\n\tmanagement, '
                          'University of\n\tGlasgow\n\t')



More information about the saem-devel mailing list