entities/adapters.py
changeset 10973 0939ad2edf63
parent 10732 6231587fcfc5
child 10974 6557833657d6
equal deleted inserted replaced
10969:b4de8b1cc135 10973:0939ad2edf63
   347                 break
   347                 break
   348         path.reverse()
   348         path.reverse()
   349         return path
   349         return path
   350 
   350 
   351 
   351 
       
   352 class ISerializableAdapter(view.EntityAdapter):
       
   353     """Adapter to serialize an entity to a bare python structure that may be
       
   354     directly serialized to e.g. JSON.
       
   355     """
       
   356 
       
   357     __regid__ = 'ISerializable'
       
   358     __select__ = is_instance('Any')
       
   359 
       
   360     def serialize(self):
       
   361         entity = self.entity
       
   362         entity.complete()
       
   363         data = {
       
   364             'cw_etype': entity.cw_etype,
       
   365             'cw_source': entity.cw_metainformation()['source']['uri'],
       
   366         }
       
   367         for rschema, __ in entity.e_schema.attribute_definitions():
       
   368             attr = rschema.type
       
   369             try:
       
   370                 value = entity.cw_attr_cache[attr]
       
   371             except KeyError:
       
   372                 # Bytes
       
   373                 continue
       
   374             data[attr] = value
       
   375         return data
       
   376 
       
   377 
   352 # error handling adapters ######################################################
   378 # error handling adapters ######################################################
   353 
   379 
   354 from cubicweb import UniqueTogetherError
   380 from cubicweb import UniqueTogetherError
   355 
   381 
   356 class IUserFriendlyError(view.EntityAdapter):
   382 class IUserFriendlyError(view.EntityAdapter):