[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>
changeset6f62344ea805
branchdefault
phasedraft
hiddenyes
parent revision#ff7f86d8393d [devtools] call turn_repo_off in tearDown (closes #4673491)
child revision#53dedf7028f9 [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 6f62344ea8059a00f197a91e1b9b750f21ba27ba
# Parent ff7f86d8393db5841ff1d91343d3a4d227f5d091
[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
@@ -497,11 +497,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')
@@ -518,16 +518,20 @@
13          if rql:
14              rql = 'INSERT %s X: %s' % (cls.__regid__, rql)
15          else:
16              rql = 'INSERT %s X' % (cls.__regid__)
17          try:
18 -            created = execute(rql, qargs).get_entity(0, 0)
19 +            created = _cw.execute(rql, qargs).get_entity(0, 0)
20          except IndexError:
21              raise Exception('could not create a %r with %r (%r)' %
22                              (cls.__regid__, rql, qargs))
23 +        # the planner built entity._cw with a cnx (Connection) object
24 +        # however we want the original _cw object because it may be a request
25 +        # with a .form, etc.
26 +        created._cw = _cw
27          created._cw_update_attr_cache(attrcache)
28 -        cls._cw_handle_pending_relations(created.eid, pendingrels, execute)
29 +        cls._cw_handle_pending_relations(created.eid, pendingrels, _cw.execute)
30          return created
31 
32      def __init__(self, req, rset=None, row=None, col=0):
33          AppObject.__init__(self, req, rset=rset, row=row, col=col)
34          self._cw_related_cache = {}
diff --git a/req.py b/req.py
@@ -170,11 +170,11 @@
35          >>> create_entity('Person', firstname=u'John', surname=u'Doe',
36          ...               works_for=c)
37 
38          """
39          cls = self.vreg['etypes'].etype_class(etype)
40 -        return cls.cw_instantiate(self.execute, **kwargs)
41 +        return cls.cw_instantiate(self, **kwargs)
42 
43      @deprecated('[3.18] use find(etype, **kwargs).entities()')
44      def find_entities(self, etype, **kwargs):
45          """find entities of the given type and attribute values.
46