Commit ab16a12e authored by Sylvain Thénault's avatar Sylvain Thénault
Browse files

[refactor] drop rset.vreg attribute, vreg should be accessed through rset.req....

[refactor] drop rset.vreg attribute, vreg should be accessed through rset.req. Also kill decorate_rset, simply set rset.req where we were calling this method.
parent 3827b9ee77ac
......@@ -203,11 +203,6 @@ class DBAPIRequest(RequestSessionBase):
self.pgettext = lambda x, y: y
self.debug('request default language: %s', self.lang)
def decorate_rset(self, rset):
rset.vreg = self.vreg
rset.req = self
return rset
def describe(self, eid):
"""return a tuple (type, sourceuri, extid) for the entity with id <eid>"""
return self.cnx.describe(eid)
......@@ -659,11 +654,11 @@ class Cursor(object):
Return values are not defined by the DB-API, but this here it returns a
ResultSet object.
"""
self._res = res = self._repo.execute(self._sessid, operation,
parameters, eid_key, build_descr)
self.req.decorate_rset(res)
self._res = rset = self._repo.execute(self._sessid, operation,
parameters, eid_key, build_descr)
rset.req = self.req
self._index = 0
return res
return rset
def executemany(self, operation, seq_of_parameters):
......
......@@ -146,11 +146,6 @@ class FakeSession(RequestSessionBase):
def system_sql(self, sql, args=None):
pass
def decorate_rset(self, rset, propagate=False):
rset.vreg = self.vreg
rset.req = self
return rset
def set_entity_cache(self, entity):
pass
......
......@@ -441,7 +441,8 @@ class Entity(AppObject, dict):
"""returns a resultset containing `self` information"""
rset = ResultSet([(self.eid,)], 'Any X WHERE X eid %(x)s',
{'x': self.eid}, [(self.__regid__,)])
return self._cw.decorate_rset(rset)
rset.req = self._cw
return rset
def to_complete_relations(self):
"""by default complete final relations to when calling .complete()"""
......@@ -543,7 +544,7 @@ class Entity(AppObject, dict):
value = rset[i]
if value is None:
rrset = ResultSet([], rql, {'x': self.eid})
self._cw.decorate_rset(rrset)
rrset.req = self._cw
else:
rrset = self._cw.eid_rset(value)
self.set_related_cache(rtype, role, rrset)
......
......@@ -76,7 +76,8 @@ class RequestSessionBase(object):
def get_entity(row, col=0, etype=etype, req=self, rset=rset):
return req.vreg.etype_class(etype)(req, rset, row, col)
rset.get_entity = get_entity
return self.decorate_rset(rset)
rset.req = self
return rset
def eid_rset(self, eid, etype=None):
"""return a result set for the given eid without doing actual query
......@@ -88,14 +89,17 @@ class RequestSessionBase(object):
etype = self.describe(eid)[0]
rset = ResultSet([(eid,)], 'Any X WHERE X eid %(x)s', {'x': eid},
[(etype,)])
return self.decorate_rset(rset)
rset.req = self
return rset
def empty_rset(self):
"""return a result set for the given eid without doing actual query
(we have the eid, we can suppose it exists and user has access to the
entity)
"""
return self.decorate_rset(ResultSet([], 'Any X WHERE X eid -1'))
rset = ResultSet([], 'Any X WHERE X eid -1')
rset.req = self
return rset
def entity_from_eid(self, eid, etype=None):
"""return an entity instance for the given eid. No query is done"""
......@@ -387,10 +391,6 @@ class RequestSessionBase(object):
"""return the root url of the instance"""
raise NotImplementedError
def decorate_rset(self, rset):
"""add vreg/req (at least) attributes to the given result set """
raise NotImplementedError
def describe(self, eid):
"""return a tuple (type, sourceuri, extid) for the entity with id <eid>"""
raise NotImplementedError
......
......@@ -50,7 +50,6 @@ class ResultSet(object):
# .limit method
self.limited = None
# set by the cursor which returned this resultset
self.vreg = None
self.req = None
# actions cache
self._rsetactions = None
......@@ -83,7 +82,7 @@ class ResultSet(object):
try:
return self._rsetactions[key]
except KeyError:
actions = self.vreg['actions'].poss_visible_objects(
actions = self.req.vreg['actions'].poss_visible_objects(
self.req, rset=self, **kwargs)
self._rsetactions[key] = actions
return actions
......@@ -115,14 +114,16 @@ class ResultSet(object):
# method anymore (syt)
rset = ResultSet(self.rows+rset.rows, self.rql, self.args,
self.description +rset.description)
return self.req.decorate_rset(rset)
rset.req = self.req
return rset
def copy(self, rows=None, descr=None):
if rows is None:
rows = self.rows[:]
descr = self.description[:]
rset = ResultSet(rows, self.rql, self.args, descr)
return self.req.decorate_rset(rset)
rset.req = self.req
return rset
def transformed_rset(self, transformcb):
""" the result set according to a given column types
......@@ -258,8 +259,8 @@ class ResultSet(object):
# try to get page boundaries from the navigation component
# XXX we should probably not have a ref to this component here (eg in
# cubicweb)
nav = self.vreg['components'].select_or_none('navigation', self.req,
rset=self)
nav = self.req.vreg['components'].select_or_none('navigation', self.req,
rset=self)
if nav:
start, stop = nav.page_boundaries()
rql = self._limit_offset_rql(stop - start, start)
......@@ -389,7 +390,7 @@ class ResultSet(object):
"""
etype = self.description[row][col]
try:
eschema = self.vreg.schema.eschema(etype)
eschema = self.req.vreg.schema.eschema(etype)
if eschema.final:
raise NotAnEntity(etype)
except KeyError:
......@@ -433,8 +434,8 @@ class ResultSet(object):
return entity
# build entity instance
etype = self.description[row][col]
entity = self.vreg['etypes'].etype_class(etype)(req, rset=self,
row=row, col=col)
entity = self.req.vreg['etypes'].etype_class(etype)(req, rset=self,
row=row, col=col)
entity.set_eid(eid)
# cache entity
req.set_entity_cache(entity)
......@@ -470,7 +471,7 @@ class ResultSet(object):
else:
rql = 'Any Y WHERE Y %s X, X eid %s'
rrset = ResultSet([], rql % (attr, entity.eid))
req.decorate_rset(rrset)
rrset.req = req
else:
rrset = self._build_entity(row, outerselidx).as_rset()
entity.set_related_cache(attr, role, rrset)
......@@ -487,10 +488,10 @@ class ResultSet(object):
rqlst = self._rqlst.copy()
# to avoid transport overhead when pyro is used, the schema has been
# unset from the syntax tree
rqlst.schema = self.vreg.schema
self.vreg.rqlhelper.annotate(rqlst)
rqlst.schema = self.req.vreg.schema
self.req.vreg.rqlhelper.annotate(rqlst)
else:
rqlst = self.vreg.parse(self.req, self.rql, self.args)
rqlst = self.req.vreg.parse(self.req, self.rql, self.args)
return rqlst
@cached
......@@ -530,7 +531,7 @@ class ResultSet(object):
etype = self.description[row][col]
# final type, find a better one to locate the correct subquery
# (ambiguous if possible)
eschema = self.vreg.schema.eschema
eschema = self.req.vreg.schema.eschema
if eschema(etype).final:
for select in rqlst.children:
try:
......
......@@ -611,15 +611,11 @@ class Session(RequestSessionBase):
"""return the source where the entity with id <eid> is located"""
return self.repo.source_from_eid(eid, self)
def decorate_rset(self, rset):
rset.vreg = self.vreg
rset.req = self
return rset
def execute(self, rql, kwargs=None, eid_key=None, build_descr=True):
"""db-api like method directly linked to the querier execute method"""
rset = self._execute(self, rql, kwargs, eid_key, build_descr)
return self.decorate_rset(rset)
rset.req = self
return rset
def _clear_thread_data(self):
"""remove everything from the thread local storage, except pool
......
......@@ -11,7 +11,7 @@ from urlparse import urlsplit
from rql import parse
from logilab.common.testlib import TestCase, unittest_main
from logilab.common.testlib import TestCase, unittest_main, mock_object
from cubicweb.devtools.testlib import CubicWebTC
from cubicweb.rset import NotAnEntity, ResultSet, attr_desc_iterator
......@@ -60,7 +60,7 @@ class ResultSetTC(CubicWebTC):
self.rset = ResultSet([[12, 'adim'], [13, 'syt']],
'Any U,L where U is CWUser, U login L',
description=[['CWUser', 'String'], ['Bar', 'String']])
self.rset.vreg = self.vreg
self.rset.req = mock_object(vreg=self.vreg)
def compare_urls(self, url1, url2):
info1 = urlsplit(url1)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment