author | Nicolas Chauvat <nicolas.chauvat@logilab.fr> |
Fri, 14 Aug 2009 12:14:29 +0200 | |
changeset 2845 | 660caa3ddc4f |
parent 2831 | ddde6cf856a1 |
child 2880 | bfc8e1831290 |
permissions | -rw-r--r-- |
0 | 1 |
"""Base class for entity objects manipulated in clients |
2 |
||
3 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1904
diff
changeset
|
4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1904
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 7 |
""" |
8 |
__docformat__ = "restructuredtext en" |
|
9 |
||
1154 | 10 |
from warnings import warn |
11 |
||
0 | 12 |
from logilab.common import interface |
13 |
from logilab.common.compat import all |
|
14 |
from logilab.common.decorators import cached |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2125
diff
changeset
|
15 |
from logilab.mtconverter import TransformData, TransformError, xml_escape |
631
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
479
diff
changeset
|
16 |
|
0 | 17 |
from rql.utils import rqlvar_maker |
18 |
||
19 |
from cubicweb import Unauthorized |
|
20 |
from cubicweb.rset import ResultSet |
|
692
800592b8d39b
replace deprecated cubicweb.common.selectors by its new module path (cubicweb.selectors)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
631
diff
changeset
|
21 |
from cubicweb.selectors import yes |
2656
a93ae0f6c0ad
R [base classes] only AppObject remaning, no more AppRsetObject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
22 |
from cubicweb.appobject import AppObject |
2827
d1a89d165045
remove 3.2 bw compat code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2824
diff
changeset
|
23 |
from cubicweb.schema import RQLVocabularyConstraint, RQLConstraint |
709 | 24 |
|
2421
08d42928fe36
does not make sense any more
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2381
diff
changeset
|
25 |
from cubicweb.common.uilib import printable_value, soup2xhtml |
08d42928fe36
does not make sense any more
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2381
diff
changeset
|
26 |
from cubicweb.common.mixins import MI_REL_TRIGGERS |
08d42928fe36
does not make sense any more
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2381
diff
changeset
|
27 |
from cubicweb.common.mttransforms import ENGINE |
0 | 28 |
|
29 |
_marker = object() |
|
30 |
||
31 |
def greater_card(rschema, subjtypes, objtypes, index): |
|
32 |
for subjtype in subjtypes: |
|
33 |
for objtype in objtypes: |
|
34 |
card = rschema.rproperty(subjtype, objtype, 'cardinality')[index] |
|
35 |
if card in '+*': |
|
36 |
return card |
|
37 |
return '1' |
|
38 |
||
39 |
||
2784
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
40 |
class Entity(AppObject, dict): |
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
41 |
"""an entity instance has e_schema automagically set on |
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
42 |
the class and instances has access to their issuing cursor. |
1474 | 43 |
|
2784
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
44 |
A property is set for each attribute and relation on each entity's type |
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
45 |
class. Becare that among attributes, 'eid' is *NEITHER* stored in the |
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
46 |
dict containment (which acts as a cache for other attributes dynamically |
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
47 |
fetched) |
0 | 48 |
|
49 |
:type e_schema: `cubicweb.schema.EntitySchema` |
|
50 |
:ivar e_schema: the entity's schema |
|
51 |
||
52 |
:type rest_var: str |
|
53 |
:cvar rest_var: indicates which attribute should be used to build REST urls |
|
54 |
If None is specified, the first non-meta attribute will |
|
55 |
be used |
|
1474 | 56 |
|
0 | 57 |
:type skip_copy_for: list |
58 |
:cvar skip_copy_for: a list of relations that should be skipped when copying |
|
59 |
this kind of entity. Note that some relations such |
|
60 |
as composite relations or relations that have '?1' as object |
|
61 |
cardinality |
|
62 |
""" |
|
63 |
__registry__ = 'etypes' |
|
717 | 64 |
__select__ = yes() |
1264
fe2934a7df7f
cleanup, avoid spurious warning
sylvain.thenault@logilab.fr
parents:
1177
diff
changeset
|
65 |
|
1474 | 66 |
# class attributes that must be set in class definition |
0 | 67 |
rest_attr = None |
1138
22f634977c95
make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents:
1132
diff
changeset
|
68 |
fetch_attrs = None |
0 | 69 |
skip_copy_for = () |
1264
fe2934a7df7f
cleanup, avoid spurious warning
sylvain.thenault@logilab.fr
parents:
1177
diff
changeset
|
70 |
# class attributes set automatically at registration time |
fe2934a7df7f
cleanup, avoid spurious warning
sylvain.thenault@logilab.fr
parents:
1177
diff
changeset
|
71 |
e_schema = None |
1474 | 72 |
|
0 | 73 |
@classmethod |
2807
696ff03f9a58
__initialize__ needs the schema as argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
74 |
def __initialize__(cls, schema): |
0 | 75 |
"""initialize a specific entity class by adding descriptors to access |
76 |
entity type's attributes and relations |
|
77 |
""" |
|
2829
054a8805da52
turn id into __id__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2827
diff
changeset
|
78 |
etype = cls.__id__ |
0 | 79 |
assert etype != 'Any', etype |
2807
696ff03f9a58
__initialize__ needs the schema as argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
80 |
cls.e_schema = eschema = schema.eschema(etype) |
0 | 81 |
for rschema, _ in eschema.attribute_definitions(): |
2784
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
82 |
if rschema.type == 'eid': |
0 | 83 |
continue |
2784
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
84 |
setattr(cls, rschema.type, Attribute(rschema.type)) |
0 | 85 |
mixins = [] |
2784
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
86 |
for rschema, _, x in eschema.relation_definitions(): |
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
87 |
if (rschema, x) in MI_REL_TRIGGERS: |
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
88 |
mixin = MI_REL_TRIGGERS[(rschema, x)] |
0 | 89 |
if not (issubclass(cls, mixin) or mixin in mixins): # already mixed ? |
90 |
mixins.append(mixin) |
|
91 |
for iface in getattr(mixin, '__implements__', ()): |
|
92 |
if not interface.implements(cls, iface): |
|
93 |
interface.extend(cls, iface) |
|
2784
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
94 |
if x == 'subject': |
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
95 |
setattr(cls, rschema.type, SubjectRelation(rschema)) |
0 | 96 |
else: |
2784
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
97 |
attr = 'reverse_%s' % rschema.type |
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
98 |
setattr(cls, attr, ObjectRelation(rschema)) |
0 | 99 |
if mixins: |
100 |
cls.__bases__ = tuple(mixins + [p for p in cls.__bases__ if not p is object]) |
|
101 |
cls.debug('plugged %s mixins on %s', mixins, etype) |
|
1474 | 102 |
|
0 | 103 |
@classmethod |
104 |
def fetch_rql(cls, user, restriction=None, fetchattrs=None, mainvar='X', |
|
105 |
settype=True, ordermethod='fetch_order'): |
|
106 |
"""return a rql to fetch all entities of the class type""" |
|
107 |
restrictions = restriction or [] |
|
108 |
if settype: |
|
2829
054a8805da52
turn id into __id__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2827
diff
changeset
|
109 |
restrictions.append('%s is %s' % (mainvar, cls.__id__)) |
0 | 110 |
if fetchattrs is None: |
111 |
fetchattrs = cls.fetch_attrs |
|
112 |
selection = [mainvar] |
|
113 |
orderby = [] |
|
114 |
# start from 26 to avoid possible conflicts with X |
|
115 |
varmaker = rqlvar_maker(index=26) |
|
116 |
cls._fetch_restrictions(mainvar, varmaker, fetchattrs, selection, |
|
117 |
orderby, restrictions, user, ordermethod) |
|
118 |
rql = 'Any %s' % ','.join(selection) |
|
119 |
if orderby: |
|
120 |
rql += ' ORDERBY %s' % ','.join(orderby) |
|
121 |
rql += ' WHERE %s' % ', '.join(restrictions) |
|
122 |
return rql |
|
1474 | 123 |
|
0 | 124 |
@classmethod |
125 |
def _fetch_restrictions(cls, mainvar, varmaker, fetchattrs, |
|
126 |
selection, orderby, restrictions, user, |
|
127 |
ordermethod='fetch_order', visited=None): |
|
128 |
eschema = cls.e_schema |
|
129 |
if visited is None: |
|
130 |
visited = set((eschema.type,)) |
|
131 |
elif eschema.type in visited: |
|
132 |
# avoid infinite recursion |
|
133 |
return |
|
134 |
else: |
|
135 |
visited.add(eschema.type) |
|
136 |
_fetchattrs = [] |
|
137 |
for attr in fetchattrs: |
|
138 |
try: |
|
139 |
rschema = eschema.subject_relation(attr) |
|
140 |
except KeyError: |
|
141 |
cls.warning('skipping fetch_attr %s defined in %s (not found in schema)', |
|
2829
054a8805da52
turn id into __id__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2827
diff
changeset
|
142 |
attr, cls.__id__) |
0 | 143 |
continue |
144 |
if not user.matching_groups(rschema.get_groups('read')): |
|
145 |
continue |
|
146 |
var = varmaker.next() |
|
147 |
selection.append(var) |
|
148 |
restriction = '%s %s %s' % (mainvar, attr, var) |
|
149 |
restrictions.append(restriction) |
|
150 |
if not rschema.is_final(): |
|
151 |
# XXX this does not handle several destination types |
|
152 |
desttype = rschema.objects(eschema.type)[0] |
|
153 |
card = rschema.rproperty(eschema, desttype, 'cardinality')[0] |
|
154 |
if card not in '?1': |
|
155 |
selection.pop() |
|
156 |
restrictions.pop() |
|
157 |
continue |
|
158 |
if card == '?': |
|
159 |
restrictions[-1] += '?' # left outer join if not mandatory |
|
2831
ddde6cf856a1
quick and dirty fix until introduction of CWEntityManager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2830
diff
changeset
|
160 |
# XXX user.req.vreg iiiirk |
ddde6cf856a1
quick and dirty fix until introduction of CWEntityManager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2830
diff
changeset
|
161 |
destcls = user.req.vreg['etypes'].etype_class(desttype) |
0 | 162 |
destcls._fetch_restrictions(var, varmaker, destcls.fetch_attrs, |
163 |
selection, orderby, restrictions, |
|
164 |
user, ordermethod, visited=visited) |
|
165 |
orderterm = getattr(cls, ordermethod)(attr, var) |
|
166 |
if orderterm: |
|
167 |
orderby.append(orderterm) |
|
168 |
return selection, orderby, restrictions |
|
169 |
||
1471
c889c3bcf5ec
new parent_classes method (cached)
sylvain.thenault@logilab.fr
parents:
1435
diff
changeset
|
170 |
@classmethod |
c889c3bcf5ec
new parent_classes method (cached)
sylvain.thenault@logilab.fr
parents:
1435
diff
changeset
|
171 |
@cached |
1840
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
172 |
def _rest_attr_info(cls): |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
173 |
mainattr, needcheck = 'eid', True |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
174 |
if cls.rest_attr: |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
175 |
mainattr = cls.rest_attr |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
176 |
needcheck = not cls.e_schema.has_unique_values(mainattr) |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
177 |
else: |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
178 |
for rschema in cls.e_schema.subject_relations(): |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
179 |
if rschema.is_final() and rschema != 'eid' and cls.e_schema.has_unique_values(rschema): |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
180 |
mainattr = str(rschema) |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
181 |
needcheck = False |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
182 |
break |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
183 |
if mainattr == 'eid': |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
184 |
needcheck = False |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
185 |
return mainattr, needcheck |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
186 |
|
1435
6cd6172718bb
allow to instantiate an entity without rset
sylvain.thenault@logilab.fr
parents:
1363
diff
changeset
|
187 |
def __init__(self, req, rset=None, row=None, col=0): |
2822
f26578339214
deprecate appobject.vreg and rename appobject instance attributes using cw_ prefix
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2818
diff
changeset
|
188 |
AppObject.__init__(self, req, rset=rset, row=row, col=col) |
0 | 189 |
dict.__init__(self) |
190 |
self._related_cache = {} |
|
191 |
if rset is not None: |
|
192 |
self.eid = rset[row][col] |
|
193 |
else: |
|
194 |
self.eid = None |
|
195 |
self._is_saved = True |
|
1474 | 196 |
|
0 | 197 |
def __repr__(self): |
198 |
return '<Entity %s %s %s at %s>' % ( |
|
199 |
self.e_schema, self.eid, self.keys(), id(self)) |
|
200 |
||
201 |
def __nonzero__(self): |
|
202 |
return True |
|
203 |
||
204 |
def __hash__(self): |
|
205 |
return id(self) |
|
206 |
||
207 |
def pre_add_hook(self): |
|
208 |
"""hook called by the repository before doing anything to add the entity |
|
209 |
(before_add entity hooks have not been called yet). This give the |
|
210 |
occasion to do weird stuff such as autocast (File -> Image for instance). |
|
1474 | 211 |
|
0 | 212 |
This method must return the actual entity to be added. |
213 |
""" |
|
214 |
return self |
|
1474 | 215 |
|
0 | 216 |
def set_eid(self, eid): |
217 |
self.eid = self['eid'] = eid |
|
218 |
||
219 |
def has_eid(self): |
|
220 |
"""return True if the entity has an attributed eid (False |
|
221 |
meaning that the entity has to be created |
|
222 |
""" |
|
223 |
try: |
|
224 |
int(self.eid) |
|
225 |
return True |
|
226 |
except (ValueError, TypeError): |
|
227 |
return False |
|
228 |
||
229 |
def is_saved(self): |
|
230 |
"""during entity creation, there is some time during which the entity |
|
231 |
has an eid attributed though it's not saved (eg during before_add_entity |
|
232 |
hooks). You can use this method to ensure the entity has an eid *and* is |
|
233 |
saved in its source. |
|
234 |
""" |
|
235 |
return self.has_eid() and self._is_saved |
|
1474 | 236 |
|
0 | 237 |
@cached |
238 |
def metainformation(self): |
|
239 |
res = dict(zip(('type', 'source', 'extid'), self.req.describe(self.eid))) |
|
240 |
res['source'] = self.req.source_defs()[res['source']] |
|
241 |
return res |
|
242 |
||
475
b32a5772ff06
should clear local perm cache if first attempt failed
sylvain.thenault@logilab.fr
parents:
413
diff
changeset
|
243 |
def clear_local_perm_cache(self, action): |
b32a5772ff06
should clear local perm cache if first attempt failed
sylvain.thenault@logilab.fr
parents:
413
diff
changeset
|
244 |
for rqlexpr in self.e_schema.get_rqlexprs(action): |
b32a5772ff06
should clear local perm cache if first attempt failed
sylvain.thenault@logilab.fr
parents:
413
diff
changeset
|
245 |
self.req.local_perm_cache.pop((rqlexpr.eid, (('x', self.eid),)), None) |
b32a5772ff06
should clear local perm cache if first attempt failed
sylvain.thenault@logilab.fr
parents:
413
diff
changeset
|
246 |
|
0 | 247 |
def check_perm(self, action): |
248 |
self.e_schema.check_perm(self.req, action, self.eid) |
|
249 |
||
250 |
def has_perm(self, action): |
|
251 |
return self.e_schema.has_perm(self.req, action, self.eid) |
|
1474 | 252 |
|
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
253 |
def view(self, vid, __registry='views', **kwargs): |
0 | 254 |
"""shortcut to apply a view on this entity""" |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
255 |
return self.vreg[__registry].render(vid, self.req, rset=self.rset, |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
256 |
row=self.row, col=self.col, **kwargs) |
0 | 257 |
|
2059
af33833d7571
absolute_url / build_url refactoring to avoid potential name clash
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2058
diff
changeset
|
258 |
def absolute_url(self, *args, **kwargs): |
0 | 259 |
"""return an absolute url to view this entity""" |
2059
af33833d7571
absolute_url / build_url refactoring to avoid potential name clash
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2058
diff
changeset
|
260 |
# use *args since we don't want first argument to be "anonymous" to |
af33833d7571
absolute_url / build_url refactoring to avoid potential name clash
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2058
diff
changeset
|
261 |
# avoid potential clash with kwargs |
af33833d7571
absolute_url / build_url refactoring to avoid potential name clash
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2058
diff
changeset
|
262 |
if args: |
af33833d7571
absolute_url / build_url refactoring to avoid potential name clash
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2058
diff
changeset
|
263 |
assert len(args) == 1, 'only 0 or 1 non-named-argument expected' |
af33833d7571
absolute_url / build_url refactoring to avoid potential name clash
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2058
diff
changeset
|
264 |
method = args[0] |
af33833d7571
absolute_url / build_url refactoring to avoid potential name clash
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2058
diff
changeset
|
265 |
else: |
af33833d7571
absolute_url / build_url refactoring to avoid potential name clash
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2058
diff
changeset
|
266 |
method = None |
0 | 267 |
# in linksearch mode, we don't want external urls else selecting |
268 |
# the object for use in the relation is tricky |
|
269 |
# XXX search_state is web specific |
|
270 |
if getattr(self.req, 'search_state', ('normal',))[0] == 'normal': |
|
271 |
kwargs['base_url'] = self.metainformation()['source'].get('base-url') |
|
1904
e23536d29231
minor refactoring of absolute_url()
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1842
diff
changeset
|
272 |
if method in (None, 'view'): |
1842
c7a22540d6f7
add some bw compat code for old rest_path prototype
sylvain.thenault@logilab.fr
parents:
1840
diff
changeset
|
273 |
try: |
c7a22540d6f7
add some bw compat code for old rest_path prototype
sylvain.thenault@logilab.fr
parents:
1840
diff
changeset
|
274 |
kwargs['_restpath'] = self.rest_path(kwargs.get('base_url')) |
c7a22540d6f7
add some bw compat code for old rest_path prototype
sylvain.thenault@logilab.fr
parents:
1840
diff
changeset
|
275 |
except TypeError: |
c7a22540d6f7
add some bw compat code for old rest_path prototype
sylvain.thenault@logilab.fr
parents:
1840
diff
changeset
|
276 |
warn('%s: rest_path() now take use_ext_eid argument, ' |
2829
054a8805da52
turn id into __id__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2827
diff
changeset
|
277 |
'please update' % self.__id__, DeprecationWarning) |
1842
c7a22540d6f7
add some bw compat code for old rest_path prototype
sylvain.thenault@logilab.fr
parents:
1840
diff
changeset
|
278 |
kwargs['_restpath'] = self.rest_path() |
0 | 279 |
else: |
280 |
kwargs['rql'] = 'Any X WHERE X eid %s' % self.eid |
|
2818
326375561412
propagate some api changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2807
diff
changeset
|
281 |
return self.req.build_url(method, **kwargs) |
0 | 282 |
|
1840
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
283 |
def rest_path(self, use_ext_eid=False): |
0 | 284 |
"""returns a REST-like (relative) path for this entity""" |
285 |
mainattr, needcheck = self._rest_attr_info() |
|
286 |
etype = str(self.e_schema) |
|
1840
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
287 |
path = etype.lower() |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
288 |
if mainattr != 'eid': |
2784
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
289 |
value = getattr(self, mainattr) |
2125
19861294506f
https://www.logilab.net/cwo/ticket/343724
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2005
diff
changeset
|
290 |
if value is None or unicode(value) == u'': |
1840
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
291 |
mainattr = 'eid' |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
292 |
path += '/eid' |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
293 |
elif needcheck: |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
294 |
# make sure url is not ambiguous |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
295 |
rql = 'Any COUNT(X) WHERE X is %s, X %s %%(value)s' % ( |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
296 |
etype, mainattr) |
1904
e23536d29231
minor refactoring of absolute_url()
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1842
diff
changeset
|
297 |
nbresults = self.req.execute(rql, {'value' : value})[0][0] |
e23536d29231
minor refactoring of absolute_url()
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1842
diff
changeset
|
298 |
if nbresults != 1: # ambiguity? |
e23536d29231
minor refactoring of absolute_url()
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1842
diff
changeset
|
299 |
mainattr = 'eid' |
e23536d29231
minor refactoring of absolute_url()
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1842
diff
changeset
|
300 |
path += '/eid' |
0 | 301 |
if mainattr == 'eid': |
1840
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
302 |
if use_ext_eid: |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
303 |
value = self.metainformation()['extid'] |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
304 |
else: |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
305 |
value = self.eid |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
306 |
return '%s/%s' % (path, self.req.url_quote(value)) |
0 | 307 |
|
1360
13ae1121835e
rename attribute_metadata method to attr_metadata to save a few chars
sylvain.thenault@logilab.fr
parents:
1313
diff
changeset
|
308 |
def attr_metadata(self, attr, metadata): |
1101
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
1098
diff
changeset
|
309 |
"""return a metadata for an attribute (None if unspecified)""" |
2784
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
310 |
value = getattr(self, '%s_%s' % (attr, metadata), None) |
1101
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
1098
diff
changeset
|
311 |
if value is None and metadata == 'encoding': |
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
1098
diff
changeset
|
312 |
value = self.vreg.property_value('ui.encoding') |
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
1098
diff
changeset
|
313 |
return value |
0 | 314 |
|
315 |
def printable_value(self, attr, value=_marker, attrtype=None, |
|
316 |
format='text/html', displaytime=True): |
|
317 |
"""return a displayable value (i.e. unicode string) which may contains |
|
318 |
html tags |
|
319 |
""" |
|
320 |
attr = str(attr) |
|
321 |
if value is _marker: |
|
2784
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
322 |
value = getattr(self, attr) |
0 | 323 |
if isinstance(value, basestring): |
324 |
value = value.strip() |
|
325 |
if value is None or value == '': # don't use "not", 0 is an acceptable value |
|
326 |
return u'' |
|
327 |
if attrtype is None: |
|
328 |
attrtype = self.e_schema.destination(attr) |
|
2784
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
329 |
props = self.e_schema.rproperties(attr) |
0 | 330 |
if attrtype == 'String': |
331 |
# internalinalized *and* formatted string such as schema |
|
332 |
# description... |
|
333 |
if props.get('internationalizable'): |
|
334 |
value = self.req._(value) |
|
1360
13ae1121835e
rename attribute_metadata method to attr_metadata to save a few chars
sylvain.thenault@logilab.fr
parents:
1313
diff
changeset
|
335 |
attrformat = self.attr_metadata(attr, 'format') |
0 | 336 |
if attrformat: |
337 |
return self.mtc_transform(value, attrformat, format, |
|
338 |
self.req.encoding) |
|
339 |
elif attrtype == 'Bytes': |
|
1360
13ae1121835e
rename attribute_metadata method to attr_metadata to save a few chars
sylvain.thenault@logilab.fr
parents:
1313
diff
changeset
|
340 |
attrformat = self.attr_metadata(attr, 'format') |
0 | 341 |
if attrformat: |
1360
13ae1121835e
rename attribute_metadata method to attr_metadata to save a few chars
sylvain.thenault@logilab.fr
parents:
1313
diff
changeset
|
342 |
encoding = self.attr_metadata(attr, 'encoding') |
0 | 343 |
return self.mtc_transform(value.getvalue(), attrformat, format, |
344 |
encoding) |
|
345 |
return u'' |
|
346 |
value = printable_value(self.req, attrtype, value, props, displaytime) |
|
347 |
if format == 'text/html': |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2125
diff
changeset
|
348 |
value = xml_escape(value) |
0 | 349 |
return value |
350 |
||
351 |
def mtc_transform(self, data, format, target_format, encoding, |
|
352 |
_engine=ENGINE): |
|
353 |
trdata = TransformData(data, format, encoding, appobject=self) |
|
354 |
data = _engine.convert(trdata, target_format).decode() |
|
355 |
if format == 'text/html': |
|
1474 | 356 |
data = soup2xhtml(data, self.req.encoding) |
0 | 357 |
return data |
1474 | 358 |
|
0 | 359 |
# entity cloning ########################################################## |
360 |
||
361 |
def copy_relations(self, ceid): |
|
362 |
"""copy relations of the object with the given eid on this object |
|
363 |
||
364 |
By default meta and composite relations are skipped. |
|
365 |
Overrides this if you want another behaviour |
|
366 |
""" |
|
367 |
assert self.has_eid() |
|
368 |
execute = self.req.execute |
|
369 |
for rschema in self.e_schema.subject_relations(): |
|
2784
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
370 |
if rschema.is_final() or rschema.meta: |
2778
3f6dfc312760
[entities] access persistent attributes through entity.cwdb
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2658
diff
changeset
|
371 |
continue |
2784
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
372 |
# skip already defined relations |
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
373 |
if getattr(self, rschema.type): |
2778
3f6dfc312760
[entities] access persistent attributes through entity.cwdb
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2658
diff
changeset
|
374 |
continue |
3f6dfc312760
[entities] access persistent attributes through entity.cwdb
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2658
diff
changeset
|
375 |
if rschema.type in self.skip_copy_for: |
0 | 376 |
continue |
377 |
if rschema.type == 'in_state': |
|
378 |
# if the workflow is defining an initial state (XXX AND we are |
|
379 |
# not in the managers group? not done to be more consistent) |
|
380 |
# don't try to copy in_state |
|
381 |
if execute('Any S WHERE S state_of ET, ET initial_state S,' |
|
382 |
'ET name %(etype)s', {'etype': str(self.e_schema)}): |
|
383 |
continue |
|
2784
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
384 |
# skip composite relation |
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
385 |
if self.e_schema.subjrproperty(rschema, 'composite'): |
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
386 |
continue |
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
387 |
# skip relation with card in ?1 else we either change the copied |
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
388 |
# object (inlined relation) or inserting some inconsistency |
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
389 |
if self.e_schema.subjrproperty(rschema, 'cardinality')[1] in '?1': |
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
390 |
continue |
0 | 391 |
rql = 'SET X %s V WHERE X eid %%(x)s, Y eid %%(y)s, Y %s V' % ( |
2784
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
392 |
rschema.type, rschema.type) |
0 | 393 |
execute(rql, {'x': self.eid, 'y': ceid}, ('x', 'y')) |
394 |
self.clear_related_cache(rschema.type, 'subject') |
|
395 |
for rschema in self.e_schema.object_relations(): |
|
2784
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
396 |
if rschema.meta: |
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
397 |
continue |
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
398 |
# skip already defined relations |
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
399 |
if getattr(self, 'reverse_%s' % rschema.type): |
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
400 |
continue |
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
401 |
# skip composite relation |
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
402 |
if self.e_schema.objrproperty(rschema, 'composite'): |
0 | 403 |
continue |
404 |
# skip relation with card in ?1 else we either change the copied |
|
405 |
# object (inlined relation) or inserting some inconsistency |
|
406 |
if self.e_schema.objrproperty(rschema, 'cardinality')[0] in '?1': |
|
407 |
continue |
|
408 |
rql = 'SET V %s X WHERE X eid %%(x)s, Y eid %%(y)s, V %s Y' % ( |
|
2784
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
409 |
rschema.type, rschema.type) |
0 | 410 |
execute(rql, {'x': self.eid, 'y': ceid}, ('x', 'y')) |
411 |
self.clear_related_cache(rschema.type, 'object') |
|
412 |
||
413 |
# data fetching methods ################################################### |
|
414 |
||
415 |
@cached |
|
416 |
def as_rset(self): |
|
417 |
"""returns a resultset containing `self` information""" |
|
418 |
rset = ResultSet([(self.eid,)], 'Any X WHERE X eid %(x)s', |
|
2829
054a8805da52
turn id into __id__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2827
diff
changeset
|
419 |
{'x': self.eid}, [(self.__id__,)]) |
0 | 420 |
return self.req.decorate_rset(rset) |
1474 | 421 |
|
0 | 422 |
def to_complete_relations(self): |
423 |
"""by default complete final relations to when calling .complete()""" |
|
424 |
for rschema in self.e_schema.subject_relations(): |
|
425 |
if rschema.is_final(): |
|
426 |
continue |
|
427 |
if len(rschema.objects(self.e_schema)) > 1: |
|
428 |
# ambigous relations, the querier doesn't handle |
|
429 |
# outer join correctly in this case |
|
430 |
continue |
|
431 |
if rschema.inlined: |
|
432 |
matching_groups = self.req.user.matching_groups |
|
433 |
if matching_groups(rschema.get_groups('read')) and \ |
|
434 |
all(matching_groups(es.get_groups('read')) |
|
435 |
for es in rschema.objects(self.e_schema)): |
|
436 |
yield rschema, 'subject' |
|
1474 | 437 |
|
0 | 438 |
def to_complete_attributes(self, skip_bytes=True): |
439 |
for rschema, attrschema in self.e_schema.attribute_definitions(): |
|
440 |
# skip binary data by default |
|
441 |
if skip_bytes and attrschema.type == 'Bytes': |
|
442 |
continue |
|
443 |
attr = rschema.type |
|
444 |
if attr == 'eid': |
|
445 |
continue |
|
446 |
# password retreival is blocked at the repository server level |
|
447 |
if not self.req.user.matching_groups(rschema.get_groups('read')) \ |
|
448 |
or attrschema.type == 'Password': |
|
449 |
self[attr] = None |
|
450 |
continue |
|
451 |
yield attr |
|
1474 | 452 |
|
0 | 453 |
def complete(self, attributes=None, skip_bytes=True): |
454 |
"""complete this entity by adding missing attributes (i.e. query the |
|
455 |
repository to fill the entity) |
|
456 |
||
457 |
:type skip_bytes: bool |
|
458 |
:param skip_bytes: |
|
459 |
if true, attribute of type Bytes won't be considered |
|
460 |
""" |
|
461 |
assert self.has_eid() |
|
462 |
varmaker = rqlvar_maker() |
|
463 |
V = varmaker.next() |
|
464 |
rql = ['WHERE %s eid %%(x)s' % V] |
|
465 |
selected = [] |
|
466 |
for attr in (attributes or self.to_complete_attributes(skip_bytes)): |
|
467 |
# if attribute already in entity, nothing to do |
|
468 |
if self.has_key(attr): |
|
469 |
continue |
|
470 |
# case where attribute must be completed, but is not yet in entity |
|
471 |
var = varmaker.next() |
|
472 |
rql.append('%s %s %s' % (V, attr, var)) |
|
473 |
selected.append((attr, var)) |
|
474 |
# +1 since this doen't include the main variable |
|
475 |
lastattr = len(selected) + 1 |
|
476 |
if attributes is None: |
|
477 |
# fetch additional relations (restricted to 0..1 relations) |
|
478 |
for rschema, role in self.to_complete_relations(): |
|
479 |
rtype = rschema.type |
|
480 |
if self.relation_cached(rtype, role): |
|
481 |
continue |
|
482 |
var = varmaker.next() |
|
483 |
if role == 'subject': |
|
484 |
targettype = rschema.objects(self.e_schema)[0] |
|
485 |
card = rschema.rproperty(self.e_schema, targettype, |
|
486 |
'cardinality')[0] |
|
487 |
if card == '1': |
|
488 |
rql.append('%s %s %s' % (V, rtype, var)) |
|
489 |
else: # '?" |
|
490 |
rql.append('%s %s %s?' % (V, rtype, var)) |
|
491 |
else: |
|
492 |
targettype = rschema.subjects(self.e_schema)[1] |
|
493 |
card = rschema.rproperty(self.e_schema, targettype, |
|
494 |
'cardinality')[1] |
|
495 |
if card == '1': |
|
496 |
rql.append('%s %s %s' % (var, rtype, V)) |
|
497 |
else: # '?" |
|
498 |
rql.append('%s? %s %s' % (var, rtype, V)) |
|
499 |
assert card in '1?', '%s %s %s %s' % (self.e_schema, rtype, |
|
500 |
role, card) |
|
501 |
selected.append(((rtype, role), var)) |
|
502 |
if selected: |
|
503 |
# select V, we need it as the left most selected variable |
|
504 |
# if some outer join are included to fetch inlined relations |
|
505 |
rql = 'Any %s,%s %s' % (V, ','.join(var for attr, var in selected), |
|
506 |
','.join(rql)) |
|
507 |
execute = getattr(self.req, 'unsafe_execute', self.req.execute) |
|
508 |
rset = execute(rql, {'x': self.eid}, 'x', build_descr=False)[0] |
|
509 |
# handle attributes |
|
510 |
for i in xrange(1, lastattr): |
|
511 |
self[str(selected[i-1][0])] = rset[i] |
|
512 |
# handle relations |
|
513 |
for i in xrange(lastattr, len(rset)): |
|
514 |
rtype, x = selected[i-1][0] |
|
515 |
value = rset[i] |
|
516 |
if value is None: |
|
517 |
rrset = ResultSet([], rql, {'x': self.eid}) |
|
518 |
self.req.decorate_rset(rrset) |
|
519 |
else: |
|
520 |
rrset = self.req.eid_rset(value) |
|
521 |
self.set_related_cache(rtype, x, rrset) |
|
1474 | 522 |
|
0 | 523 |
def get_value(self, name): |
524 |
"""get value for the attribute relation <name>, query the repository |
|
525 |
to get the value if necessary. |
|
526 |
||
527 |
:type name: str |
|
528 |
:param name: name of the attribute to get |
|
529 |
""" |
|
530 |
try: |
|
531 |
value = self[name] |
|
532 |
except KeyError: |
|
533 |
if not self.is_saved(): |
|
534 |
return None |
|
535 |
rql = "Any A WHERE X eid %%(x)s, X %s A" % name |
|
2647
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2613
diff
changeset
|
536 |
# XXX should we really use unsafe_execute here? I think so (syt), |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2613
diff
changeset
|
537 |
# see #344874 |
0 | 538 |
execute = getattr(self.req, 'unsafe_execute', self.req.execute) |
539 |
try: |
|
540 |
rset = execute(rql, {'x': self.eid}, 'x') |
|
541 |
except Unauthorized: |
|
542 |
self[name] = value = None |
|
543 |
else: |
|
544 |
assert rset.rowcount <= 1, (self, rql, rset.rowcount) |
|
545 |
try: |
|
546 |
self[name] = value = rset.rows[0][0] |
|
547 |
except IndexError: |
|
548 |
# probably a multisource error |
|
549 |
self.critical("can't get value for attribute %s of entity with eid %s", |
|
550 |
name, self.eid) |
|
551 |
if self.e_schema.destination(name) == 'String': |
|
2320 | 552 |
# XXX (syt) imo emtpy string is better |
0 | 553 |
self[name] = value = self.req._('unaccessible') |
554 |
else: |
|
555 |
self[name] = value = None |
|
556 |
return value |
|
557 |
||
558 |
def related(self, rtype, role='subject', limit=None, entities=False): |
|
559 |
"""returns a resultset of related entities |
|
1474 | 560 |
|
0 | 561 |
:param role: is the role played by 'self' in the relation ('subject' or 'object') |
562 |
:param limit: resultset's maximum size |
|
563 |
:param entities: if True, the entites are returned; if False, a result set is returned |
|
564 |
""" |
|
565 |
try: |
|
566 |
return self.related_cache(rtype, role, entities, limit) |
|
567 |
except KeyError: |
|
568 |
pass |
|
569 |
assert self.has_eid() |
|
570 |
rql = self.related_rql(rtype, role) |
|
2647
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2613
diff
changeset
|
571 |
# XXX should we really use unsafe_execute here? I think so (syt), |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2613
diff
changeset
|
572 |
# see #344874 |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2613
diff
changeset
|
573 |
execute = getattr(self.req, 'unsafe_execute', self.req.execute) |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2613
diff
changeset
|
574 |
rset = execute(rql, {'x': self.eid}, 'x') |
0 | 575 |
self.set_related_cache(rtype, role, rset) |
576 |
return self.related(rtype, role, limit, entities) |
|
577 |
||
578 |
def related_rql(self, rtype, role='subject'): |
|
2818
326375561412
propagate some api changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2807
diff
changeset
|
579 |
rschema = self.req.vreg.schema[rtype] |
0 | 580 |
if role == 'subject': |
581 |
targettypes = rschema.objects(self.e_schema) |
|
582 |
restriction = 'E eid %%(x)s, E %s X' % rtype |
|
583 |
card = greater_card(rschema, (self.e_schema,), targettypes, 0) |
|
584 |
else: |
|
585 |
targettypes = rschema.subjects(self.e_schema) |
|
586 |
restriction = 'E eid %%(x)s, X %s E' % rtype |
|
587 |
card = greater_card(rschema, targettypes, (self.e_schema,), 1) |
|
588 |
if len(targettypes) > 1: |
|
413
a7366dd3c33c
fix bug in entity.related_rql(): fetch_attr list / fetchorder weren't computed correctly
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
397
diff
changeset
|
589 |
fetchattrs_list = [] |
0 | 590 |
for ttype in targettypes: |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
591 |
etypecls = self.vreg['etypes'].etype_class(ttype) |
413
a7366dd3c33c
fix bug in entity.related_rql(): fetch_attr list / fetchorder weren't computed correctly
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
397
diff
changeset
|
592 |
fetchattrs_list.append(set(etypecls.fetch_attrs)) |
a7366dd3c33c
fix bug in entity.related_rql(): fetch_attr list / fetchorder weren't computed correctly
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
397
diff
changeset
|
593 |
fetchattrs = reduce(set.intersection, fetchattrs_list) |
0 | 594 |
rql = etypecls.fetch_rql(self.req.user, [restriction], fetchattrs, |
595 |
settype=False) |
|
596 |
else: |
|
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
597 |
etypecls = self.vreg['etypes'].etype_class(targettypes[0]) |
0 | 598 |
rql = etypecls.fetch_rql(self.req.user, [restriction], settype=False) |
599 |
# optimisation: remove ORDERBY if cardinality is 1 or ? (though |
|
600 |
# greater_card return 1 for those both cases) |
|
601 |
if card == '1': |
|
602 |
if ' ORDERBY ' in rql: |
|
603 |
rql = '%s WHERE %s' % (rql.split(' ORDERBY ', 1)[0], |
|
604 |
rql.split(' WHERE ', 1)[1]) |
|
605 |
elif not ' ORDERBY ' in rql: |
|
606 |
args = tuple(rql.split(' WHERE ', 1)) |
|
607 |
rql = '%s ORDERBY Z DESC WHERE X modification_date Z, %s' % args |
|
608 |
return rql |
|
1474 | 609 |
|
0 | 610 |
# generic vocabulary methods ############################################## |
611 |
||
612 |
def unrelated_rql(self, rtype, targettype, role, ordermethod=None, |
|
613 |
vocabconstraints=True): |
|
614 |
"""build a rql to fetch `targettype` entities unrelated to this entity |
|
615 |
using (rtype, role) relation |
|
616 |
""" |
|
617 |
ordermethod = ordermethod or 'fetch_unrelated_order' |
|
618 |
if isinstance(rtype, basestring): |
|
2818
326375561412
propagate some api changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2807
diff
changeset
|
619 |
rtype = self.req.vreg.schema.rschema(rtype) |
0 | 620 |
if role == 'subject': |
621 |
evar, searchedvar = 'S', 'O' |
|
622 |
subjtype, objtype = self.e_schema, targettype |
|
623 |
else: |
|
624 |
searchedvar, evar = 'S', 'O' |
|
625 |
objtype, subjtype = self.e_schema, targettype |
|
626 |
if self.has_eid(): |
|
627 |
restriction = ['NOT S %s O' % rtype, '%s eid %%(x)s' % evar] |
|
628 |
else: |
|
629 |
restriction = [] |
|
630 |
constraints = rtype.rproperty(subjtype, objtype, 'constraints') |
|
631 |
if vocabconstraints: |
|
632 |
# RQLConstraint is a subclass for RQLVocabularyConstraint, so they |
|
633 |
# will be included as well |
|
634 |
restriction += [cstr.restriction for cstr in constraints |
|
635 |
if isinstance(cstr, RQLVocabularyConstraint)] |
|
636 |
else: |
|
637 |
restriction += [cstr.restriction for cstr in constraints |
|
638 |
if isinstance(cstr, RQLConstraint)] |
|
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
639 |
etypecls = self.vreg['etypes'].etype_class(targettype) |
0 | 640 |
rql = etypecls.fetch_rql(self.req.user, restriction, |
641 |
mainvar=searchedvar, ordermethod=ordermethod) |
|
642 |
# ensure we have an order defined |
|
643 |
if not ' ORDERBY ' in rql: |
|
644 |
before, after = rql.split(' WHERE ', 1) |
|
645 |
rql = '%s ORDERBY %s WHERE %s' % (before, searchedvar, after) |
|
646 |
return rql |
|
1474 | 647 |
|
0 | 648 |
def unrelated(self, rtype, targettype, role='subject', limit=None, |
649 |
ordermethod=None): |
|
650 |
"""return a result set of target type objects that may be related |
|
651 |
by a given relation, with self as subject or object |
|
652 |
""" |
|
653 |
rql = self.unrelated_rql(rtype, targettype, role, ordermethod) |
|
654 |
if limit is not None: |
|
655 |
before, after = rql.split(' WHERE ', 1) |
|
656 |
rql = '%s LIMIT %s WHERE %s' % (before, limit, after) |
|
657 |
if self.has_eid(): |
|
658 |
return self.req.execute(rql, {'x': self.eid}) |
|
659 |
return self.req.execute(rql) |
|
1474 | 660 |
|
0 | 661 |
# relations cache handling ################################################ |
1474 | 662 |
|
0 | 663 |
def relation_cached(self, rtype, role): |
664 |
"""return true if the given relation is already cached on the instance |
|
665 |
""" |
|
2647
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2613
diff
changeset
|
666 |
return self._related_cache.get('%s_%s' % (rtype, role)) |
1474 | 667 |
|
0 | 668 |
def related_cache(self, rtype, role, entities=True, limit=None): |
669 |
"""return values for the given relation if it's cached on the instance, |
|
670 |
else raise `KeyError` |
|
671 |
""" |
|
672 |
res = self._related_cache['%s_%s' % (rtype, role)][entities] |
|
673 |
if limit: |
|
674 |
if entities: |
|
675 |
res = res[:limit] |
|
676 |
else: |
|
677 |
res = res.limit(limit) |
|
678 |
return res |
|
1474 | 679 |
|
0 | 680 |
def set_related_cache(self, rtype, role, rset, col=0): |
681 |
"""set cached values for the given relation""" |
|
682 |
if rset: |
|
683 |
related = list(rset.entities(col)) |
|
2818
326375561412
propagate some api changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2807
diff
changeset
|
684 |
rschema = self.req.vreg.schema.rschema(rtype) |
0 | 685 |
if role == 'subject': |
686 |
rcard = rschema.rproperty(self.e_schema, related[0].e_schema, |
|
687 |
'cardinality')[1] |
|
688 |
target = 'object' |
|
689 |
else: |
|
690 |
rcard = rschema.rproperty(related[0].e_schema, self.e_schema, |
|
691 |
'cardinality')[0] |
|
692 |
target = 'subject' |
|
693 |
if rcard in '?1': |
|
694 |
for rentity in related: |
|
695 |
rentity._related_cache['%s_%s' % (rtype, target)] = (self.as_rset(), [self]) |
|
696 |
else: |
|
697 |
related = [] |
|
698 |
self._related_cache['%s_%s' % (rtype, role)] = (rset, related) |
|
1474 | 699 |
|
0 | 700 |
def clear_related_cache(self, rtype=None, role=None): |
701 |
"""clear cached values for the given relation or the entire cache if |
|
702 |
no relation is given |
|
703 |
""" |
|
704 |
if rtype is None: |
|
705 |
self._related_cache = {} |
|
706 |
else: |
|
707 |
assert role |
|
708 |
self._related_cache.pop('%s_%s' % (rtype, role), None) |
|
1474 | 709 |
|
0 | 710 |
# raw edition utilities ################################################### |
1474 | 711 |
|
0 | 712 |
def set_attributes(self, **kwargs): |
713 |
assert kwargs |
|
714 |
relations = [] |
|
715 |
for key in kwargs: |
|
716 |
relations.append('X %s %%(%s)s' % (key, key)) |
|
397
cf577e26f924
don't introduce a spurious 'x' key in the entity-as dict
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
237
diff
changeset
|
717 |
# update current local object |
cf577e26f924
don't introduce a spurious 'x' key in the entity-as dict
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
237
diff
changeset
|
718 |
self.update(kwargs) |
cf577e26f924
don't introduce a spurious 'x' key in the entity-as dict
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
237
diff
changeset
|
719 |
# and now update the database |
0 | 720 |
kwargs['x'] = self.eid |
721 |
self.req.execute('SET %s WHERE X eid %%(x)s' % ','.join(relations), |
|
722 |
kwargs, 'x') |
|
1474 | 723 |
|
0 | 724 |
def delete(self): |
725 |
assert self.has_eid(), self.eid |
|
726 |
self.req.execute('DELETE %s X WHERE X eid %%(x)s' % self.e_schema, |
|
727 |
{'x': self.eid}) |
|
1474 | 728 |
|
0 | 729 |
# server side utilities ################################################### |
1474 | 730 |
|
0 | 731 |
def set_defaults(self): |
732 |
"""set default values according to the schema""" |
|
733 |
self._default_set = set() |
|
734 |
for attr, value in self.e_schema.defaults(): |
|
735 |
if not self.has_key(attr): |
|
736 |
self[str(attr)] = value |
|
737 |
self._default_set.add(attr) |
|
738 |
||
739 |
def check(self, creation=False): |
|
740 |
"""check this entity against its schema. Only final relation |
|
741 |
are checked here, constraint on actual relations are checked in hooks |
|
742 |
""" |
|
743 |
# necessary since eid is handled specifically and yams require it to be |
|
744 |
# in the dictionary |
|
745 |
if self.req is None: |
|
746 |
_ = unicode |
|
747 |
else: |
|
748 |
_ = self.req._ |
|
749 |
self.e_schema.check(self, creation=creation, _=_) |
|
750 |
||
751 |
def fti_containers(self, _done=None): |
|
752 |
if _done is None: |
|
753 |
_done = set() |
|
754 |
_done.add(self.eid) |
|
755 |
containers = tuple(self.e_schema.fulltext_containers()) |
|
756 |
if containers: |
|
476
62968fa8845c
yield self if no ft container found
sylvain.thenault@logilab.fr
parents:
475
diff
changeset
|
757 |
yielded = False |
0 | 758 |
for rschema, target in containers: |
759 |
if target == 'object': |
|
2784
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
760 |
targets = getattr(self, rschema.type) |
0 | 761 |
else: |
2784
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
762 |
targets = getattr(self, 'reverse_%s' % rschema) |
0 | 763 |
for entity in targets: |
764 |
if entity.eid in _done: |
|
765 |
continue |
|
766 |
for container in entity.fti_containers(_done): |
|
767 |
yield container |
|
476
62968fa8845c
yield self if no ft container found
sylvain.thenault@logilab.fr
parents:
475
diff
changeset
|
768 |
yielded = True |
62968fa8845c
yield self if no ft container found
sylvain.thenault@logilab.fr
parents:
475
diff
changeset
|
769 |
if not yielded: |
62968fa8845c
yield self if no ft container found
sylvain.thenault@logilab.fr
parents:
475
diff
changeset
|
770 |
yield self |
0 | 771 |
else: |
772 |
yield self |
|
1474 | 773 |
|
0 | 774 |
def get_words(self): |
775 |
"""used by the full text indexer to get words to index |
|
776 |
||
777 |
this method should only be used on the repository side since it depends |
|
778 |
on the indexer package |
|
1474 | 779 |
|
0 | 780 |
:rtype: list |
781 |
:return: the list of indexable word of this entity |
|
782 |
""" |
|
783 |
from indexer.query_objects import tokenize |
|
784 |
words = [] |
|
785 |
for rschema in self.e_schema.indexable_attributes(): |
|
786 |
try: |
|
787 |
value = self.printable_value(rschema, format='text/plain') |
|
1132 | 788 |
except TransformError: |
0 | 789 |
continue |
790 |
except: |
|
791 |
self.exception("can't add value of %s to text index for entity %s", |
|
792 |
rschema, self.eid) |
|
793 |
continue |
|
794 |
if value: |
|
795 |
words += tokenize(value) |
|
1474 | 796 |
|
0 | 797 |
for rschema, role in self.e_schema.fulltext_relations(): |
2784
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
798 |
if role == 'subject': |
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
799 |
for entity in getattr(self, rschema.type): |
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
800 |
words += entity.get_words() |
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
801 |
else: # if role == 'object': |
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
802 |
for entity in getattr(self, 'reverse_%s' % rschema.type): |
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
803 |
words += entity.get_words() |
0 | 804 |
return words |
805 |
||
806 |
||
807 |
# attribute and relation descriptors ########################################## |
|
808 |
||
809 |
class Attribute(object): |
|
810 |
"""descriptor that controls schema attribute access""" |
|
811 |
||
2784
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
812 |
def __init__(self, attrname): |
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
813 |
assert attrname != 'eid' |
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
814 |
self._attrname = attrname |
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
815 |
|
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
816 |
def __get__(self, eobj, eclass): |
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
817 |
if eobj is None: |
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
818 |
return self |
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
819 |
return eobj.get_value(self._attrname) |
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
820 |
|
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
821 |
def __set__(self, eobj, value): |
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
822 |
eobj[self._attrname] = value |
2778
3f6dfc312760
[entities] access persistent attributes through entity.cwdb
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2658
diff
changeset
|
823 |
|
2784
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
824 |
|
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
825 |
class Relation(object): |
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
826 |
"""descriptor that controls schema relation access""" |
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
827 |
_role = None # for pylint |
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
828 |
|
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
829 |
def __init__(self, rschema): |
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
830 |
self._rschema = rschema |
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
831 |
self._rtype = rschema.type |
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
832 |
|
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
833 |
def __get__(self, eobj, eclass): |
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
834 |
if eobj is None: |
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
835 |
raise AttributeError('%s cannot be only be accessed from instances' |
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
836 |
% self._rtype) |
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
837 |
return eobj.related(self._rtype, self._role, entities=True) |
2778
3f6dfc312760
[entities] access persistent attributes through entity.cwdb
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2658
diff
changeset
|
838 |
|
3f6dfc312760
[entities] access persistent attributes through entity.cwdb
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2658
diff
changeset
|
839 |
def __set__(self, eobj, value): |
3f6dfc312760
[entities] access persistent attributes through entity.cwdb
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2658
diff
changeset
|
840 |
raise NotImplementedError |
3f6dfc312760
[entities] access persistent attributes through entity.cwdb
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2658
diff
changeset
|
841 |
|
3f6dfc312760
[entities] access persistent attributes through entity.cwdb
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2658
diff
changeset
|
842 |
|
2784
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
843 |
class SubjectRelation(Relation): |
2778
3f6dfc312760
[entities] access persistent attributes through entity.cwdb
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2658
diff
changeset
|
844 |
"""descriptor that controls schema relation access""" |
2784
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
845 |
_role = 'subject' |
0 | 846 |
|
2784
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
847 |
class ObjectRelation(Relation): |
0 | 848 |
"""descriptor that controls schema relation access""" |
2784
f395115070c1
Backed out changeset 3f6dfc312760
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2778
diff
changeset
|
849 |
_role = 'object' |
0 | 850 |
|
851 |
from logging import getLogger |
|
852 |
from cubicweb import set_log_methods |
|
853 |
set_log_methods(Entity, getLogger('cubicweb.entity')) |