[entity,views/json] backout 353bbd17a8b6 (reopens #2559931) stable
authorAurelien Campeas <aurelien.campeas@logilab.fr>
Fri, 18 Jan 2013 17:37:35 +0100
branchstable
changeset 8657 5ab6ec83d5ba
parent 8656 9bb93efa1952
child 8659 cd9fddc21361
[entity,views/json] backout 353bbd17a8b6 (reopens #2559931) Calling .complete() unconditionnally from the json encoder is unsafe since on entity creation validation: * an eid may have been drawn (hence even .has_eid() would not help) while processing form data in the edit controller * a ValidationError may have been raised and the entity-creating transaction rollbacked This leads to a crash on the return path from the validation to the browser, where the json_dumps((status, args, entity)) call will stumble upon the .complete() call which will fail because the entity does not (any more) exist in the database.
entity.py
web/views/json.py
--- a/entity.py	Tue Jan 15 20:17:09 2013 +0100
+++ b/entity.py	Fri Jan 18 17:37:35 2013 +0100
@@ -539,10 +539,8 @@
         """custom json dumps hook to dump the entity's eid
         which is not part of dict structure itself
         """
-        self.complete()
         dumpable = self.cw_attr_cache.copy()
         dumpable['eid'] = self.eid
-        dumpable['__cwetype__'] = self.__regid__
         return dumpable
 
     def cw_adapt_to(self, interface):
--- a/web/views/json.py	Tue Jan 15 20:17:09 2013 +0100
+++ b/web/views/json.py	Fri Jan 18 17:37:35 2013 +0100
@@ -111,7 +111,12 @@
     title = _('json-entities-export-view')
 
     def call(self):
-        if self.cw_rset is None:
-            self.wdata([self.cw_extra_kwargs.get('entity')])
-        else:
-            self.wdata(list(self.cw_rset.entities()))
+        entities = []
+        for entity in self.cw_rset.entities():
+            entity.complete() # fetch all attributes
+            # hack to add extra metadata
+            entity.cw_attr_cache.update({
+                    '__cwetype__': entity.__regid__,
+                    })
+            entities.append(entity)
+        self.wdata(entities)