[entity/req] pass the _cw object to cw_instantiate

When ClientConnection and Connection will be the same thing, we will need to give the _cw object to the freshly created entity.

If we dont do that, it will always bear a Connection whereas it may be expected to carry a Request (in the view/adapter land).

Related to #3837233.

authorAurelien Campeas <aurelien.campeas@logilab.fr>
changesetaef3935acfe3
branchdefault
phasedraft
hiddenyes
parent revision#63b6aee4da7a [rset] kill the rset._rqlst cache
child revision#4cc9a8c4684f [shared data] remove get/set_shared_data api
files modified by this revision
entity.py
req.py
# HG changeset patch
# User Aurelien Campeas <aurelien.campeas@logilab.fr>
# Date 1401210221 -7200
# Tue May 27 19:03:41 2014 +0200
# Node ID aef3935acfe3b2e07b21918c375f55dc5fe75a91
# Parent 63b6aee4da7a8e30b947cb9263e6baf71e00e5f9
[entity/req] pass the _cw object to cw_instantiate

When ClientConnection and Connection will be the same thing, we will
need to give the _cw object to the freshly created entity.

If we dont do that, it will always bear a Connection whereas it may be
expected to carry a Request (in the view/adapter land).

Related to #3837233.

diff --git a/entity.py b/entity.py
@@ -496,11 +496,11 @@
1              execute('SET %s WHERE X eid %%(x)s, Y eid IN (%s)' % (
2                  restr, ','.join(str(getattr(r, 'eid', r)) for r in values)),
3                      {'x': eid}, build_descr=False)
4 
5      @classmethod
6 -    def cw_instantiate(cls, execute, **kwargs):
7 +    def cw_instantiate(cls, _cw, **kwargs):
8          """add a new entity of this given type
9 
10          Example (in a shell session):
11 
12          >>> companycls = vreg['etypes'].etype_class('Company')
@@ -516,13 +516,17 @@
13          rql, qargs, pendingrels, attrcache = cls._cw_build_entity_query(kwargs)
14          if rql:
15              rql = 'INSERT %s X: %s' % (cls.__regid__, rql)
16          else:
17              rql = 'INSERT %s X' % (cls.__regid__)
18 -        created = execute(rql, qargs).get_entity(0, 0)
19 +        created = _cw.execute(rql, qargs).get_entity(0, 0)
20 +        # the planner built entity._cw with a cnx (Connection) object
21 +        # however we want the original _cw object because it may be a request
22 +        # with a .form, etc.
23 +        created._cw = _cw
24          created._cw_update_attr_cache(attrcache)
25 -        cls._cw_handle_pending_relations(created.eid, pendingrels, execute)
26 +        cls._cw_handle_pending_relations(created.eid, pendingrels, _cw.execute)
27          return created
28 
29      def __init__(self, req, rset=None, row=None, col=0):
30          AppObject.__init__(self, req, rset=rset, row=row, col=col)
31          self._cw_related_cache = {}
diff --git a/req.py b/req.py
@@ -170,11 +170,11 @@
32          >>> create_entity('Person', firstname=u'John', surname=u'Doe',
33          ...               works_for=c)
34 
35          """
36          cls = self.vreg['etypes'].etype_class(etype)
37 -        return cls.cw_instantiate(self.execute, **kwargs)
38 +        return cls.cw_instantiate(self, **kwargs)
39 
40      @deprecated('[3.18] use find(etype, **kwargs).entities()')
41      def find_entities(self, etype, **kwargs):
42          """find entities of the given type and attribute values.
43