entities/adapters.py
changeset 10973 0939ad2edf63
parent 10732 6231587fcfc5
child 10974 6557833657d6
--- a/entities/adapters.py	Tue Dec 08 16:28:20 2015 +0100
+++ b/entities/adapters.py	Tue Dec 15 08:55:58 2015 +0100
@@ -349,6 +349,32 @@
         return path
 
 
+class ISerializableAdapter(view.EntityAdapter):
+    """Adapter to serialize an entity to a bare python structure that may be
+    directly serialized to e.g. JSON.
+    """
+
+    __regid__ = 'ISerializable'
+    __select__ = is_instance('Any')
+
+    def serialize(self):
+        entity = self.entity
+        entity.complete()
+        data = {
+            'cw_etype': entity.cw_etype,
+            'cw_source': entity.cw_metainformation()['source']['uri'],
+        }
+        for rschema, __ in entity.e_schema.attribute_definitions():
+            attr = rschema.type
+            try:
+                value = entity.cw_attr_cache[attr]
+            except KeyError:
+                # Bytes
+                continue
+            data[attr] = value
+        return data
+
+
 # error handling adapters ######################################################
 
 from cubicweb import UniqueTogetherError