author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 26 Jan 2010 13:36:55 +0100 | |
changeset 4366 | d51f28ba9399 |
parent 4363 | 5c18d82042fb |
child 4436 | 294e084f1263 |
permissions | -rw-r--r-- |
0 | 1 |
"""Base class for entity objects manipulated in clients |
2 |
||
3 |
:organization: Logilab |
|
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3961
diff
changeset
|
4 |
:copyright: 2001-2010 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 |
|
2613
5e19c2bb370e
R [all] logilab.common 0.44 provides only deprecated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2421
diff
changeset
|
15 |
from logilab.common.deprecation import deprecated |
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2125
diff
changeset
|
16 |
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
|
17 |
|
3241
1a6f7a0e7dbd
unrelated_rql now considers relation's add perm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3212
diff
changeset
|
18 |
from rql import parse |
0 | 19 |
from rql.utils import rqlvar_maker |
20 |
||
3664
af7ca3597b8d
use typed_eid
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3626
diff
changeset
|
21 |
from cubicweb import Unauthorized, typed_eid |
0 | 22 |
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
|
23 |
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
|
24 |
from cubicweb.appobject import AppObject |
2827
d1a89d165045
remove 3.2 bw compat code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2824
diff
changeset
|
25 |
from cubicweb.schema import RQLVocabularyConstraint, RQLConstraint |
3241
1a6f7a0e7dbd
unrelated_rql now considers relation's add perm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3212
diff
changeset
|
26 |
from cubicweb.rqlrewrite import RQLRewriter |
709 | 27 |
|
4023
eae23c40627a
drop common subpackage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3998
diff
changeset
|
28 |
from cubicweb.uilib import printable_value, soup2xhtml |
eae23c40627a
drop common subpackage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3998
diff
changeset
|
29 |
from cubicweb.mixins import MI_REL_TRIGGERS |
eae23c40627a
drop common subpackage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3998
diff
changeset
|
30 |
from cubicweb.mttransforms import ENGINE |
0 | 31 |
|
32 |
_marker = object() |
|
33 |
||
34 |
def greater_card(rschema, subjtypes, objtypes, index): |
|
35 |
for subjtype in subjtypes: |
|
36 |
for objtype in objtypes: |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
37 |
card = rschema.rdef(subjtype, objtype).cardinality[index] |
0 | 38 |
if card in '+*': |
39 |
return card |
|
40 |
return '1' |
|
41 |
||
42 |
||
2656
a93ae0f6c0ad
R [base classes] only AppObject remaning, no more AppRsetObject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
43 |
class Entity(AppObject, dict): |
0 | 44 |
"""an entity instance has e_schema automagically set on |
45 |
the class and instances has access to their issuing cursor. |
|
1474 | 46 |
|
0 | 47 |
A property is set for each attribute and relation on each entity's type |
48 |
class. Becare that among attributes, 'eid' is *NEITHER* stored in the |
|
49 |
dict containment (which acts as a cache for other attributes dynamically |
|
50 |
fetched) |
|
51 |
||
52 |
:type e_schema: `cubicweb.schema.EntitySchema` |
|
53 |
:ivar e_schema: the entity's schema |
|
54 |
||
55 |
:type rest_var: str |
|
56 |
:cvar rest_var: indicates which attribute should be used to build REST urls |
|
57 |
If None is specified, the first non-meta attribute will |
|
58 |
be used |
|
1474 | 59 |
|
0 | 60 |
:type skip_copy_for: list |
61 |
:cvar skip_copy_for: a list of relations that should be skipped when copying |
|
62 |
this kind of entity. Note that some relations such |
|
63 |
as composite relations or relations that have '?1' as object |
|
3626
017869a514c3
[doc] updated docstrings
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
3552
diff
changeset
|
64 |
cardinality are always skipped. |
0 | 65 |
""" |
66 |
__registry__ = 'etypes' |
|
717 | 67 |
__select__ = yes() |
1264
fe2934a7df7f
cleanup, avoid spurious warning
sylvain.thenault@logilab.fr
parents:
1177
diff
changeset
|
68 |
|
1474 | 69 |
# class attributes that must be set in class definition |
0 | 70 |
rest_attr = None |
1138
22f634977c95
make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents:
1132
diff
changeset
|
71 |
fetch_attrs = None |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2877
diff
changeset
|
72 |
skip_copy_for = ('in_state',) |
1264
fe2934a7df7f
cleanup, avoid spurious warning
sylvain.thenault@logilab.fr
parents:
1177
diff
changeset
|
73 |
# class attributes set automatically at registration time |
fe2934a7df7f
cleanup, avoid spurious warning
sylvain.thenault@logilab.fr
parents:
1177
diff
changeset
|
74 |
e_schema = None |
1474 | 75 |
|
0 | 76 |
@classmethod |
2807
696ff03f9a58
__initialize__ needs the schema as argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
77 |
def __initialize__(cls, schema): |
0 | 78 |
"""initialize a specific entity class by adding descriptors to access |
79 |
entity type's attributes and relations |
|
80 |
""" |
|
3376
f5c69485381f
[appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3369
diff
changeset
|
81 |
etype = cls.__regid__ |
0 | 82 |
assert etype != 'Any', etype |
2807
696ff03f9a58
__initialize__ needs the schema as argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
83 |
cls.e_schema = eschema = schema.eschema(etype) |
0 | 84 |
for rschema, _ in eschema.attribute_definitions(): |
85 |
if rschema.type == 'eid': |
|
86 |
continue |
|
87 |
setattr(cls, rschema.type, Attribute(rschema.type)) |
|
88 |
mixins = [] |
|
2941
196493bd099c
[entity] use role instead of x
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2931
diff
changeset
|
89 |
for rschema, _, role in eschema.relation_definitions(): |
196493bd099c
[entity] use role instead of x
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2931
diff
changeset
|
90 |
if (rschema, role) in MI_REL_TRIGGERS: |
196493bd099c
[entity] use role instead of x
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2931
diff
changeset
|
91 |
mixin = MI_REL_TRIGGERS[(rschema, role)] |
0 | 92 |
if not (issubclass(cls, mixin) or mixin in mixins): # already mixed ? |
93 |
mixins.append(mixin) |
|
94 |
for iface in getattr(mixin, '__implements__', ()): |
|
95 |
if not interface.implements(cls, iface): |
|
96 |
interface.extend(cls, iface) |
|
2941
196493bd099c
[entity] use role instead of x
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2931
diff
changeset
|
97 |
if role == 'subject': |
0 | 98 |
setattr(cls, rschema.type, SubjectRelation(rschema)) |
99 |
else: |
|
100 |
attr = 'reverse_%s' % rschema.type |
|
101 |
setattr(cls, attr, ObjectRelation(rschema)) |
|
102 |
if mixins: |
|
3919
f91bd15f427c
fix nasty pluggable mixins bug, where mixins take precedence other user class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
103 |
# see etype class instantation in cwvreg.ETypeRegistry.etype_class method: |
f91bd15f427c
fix nasty pluggable mixins bug, where mixins take precedence other user class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
104 |
# due to class dumping, cls is the generated top level class with actual |
f91bd15f427c
fix nasty pluggable mixins bug, where mixins take precedence other user class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
105 |
# user class as (only) parent. Since we want to be able to override mixins |
f91bd15f427c
fix nasty pluggable mixins bug, where mixins take precedence other user class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
106 |
# method from this user class, we have to take care to insert mixins after that |
f91bd15f427c
fix nasty pluggable mixins bug, where mixins take precedence other user class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
107 |
# class |
f91bd15f427c
fix nasty pluggable mixins bug, where mixins take precedence other user class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
108 |
# |
f91bd15f427c
fix nasty pluggable mixins bug, where mixins take precedence other user class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
109 |
# note that we don't plug mixins as user class parent since it causes pb |
f91bd15f427c
fix nasty pluggable mixins bug, where mixins take precedence other user class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
110 |
# with some cases of entity classes inheritance. |
f91bd15f427c
fix nasty pluggable mixins bug, where mixins take precedence other user class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
111 |
mixins.insert(0, cls.__bases__[0]) |
f91bd15f427c
fix nasty pluggable mixins bug, where mixins take precedence other user class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
112 |
mixins += cls.__bases__[1:] |
f91bd15f427c
fix nasty pluggable mixins bug, where mixins take precedence other user class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
113 |
cls.__bases__ = tuple(mixins) |
f91bd15f427c
fix nasty pluggable mixins bug, where mixins take precedence other user class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
114 |
cls.info('plugged %s mixins on %s', mixins, cls) |
1474 | 115 |
|
0 | 116 |
@classmethod |
117 |
def fetch_rql(cls, user, restriction=None, fetchattrs=None, mainvar='X', |
|
118 |
settype=True, ordermethod='fetch_order'): |
|
119 |
"""return a rql to fetch all entities of the class type""" |
|
120 |
restrictions = restriction or [] |
|
121 |
if settype: |
|
3376
f5c69485381f
[appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3369
diff
changeset
|
122 |
restrictions.append('%s is %s' % (mainvar, cls.__regid__)) |
0 | 123 |
if fetchattrs is None: |
124 |
fetchattrs = cls.fetch_attrs |
|
125 |
selection = [mainvar] |
|
126 |
orderby = [] |
|
127 |
# start from 26 to avoid possible conflicts with X |
|
128 |
varmaker = rqlvar_maker(index=26) |
|
129 |
cls._fetch_restrictions(mainvar, varmaker, fetchattrs, selection, |
|
130 |
orderby, restrictions, user, ordermethod) |
|
131 |
rql = 'Any %s' % ','.join(selection) |
|
132 |
if orderby: |
|
133 |
rql += ' ORDERBY %s' % ','.join(orderby) |
|
134 |
rql += ' WHERE %s' % ', '.join(restrictions) |
|
135 |
return rql |
|
1474 | 136 |
|
0 | 137 |
@classmethod |
138 |
def _fetch_restrictions(cls, mainvar, varmaker, fetchattrs, |
|
139 |
selection, orderby, restrictions, user, |
|
140 |
ordermethod='fetch_order', visited=None): |
|
141 |
eschema = cls.e_schema |
|
142 |
if visited is None: |
|
143 |
visited = set((eschema.type,)) |
|
144 |
elif eschema.type in visited: |
|
145 |
# avoid infinite recursion |
|
146 |
return |
|
147 |
else: |
|
148 |
visited.add(eschema.type) |
|
149 |
_fetchattrs = [] |
|
150 |
for attr in fetchattrs: |
|
151 |
try: |
|
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3683
diff
changeset
|
152 |
rschema = eschema.subjrels[attr] |
0 | 153 |
except KeyError: |
154 |
cls.warning('skipping fetch_attr %s defined in %s (not found in schema)', |
|
3376
f5c69485381f
[appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3369
diff
changeset
|
155 |
attr, cls.__regid__) |
0 | 156 |
continue |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
157 |
rdef = eschema.rdef(attr) |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
158 |
if not user.matching_groups(rdef.get_groups('read')): |
0 | 159 |
continue |
160 |
var = varmaker.next() |
|
161 |
selection.append(var) |
|
162 |
restriction = '%s %s %s' % (mainvar, attr, var) |
|
163 |
restrictions.append(restriction) |
|
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3683
diff
changeset
|
164 |
if not rschema.final: |
0 | 165 |
# XXX this does not handle several destination types |
166 |
desttype = rschema.objects(eschema.type)[0] |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
167 |
card = rdef.cardinality[0] |
0 | 168 |
if card not in '?1': |
2928
edfdb69df6e8
oops, not self but cls here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2877
diff
changeset
|
169 |
cls.warning('bad relation %s specified in fetch attrs for %s', |
edfdb69df6e8
oops, not self but cls here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2877
diff
changeset
|
170 |
attr, cls) |
0 | 171 |
selection.pop() |
172 |
restrictions.pop() |
|
173 |
continue |
|
2871
83c5499e1436
[entity] fix fetch_rql w/ case where it's called while entity is not 'complete' (eg time where it's being added but have not yet all mandatory relations set)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2658
diff
changeset
|
174 |
# XXX we need outer join in case the relation is not mandatory |
83c5499e1436
[entity] fix fetch_rql w/ case where it's called while entity is not 'complete' (eg time where it's being added but have not yet all mandatory relations set)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2658
diff
changeset
|
175 |
# (card == '?') *or if the entity is being added*, since in |
83c5499e1436
[entity] fix fetch_rql w/ case where it's called while entity is not 'complete' (eg time where it's being added but have not yet all mandatory relations set)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2658
diff
changeset
|
176 |
# that case the relation may still be missing. As we miss this |
83c5499e1436
[entity] fix fetch_rql w/ case where it's called while entity is not 'complete' (eg time where it's being added but have not yet all mandatory relations set)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2658
diff
changeset
|
177 |
# later information here, systematically add it. |
83c5499e1436
[entity] fix fetch_rql w/ case where it's called while entity is not 'complete' (eg time where it's being added but have not yet all mandatory relations set)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2658
diff
changeset
|
178 |
restrictions[-1] += '?' |
4049
5838a4b0766d
3.5 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4023
diff
changeset
|
179 |
# XXX user._cw.vreg iiiirk |
3378
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
180 |
destcls = user._cw.vreg['etypes'].etype_class(desttype) |
0 | 181 |
destcls._fetch_restrictions(var, varmaker, destcls.fetch_attrs, |
182 |
selection, orderby, restrictions, |
|
183 |
user, ordermethod, visited=visited) |
|
184 |
orderterm = getattr(cls, ordermethod)(attr, var) |
|
185 |
if orderterm: |
|
186 |
orderby.append(orderterm) |
|
187 |
return selection, orderby, restrictions |
|
188 |
||
1471
c889c3bcf5ec
new parent_classes method (cached)
sylvain.thenault@logilab.fr
parents:
1435
diff
changeset
|
189 |
@classmethod |
c889c3bcf5ec
new parent_classes method (cached)
sylvain.thenault@logilab.fr
parents:
1435
diff
changeset
|
190 |
@cached |
1840
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
191 |
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
|
192 |
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
|
193 |
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
|
194 |
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
|
195 |
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
|
196 |
else: |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
197 |
for rschema in cls.e_schema.subject_relations(): |
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3683
diff
changeset
|
198 |
if rschema.final and rschema != 'eid' and cls.e_schema.has_unique_values(rschema): |
1840
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
199 |
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
|
200 |
needcheck = False |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
201 |
break |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
202 |
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
|
203 |
needcheck = False |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
204 |
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
|
205 |
|
1435
6cd6172718bb
allow to instantiate an entity without rset
sylvain.thenault@logilab.fr
parents:
1363
diff
changeset
|
206 |
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
|
207 |
AppObject.__init__(self, req, rset=rset, row=row, col=col) |
0 | 208 |
dict.__init__(self) |
209 |
self._related_cache = {} |
|
210 |
if rset is not None: |
|
211 |
self.eid = rset[row][col] |
|
212 |
else: |
|
213 |
self.eid = None |
|
214 |
self._is_saved = True |
|
1474 | 215 |
|
0 | 216 |
def __repr__(self): |
217 |
return '<Entity %s %s %s at %s>' % ( |
|
218 |
self.e_schema, self.eid, self.keys(), id(self)) |
|
219 |
||
220 |
def __nonzero__(self): |
|
221 |
return True |
|
222 |
||
223 |
def __hash__(self): |
|
224 |
return id(self) |
|
225 |
||
3047
ba86b1bdbcab
fix __cmp__ arguments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3000
diff
changeset
|
226 |
def __cmp__(self, other): |
3000
4e76477949d5
B [entities] #343385 comparing entities should raise NotImplementedError
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2928
diff
changeset
|
227 |
raise NotImplementedError('comparison not implemented for %s' % self.__class__) |
4e76477949d5
B [entities] #343385 comparing entities should raise NotImplementedError
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2928
diff
changeset
|
228 |
|
0 | 229 |
def pre_add_hook(self): |
230 |
"""hook called by the repository before doing anything to add the entity |
|
231 |
(before_add entity hooks have not been called yet). This give the |
|
232 |
occasion to do weird stuff such as autocast (File -> Image for instance). |
|
1474 | 233 |
|
0 | 234 |
This method must return the actual entity to be added. |
235 |
""" |
|
236 |
return self |
|
1474 | 237 |
|
0 | 238 |
def set_eid(self, eid): |
239 |
self.eid = self['eid'] = eid |
|
240 |
||
241 |
def has_eid(self): |
|
242 |
"""return True if the entity has an attributed eid (False |
|
243 |
meaning that the entity has to be created |
|
244 |
""" |
|
245 |
try: |
|
3664
af7ca3597b8d
use typed_eid
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3626
diff
changeset
|
246 |
typed_eid(self.eid) |
0 | 247 |
return True |
248 |
except (ValueError, TypeError): |
|
249 |
return False |
|
250 |
||
251 |
def is_saved(self): |
|
252 |
"""during entity creation, there is some time during which the entity |
|
253 |
has an eid attributed though it's not saved (eg during before_add_entity |
|
254 |
hooks). You can use this method to ensure the entity has an eid *and* is |
|
255 |
saved in its source. |
|
256 |
""" |
|
257 |
return self.has_eid() and self._is_saved |
|
1474 | 258 |
|
0 | 259 |
@cached |
260 |
def metainformation(self): |
|
3378
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
261 |
res = dict(zip(('type', 'source', 'extid'), self._cw.describe(self.eid))) |
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
262 |
res['source'] = self._cw.source_defs()[res['source']] |
0 | 263 |
return res |
264 |
||
475
b32a5772ff06
should clear local perm cache if first attempt failed
sylvain.thenault@logilab.fr
parents:
413
diff
changeset
|
265 |
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
|
266 |
for rqlexpr in self.e_schema.get_rqlexprs(action): |
3378
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
267 |
self._cw.local_perm_cache.pop((rqlexpr.eid, (('x', self.eid),)), None) |
475
b32a5772ff06
should clear local perm cache if first attempt failed
sylvain.thenault@logilab.fr
parents:
413
diff
changeset
|
268 |
|
0 | 269 |
def check_perm(self, action): |
3890
d7a270f50f54
backport stable branch (one more time painfully)
Sylvain Thénault <sylvain.thenault@logilab.fr>
diff
changeset
|
270 |
self.e_schema.check_perm(self._cw, action, eid=self.eid) |
0 | 271 |
|
272 |
def has_perm(self, action): |
|
3890
d7a270f50f54
backport stable branch (one more time painfully)
Sylvain Thénault <sylvain.thenault@logilab.fr>
diff
changeset
|
273 |
return self.e_schema.has_perm(self._cw, action, eid=self.eid) |
1474 | 274 |
|
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
|
275 |
def view(self, vid, __registry='views', **kwargs): |
0 | 276 |
"""shortcut to apply a view on this entity""" |
3459
e134d2dd9992
[entity] pass cw_row and cw_col to select(vid, ...) in self.view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3452
diff
changeset
|
277 |
view = self._cw.vreg[__registry].select(vid, self._cw, rset=self.cw_rset, |
e134d2dd9992
[entity] pass cw_row and cw_col to select(vid, ...) in self.view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3452
diff
changeset
|
278 |
row=self.cw_row, col=self.cw_col, |
e134d2dd9992
[entity] pass cw_row and cw_col to select(vid, ...) in self.view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3452
diff
changeset
|
279 |
**kwargs) |
3418
7b49fa7e942d
[api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3405
diff
changeset
|
280 |
return view.render(row=self.cw_row, col=self.cw_col, **kwargs) |
0 | 281 |
|
2059
af33833d7571
absolute_url / build_url refactoring to avoid potential name clash
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2058
diff
changeset
|
282 |
def absolute_url(self, *args, **kwargs): |
0 | 283 |
"""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
|
284 |
# 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
|
285 |
# 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
|
286 |
if args: |
af33833d7571
absolute_url / build_url refactoring to avoid potential name clash
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2058
diff
changeset
|
287 |
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
|
288 |
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
|
289 |
else: |
af33833d7571
absolute_url / build_url refactoring to avoid potential name clash
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2058
diff
changeset
|
290 |
method = None |
0 | 291 |
# in linksearch mode, we don't want external urls else selecting |
292 |
# the object for use in the relation is tricky |
|
293 |
# XXX search_state is web specific |
|
3378
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
294 |
if getattr(self._cw, 'search_state', ('normal',))[0] == 'normal': |
0 | 295 |
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
|
296 |
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
|
297 |
try: |
c7a22540d6f7
add some bw compat code for old rest_path prototype
sylvain.thenault@logilab.fr
parents:
1840
diff
changeset
|
298 |
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
|
299 |
except TypeError: |
3405
9d31c9cb8103
nicer deprecation warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3382
diff
changeset
|
300 |
warn('[3.4] %s: rest_path() now take use_ext_eid argument, ' |
3376
f5c69485381f
[appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3369
diff
changeset
|
301 |
'please update' % self.__regid__, DeprecationWarning) |
1842
c7a22540d6f7
add some bw compat code for old rest_path prototype
sylvain.thenault@logilab.fr
parents:
1840
diff
changeset
|
302 |
kwargs['_restpath'] = self.rest_path() |
0 | 303 |
else: |
304 |
kwargs['rql'] = 'Any X WHERE X eid %s' % self.eid |
|
3378
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
305 |
return self._cw.build_url(method, **kwargs) |
0 | 306 |
|
1840
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
307 |
def rest_path(self, use_ext_eid=False): |
0 | 308 |
"""returns a REST-like (relative) path for this entity""" |
309 |
mainattr, needcheck = self._rest_attr_info() |
|
310 |
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
|
311 |
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
|
312 |
if mainattr != 'eid': |
0 | 313 |
value = getattr(self, mainattr) |
2125
19861294506f
https://www.logilab.net/cwo/ticket/343724
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2005
diff
changeset
|
314 |
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
|
315 |
mainattr = 'eid' |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
316 |
path += '/eid' |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
317 |
elif needcheck: |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
318 |
# make sure url is not ambiguous |
4363
5c18d82042fb
cache result of the COUNT query used to see if a non unique attribute may be used as rest path on the entity instance to avoid recomputing it later
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
319 |
try: |
5c18d82042fb
cache result of the COUNT query used to see if a non unique attribute may be used as rest path on the entity instance to avoid recomputing it later
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
320 |
nbresults = self.__unique |
5c18d82042fb
cache result of the COUNT query used to see if a non unique attribute may be used as rest path on the entity instance to avoid recomputing it later
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
321 |
except AttributeError: |
5c18d82042fb
cache result of the COUNT query used to see if a non unique attribute may be used as rest path on the entity instance to avoid recomputing it later
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
322 |
rql = 'Any COUNT(X) WHERE X is %s, X %s %%(value)s' % ( |
5c18d82042fb
cache result of the COUNT query used to see if a non unique attribute may be used as rest path on the entity instance to avoid recomputing it later
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
323 |
etype, mainattr) |
5c18d82042fb
cache result of the COUNT query used to see if a non unique attribute may be used as rest path on the entity instance to avoid recomputing it later
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
324 |
nbresults = self.__unique = self._cw.execute(rql, {'value' : value})[0][0] |
1904
e23536d29231
minor refactoring of absolute_url()
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1842
diff
changeset
|
325 |
if nbresults != 1: # ambiguity? |
e23536d29231
minor refactoring of absolute_url()
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1842
diff
changeset
|
326 |
mainattr = 'eid' |
e23536d29231
minor refactoring of absolute_url()
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1842
diff
changeset
|
327 |
path += '/eid' |
0 | 328 |
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
|
329 |
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
|
330 |
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
|
331 |
else: |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
332 |
value = self.eid |
3378
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
333 |
return '%s/%s' % (path, self._cw.url_quote(value)) |
0 | 334 |
|
1360
13ae1121835e
rename attribute_metadata method to attr_metadata to save a few chars
sylvain.thenault@logilab.fr
parents:
1313
diff
changeset
|
335 |
def attr_metadata(self, attr, metadata): |
1101
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
1098
diff
changeset
|
336 |
"""return a metadata for an attribute (None if unspecified)""" |
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
1098
diff
changeset
|
337 |
value = getattr(self, '%s_%s' % (attr, metadata), None) |
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
1098
diff
changeset
|
338 |
if value is None and metadata == 'encoding': |
3378
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
339 |
value = self._cw.vreg.property_value('ui.encoding') |
1101
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
1098
diff
changeset
|
340 |
return value |
0 | 341 |
|
342 |
def printable_value(self, attr, value=_marker, attrtype=None, |
|
343 |
format='text/html', displaytime=True): |
|
344 |
"""return a displayable value (i.e. unicode string) which may contains |
|
345 |
html tags |
|
346 |
""" |
|
347 |
attr = str(attr) |
|
348 |
if value is _marker: |
|
349 |
value = getattr(self, attr) |
|
350 |
if isinstance(value, basestring): |
|
351 |
value = value.strip() |
|
352 |
if value is None or value == '': # don't use "not", 0 is an acceptable value |
|
353 |
return u'' |
|
354 |
if attrtype is None: |
|
355 |
attrtype = self.e_schema.destination(attr) |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
356 |
props = self.e_schema.rdef(attr) |
0 | 357 |
if attrtype == 'String': |
358 |
# internalinalized *and* formatted string such as schema |
|
359 |
# description... |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
360 |
if props.internationalizable: |
3378
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
361 |
value = self._cw._(value) |
1360
13ae1121835e
rename attribute_metadata method to attr_metadata to save a few chars
sylvain.thenault@logilab.fr
parents:
1313
diff
changeset
|
362 |
attrformat = self.attr_metadata(attr, 'format') |
0 | 363 |
if attrformat: |
364 |
return self.mtc_transform(value, attrformat, format, |
|
3378
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
365 |
self._cw.encoding) |
0 | 366 |
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
|
367 |
attrformat = self.attr_metadata(attr, 'format') |
0 | 368 |
if attrformat: |
1360
13ae1121835e
rename attribute_metadata method to attr_metadata to save a few chars
sylvain.thenault@logilab.fr
parents:
1313
diff
changeset
|
369 |
encoding = self.attr_metadata(attr, 'encoding') |
0 | 370 |
return self.mtc_transform(value.getvalue(), attrformat, format, |
371 |
encoding) |
|
372 |
return u'' |
|
3378
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
373 |
value = printable_value(self._cw, attrtype, value, props, |
3212
07d11bacfefe
displaytime attribute should not have been removed from there
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3184
diff
changeset
|
374 |
displaytime=displaytime) |
0 | 375 |
if format == 'text/html': |
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2125
diff
changeset
|
376 |
value = xml_escape(value) |
0 | 377 |
return value |
378 |
||
379 |
def mtc_transform(self, data, format, target_format, encoding, |
|
380 |
_engine=ENGINE): |
|
381 |
trdata = TransformData(data, format, encoding, appobject=self) |
|
382 |
data = _engine.convert(trdata, target_format).decode() |
|
383 |
if format == 'text/html': |
|
3378
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
384 |
data = soup2xhtml(data, self._cw.encoding) |
0 | 385 |
return data |
1474 | 386 |
|
0 | 387 |
# entity cloning ########################################################## |
388 |
||
389 |
def copy_relations(self, ceid): |
|
3626
017869a514c3
[doc] updated docstrings
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
3552
diff
changeset
|
390 |
"""copy relations of the object with the given eid on this |
017869a514c3
[doc] updated docstrings
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
3552
diff
changeset
|
391 |
object (this method is called on the newly created copy, and |
017869a514c3
[doc] updated docstrings
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
3552
diff
changeset
|
392 |
ceid designates the original entity). |
0 | 393 |
|
394 |
By default meta and composite relations are skipped. |
|
395 |
Overrides this if you want another behaviour |
|
396 |
""" |
|
397 |
assert self.has_eid() |
|
3378
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
398 |
execute = self._cw.execute |
0 | 399 |
for rschema in self.e_schema.subject_relations(): |
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3683
diff
changeset
|
400 |
if rschema.final or rschema.meta: |
0 | 401 |
continue |
402 |
# skip already defined relations |
|
403 |
if getattr(self, rschema.type): |
|
404 |
continue |
|
405 |
if rschema.type in self.skip_copy_for: |
|
406 |
continue |
|
407 |
# skip composite relation |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
408 |
rdef = self.e_schema.rdef(rschema) |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
409 |
if rdef.composite: |
0 | 410 |
continue |
411 |
# skip relation with card in ?1 else we either change the copied |
|
412 |
# object (inlined relation) or inserting some inconsistency |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
413 |
if rdef.cardinality[1] in '?1': |
0 | 414 |
continue |
415 |
rql = 'SET X %s V WHERE X eid %%(x)s, Y eid %%(y)s, Y %s V' % ( |
|
416 |
rschema.type, rschema.type) |
|
417 |
execute(rql, {'x': self.eid, 'y': ceid}, ('x', 'y')) |
|
418 |
self.clear_related_cache(rschema.type, 'subject') |
|
419 |
for rschema in self.e_schema.object_relations(): |
|
420 |
if rschema.meta: |
|
421 |
continue |
|
422 |
# skip already defined relations |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
423 |
if self.related(rschema.type, 'object'): |
0 | 424 |
continue |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
425 |
rdef = self.e_schema.rdef(rschema, 'object') |
0 | 426 |
# skip composite relation |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
427 |
if rdef.composite: |
0 | 428 |
continue |
429 |
# skip relation with card in ?1 else we either change the copied |
|
430 |
# object (inlined relation) or inserting some inconsistency |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
431 |
if rdef.cardinality[0] in '?1': |
0 | 432 |
continue |
433 |
rql = 'SET V %s X WHERE X eid %%(x)s, Y eid %%(y)s, V %s Y' % ( |
|
434 |
rschema.type, rschema.type) |
|
435 |
execute(rql, {'x': self.eid, 'y': ceid}, ('x', 'y')) |
|
436 |
self.clear_related_cache(rschema.type, 'object') |
|
437 |
||
438 |
# data fetching methods ################################################### |
|
439 |
||
440 |
@cached |
|
441 |
def as_rset(self): |
|
442 |
"""returns a resultset containing `self` information""" |
|
443 |
rset = ResultSet([(self.eid,)], 'Any X WHERE X eid %(x)s', |
|
3376
f5c69485381f
[appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3369
diff
changeset
|
444 |
{'x': self.eid}, [(self.__regid__,)]) |
3378
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
445 |
return self._cw.decorate_rset(rset) |
1474 | 446 |
|
0 | 447 |
def to_complete_relations(self): |
448 |
"""by default complete final relations to when calling .complete()""" |
|
449 |
for rschema in self.e_schema.subject_relations(): |
|
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3683
diff
changeset
|
450 |
if rschema.final: |
0 | 451 |
continue |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
452 |
targets = rschema.objects(self.e_schema) |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
453 |
if len(targets) > 1: |
0 | 454 |
# ambigous relations, the querier doesn't handle |
455 |
# outer join correctly in this case |
|
456 |
continue |
|
457 |
if rschema.inlined: |
|
3378
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
458 |
matching_groups = self._cw.user.matching_groups |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
459 |
rdef = rschema.rdef(self.e_schema, targets[0]) |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
460 |
if matching_groups(rdef.get_groups('read')) and \ |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
461 |
all(matching_groups(e.get_groups('read')) for e in targets): |
0 | 462 |
yield rschema, 'subject' |
1474 | 463 |
|
0 | 464 |
def to_complete_attributes(self, skip_bytes=True): |
465 |
for rschema, attrschema in self.e_schema.attribute_definitions(): |
|
466 |
# skip binary data by default |
|
467 |
if skip_bytes and attrschema.type == 'Bytes': |
|
468 |
continue |
|
469 |
attr = rschema.type |
|
470 |
if attr == 'eid': |
|
471 |
continue |
|
472 |
# password retreival is blocked at the repository server level |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
473 |
rdef = rschema.rdef(self.e_schema, attrschema) |
3890
d7a270f50f54
backport stable branch (one more time painfully)
Sylvain Thénault <sylvain.thenault@logilab.fr>
diff
changeset
|
474 |
if not self._cw.user.matching_groups(rdef.get_groups('read')) \ |
0 | 475 |
or attrschema.type == 'Password': |
476 |
self[attr] = None |
|
477 |
continue |
|
478 |
yield attr |
|
1474 | 479 |
|
0 | 480 |
def complete(self, attributes=None, skip_bytes=True): |
481 |
"""complete this entity by adding missing attributes (i.e. query the |
|
482 |
repository to fill the entity) |
|
483 |
||
484 |
:type skip_bytes: bool |
|
485 |
:param skip_bytes: |
|
486 |
if true, attribute of type Bytes won't be considered |
|
487 |
""" |
|
488 |
assert self.has_eid() |
|
489 |
varmaker = rqlvar_maker() |
|
490 |
V = varmaker.next() |
|
491 |
rql = ['WHERE %s eid %%(x)s' % V] |
|
492 |
selected = [] |
|
493 |
for attr in (attributes or self.to_complete_attributes(skip_bytes)): |
|
494 |
# if attribute already in entity, nothing to do |
|
495 |
if self.has_key(attr): |
|
496 |
continue |
|
497 |
# case where attribute must be completed, but is not yet in entity |
|
498 |
var = varmaker.next() |
|
499 |
rql.append('%s %s %s' % (V, attr, var)) |
|
500 |
selected.append((attr, var)) |
|
501 |
# +1 since this doen't include the main variable |
|
502 |
lastattr = len(selected) + 1 |
|
503 |
if attributes is None: |
|
504 |
# fetch additional relations (restricted to 0..1 relations) |
|
505 |
for rschema, role in self.to_complete_relations(): |
|
506 |
rtype = rschema.type |
|
507 |
if self.relation_cached(rtype, role): |
|
508 |
continue |
|
509 |
var = varmaker.next() |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
510 |
targettype = rschema.targets(self.e_schema, role)[0] |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
511 |
rdef = rschema.role_rdef(self.e_schema, targettype, role) |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
512 |
card = rdef.role_cardinality(role) |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
513 |
assert card in '1?', '%s %s %s %s' % (self.e_schema, rtype, |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
514 |
role, card) |
0 | 515 |
if role == 'subject': |
516 |
if card == '1': |
|
517 |
rql.append('%s %s %s' % (V, rtype, var)) |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
518 |
else: |
0 | 519 |
rql.append('%s %s %s?' % (V, rtype, var)) |
520 |
else: |
|
521 |
if card == '1': |
|
522 |
rql.append('%s %s %s' % (var, rtype, V)) |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
523 |
else: |
0 | 524 |
rql.append('%s? %s %s' % (var, rtype, V)) |
525 |
selected.append(((rtype, role), var)) |
|
526 |
if selected: |
|
527 |
# select V, we need it as the left most selected variable |
|
528 |
# if some outer join are included to fetch inlined relations |
|
529 |
rql = 'Any %s,%s %s' % (V, ','.join(var for attr, var in selected), |
|
530 |
','.join(rql)) |
|
3378
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
531 |
execute = getattr(self._cw, 'unsafe_execute', self._cw.execute) |
0 | 532 |
rset = execute(rql, {'x': self.eid}, 'x', build_descr=False)[0] |
533 |
# handle attributes |
|
534 |
for i in xrange(1, lastattr): |
|
535 |
self[str(selected[i-1][0])] = rset[i] |
|
536 |
# handle relations |
|
537 |
for i in xrange(lastattr, len(rset)): |
|
2941
196493bd099c
[entity] use role instead of x
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2931
diff
changeset
|
538 |
rtype, role = selected[i-1][0] |
0 | 539 |
value = rset[i] |
540 |
if value is None: |
|
541 |
rrset = ResultSet([], rql, {'x': self.eid}) |
|
3378
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
542 |
self._cw.decorate_rset(rrset) |
0 | 543 |
else: |
3378
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
544 |
rrset = self._cw.eid_rset(value) |
2941
196493bd099c
[entity] use role instead of x
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2931
diff
changeset
|
545 |
self.set_related_cache(rtype, role, rrset) |
1474 | 546 |
|
0 | 547 |
def get_value(self, name): |
548 |
"""get value for the attribute relation <name>, query the repository |
|
549 |
to get the value if necessary. |
|
550 |
||
551 |
:type name: str |
|
552 |
:param name: name of the attribute to get |
|
553 |
""" |
|
554 |
try: |
|
555 |
value = self[name] |
|
556 |
except KeyError: |
|
557 |
if not self.is_saved(): |
|
558 |
return None |
|
559 |
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
|
560 |
# 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
|
561 |
# see #344874 |
3378
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
562 |
execute = getattr(self._cw, 'unsafe_execute', self._cw.execute) |
0 | 563 |
try: |
564 |
rset = execute(rql, {'x': self.eid}, 'x') |
|
565 |
except Unauthorized: |
|
566 |
self[name] = value = None |
|
567 |
else: |
|
568 |
assert rset.rowcount <= 1, (self, rql, rset.rowcount) |
|
569 |
try: |
|
570 |
self[name] = value = rset.rows[0][0] |
|
571 |
except IndexError: |
|
572 |
# probably a multisource error |
|
573 |
self.critical("can't get value for attribute %s of entity with eid %s", |
|
574 |
name, self.eid) |
|
575 |
if self.e_schema.destination(name) == 'String': |
|
2320 | 576 |
# XXX (syt) imo emtpy string is better |
3378
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
577 |
self[name] = value = self._cw._('unaccessible') |
0 | 578 |
else: |
579 |
self[name] = value = None |
|
580 |
return value |
|
581 |
||
582 |
def related(self, rtype, role='subject', limit=None, entities=False): |
|
583 |
"""returns a resultset of related entities |
|
1474 | 584 |
|
0 | 585 |
:param role: is the role played by 'self' in the relation ('subject' or 'object') |
586 |
:param limit: resultset's maximum size |
|
587 |
:param entities: if True, the entites are returned; if False, a result set is returned |
|
588 |
""" |
|
589 |
try: |
|
590 |
return self.related_cache(rtype, role, entities, limit) |
|
591 |
except KeyError: |
|
592 |
pass |
|
593 |
assert self.has_eid() |
|
594 |
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
|
595 |
# 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
|
596 |
# see #344874 |
3378
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
597 |
execute = getattr(self._cw, 'unsafe_execute', self._cw.execute) |
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
|
598 |
rset = execute(rql, {'x': self.eid}, 'x') |
0 | 599 |
self.set_related_cache(rtype, role, rset) |
600 |
return self.related(rtype, role, limit, entities) |
|
601 |
||
3130
2486163c4630
allow to specify targettypes in related_rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3047
diff
changeset
|
602 |
def related_rql(self, rtype, role='subject', targettypes=None): |
3378
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
603 |
rschema = self._cw.vreg.schema[rtype] |
0 | 604 |
if role == 'subject': |
3672
554a588ffaea
[entity] make related_rql honors its targettypes argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3671
diff
changeset
|
605 |
restriction = 'E eid %%(x)s, E %s X' % rtype |
3130
2486163c4630
allow to specify targettypes in related_rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3047
diff
changeset
|
606 |
if targettypes is None: |
2486163c4630
allow to specify targettypes in related_rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3047
diff
changeset
|
607 |
targettypes = rschema.objects(self.e_schema) |
3672
554a588ffaea
[entity] make related_rql honors its targettypes argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3671
diff
changeset
|
608 |
else: |
3683
2e4794c97cf4
fix related_rql when target_type specified
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
3680
diff
changeset
|
609 |
restriction += ', X is IN (%s)' % ','.join(targettypes) |
0 | 610 |
card = greater_card(rschema, (self.e_schema,), targettypes, 0) |
611 |
else: |
|
3672
554a588ffaea
[entity] make related_rql honors its targettypes argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3671
diff
changeset
|
612 |
restriction = 'E eid %%(x)s, X %s E' % rtype |
3130
2486163c4630
allow to specify targettypes in related_rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3047
diff
changeset
|
613 |
if targettypes is None: |
2486163c4630
allow to specify targettypes in related_rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3047
diff
changeset
|
614 |
targettypes = rschema.subjects(self.e_schema) |
3672
554a588ffaea
[entity] make related_rql honors its targettypes argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3671
diff
changeset
|
615 |
else: |
3683
2e4794c97cf4
fix related_rql when target_type specified
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
3680
diff
changeset
|
616 |
restriction += ', X is IN (%s)' % ','.join(targettypes) |
0 | 617 |
card = greater_card(rschema, targettypes, (self.e_schema,), 1) |
618 |
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
|
619 |
fetchattrs_list = [] |
0 | 620 |
for ttype in targettypes: |
3378
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
621 |
etypecls = self._cw.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
|
622 |
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
|
623 |
fetchattrs = reduce(set.intersection, fetchattrs_list) |
3378
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
624 |
rql = etypecls.fetch_rql(self._cw.user, [restriction], fetchattrs, |
0 | 625 |
settype=False) |
626 |
else: |
|
3378
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
627 |
etypecls = self._cw.vreg['etypes'].etype_class(targettypes[0]) |
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
628 |
rql = etypecls.fetch_rql(self._cw.user, [restriction], settype=False) |
0 | 629 |
# optimisation: remove ORDERBY if cardinality is 1 or ? (though |
630 |
# greater_card return 1 for those both cases) |
|
631 |
if card == '1': |
|
632 |
if ' ORDERBY ' in rql: |
|
633 |
rql = '%s WHERE %s' % (rql.split(' ORDERBY ', 1)[0], |
|
634 |
rql.split(' WHERE ', 1)[1]) |
|
635 |
elif not ' ORDERBY ' in rql: |
|
636 |
args = tuple(rql.split(' WHERE ', 1)) |
|
637 |
rql = '%s ORDERBY Z DESC WHERE X modification_date Z, %s' % args |
|
638 |
return rql |
|
1474 | 639 |
|
0 | 640 |
# generic vocabulary methods ############################################## |
641 |
||
642 |
def unrelated_rql(self, rtype, targettype, role, ordermethod=None, |
|
643 |
vocabconstraints=True): |
|
644 |
"""build a rql to fetch `targettype` entities unrelated to this entity |
|
3241
1a6f7a0e7dbd
unrelated_rql now considers relation's add perm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3212
diff
changeset
|
645 |
using (rtype, role) relation. |
1a6f7a0e7dbd
unrelated_rql now considers relation's add perm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3212
diff
changeset
|
646 |
|
1a6f7a0e7dbd
unrelated_rql now considers relation's add perm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3212
diff
changeset
|
647 |
Consider relation permissions so that returned entities may be actually |
1a6f7a0e7dbd
unrelated_rql now considers relation's add perm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3212
diff
changeset
|
648 |
linked by `rtype`. |
0 | 649 |
""" |
650 |
ordermethod = ordermethod or 'fetch_unrelated_order' |
|
651 |
if isinstance(rtype, basestring): |
|
3378
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
652 |
rtype = self._cw.vreg.schema.rschema(rtype) |
0 | 653 |
if role == 'subject': |
654 |
evar, searchedvar = 'S', 'O' |
|
655 |
subjtype, objtype = self.e_schema, targettype |
|
656 |
else: |
|
657 |
searchedvar, evar = 'S', 'O' |
|
658 |
objtype, subjtype = self.e_schema, targettype |
|
659 |
if self.has_eid(): |
|
660 |
restriction = ['NOT S %s O' % rtype, '%s eid %%(x)s' % evar] |
|
3241
1a6f7a0e7dbd
unrelated_rql now considers relation's add perm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3212
diff
changeset
|
661 |
args = {'x': self.eid} |
1a6f7a0e7dbd
unrelated_rql now considers relation's add perm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3212
diff
changeset
|
662 |
if role == 'subject': |
1a6f7a0e7dbd
unrelated_rql now considers relation's add perm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3212
diff
changeset
|
663 |
securitycheck_args = {'fromeid': self.eid} |
1a6f7a0e7dbd
unrelated_rql now considers relation's add perm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3212
diff
changeset
|
664 |
else: |
1a6f7a0e7dbd
unrelated_rql now considers relation's add perm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3212
diff
changeset
|
665 |
securitycheck_args = {'toeid': self.eid} |
0 | 666 |
else: |
667 |
restriction = [] |
|
3241
1a6f7a0e7dbd
unrelated_rql now considers relation's add perm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3212
diff
changeset
|
668 |
args = {} |
1a6f7a0e7dbd
unrelated_rql now considers relation's add perm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3212
diff
changeset
|
669 |
securitycheck_args = {} |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
670 |
rdef = rtype.role_rdef(self.e_schema, targettype, role) |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
671 |
insertsecurity = (rdef.has_local_role('add') and not |
3890
d7a270f50f54
backport stable branch (one more time painfully)
Sylvain Thénault <sylvain.thenault@logilab.fr>
diff
changeset
|
672 |
rdef.has_perm(self._cw, 'add', **securitycheck_args)) |
3961
d1cbf77db999
fix RQLUniqueConstraint behaviour by using a DISTINCT query and allowing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3955
diff
changeset
|
673 |
# XXX consider constraint.mainvars to check if constraint apply |
0 | 674 |
if vocabconstraints: |
675 |
# RQLConstraint is a subclass for RQLVocabularyConstraint, so they |
|
676 |
# will be included as well |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
677 |
restriction += [cstr.restriction for cstr in rdef.constraints |
0 | 678 |
if isinstance(cstr, RQLVocabularyConstraint)] |
679 |
else: |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
680 |
restriction += [cstr.restriction for cstr in rdef.constraints |
0 | 681 |
if isinstance(cstr, RQLConstraint)] |
3378
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
682 |
etypecls = self._cw.vreg['etypes'].etype_class(targettype) |
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
683 |
rql = etypecls.fetch_rql(self._cw.user, restriction, |
0 | 684 |
mainvar=searchedvar, ordermethod=ordermethod) |
685 |
# ensure we have an order defined |
|
686 |
if not ' ORDERBY ' in rql: |
|
687 |
before, after = rql.split(' WHERE ', 1) |
|
688 |
rql = '%s ORDERBY %s WHERE %s' % (before, searchedvar, after) |
|
3241
1a6f7a0e7dbd
unrelated_rql now considers relation's add perm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3212
diff
changeset
|
689 |
if insertsecurity: |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
690 |
rqlexprs = rdef.get_rqlexprs('add') |
3378
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
691 |
rewriter = RQLRewriter(self._cw) |
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
692 |
rqlst = self._cw.vreg.parse(self._cw, rql, args) |
3826
0c0c051863cb
close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3746
diff
changeset
|
693 |
if not self.has_eid(): |
0c0c051863cb
close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3746
diff
changeset
|
694 |
existant = searchedvar |
0c0c051863cb
close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3746
diff
changeset
|
695 |
else: |
0c0c051863cb
close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3746
diff
changeset
|
696 |
existant = None # instead of 'SO', improve perfs |
3241
1a6f7a0e7dbd
unrelated_rql now considers relation's add perm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3212
diff
changeset
|
697 |
for select in rqlst.children: |
1a6f7a0e7dbd
unrelated_rql now considers relation's add perm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3212
diff
changeset
|
698 |
rewriter.rewrite(select, [((searchedvar, searchedvar), rqlexprs)], |
3826
0c0c051863cb
close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3746
diff
changeset
|
699 |
select.solutions, args, existant) |
3241
1a6f7a0e7dbd
unrelated_rql now considers relation's add perm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3212
diff
changeset
|
700 |
rql = rqlst.as_string() |
1a6f7a0e7dbd
unrelated_rql now considers relation's add perm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3212
diff
changeset
|
701 |
return rql, args |
1474 | 702 |
|
0 | 703 |
def unrelated(self, rtype, targettype, role='subject', limit=None, |
704 |
ordermethod=None): |
|
705 |
"""return a result set of target type objects that may be related |
|
706 |
by a given relation, with self as subject or object |
|
707 |
""" |
|
3241
1a6f7a0e7dbd
unrelated_rql now considers relation's add perm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3212
diff
changeset
|
708 |
try: |
1a6f7a0e7dbd
unrelated_rql now considers relation's add perm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3212
diff
changeset
|
709 |
rql, args = self.unrelated_rql(rtype, targettype, role, ordermethod) |
1a6f7a0e7dbd
unrelated_rql now considers relation's add perm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3212
diff
changeset
|
710 |
except Unauthorized: |
3378
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
711 |
return self._cw.empty_rset() |
0 | 712 |
if limit is not None: |
713 |
before, after = rql.split(' WHERE ', 1) |
|
714 |
rql = '%s LIMIT %s WHERE %s' % (before, limit, after) |
|
3378
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
715 |
return self._cw.execute(rql, args, tuple(args)) |
1474 | 716 |
|
0 | 717 |
# relations cache handling ################################################ |
1474 | 718 |
|
0 | 719 |
def relation_cached(self, rtype, role): |
720 |
"""return true if the given relation is already cached on the instance |
|
721 |
""" |
|
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
|
722 |
return self._related_cache.get('%s_%s' % (rtype, role)) |
1474 | 723 |
|
0 | 724 |
def related_cache(self, rtype, role, entities=True, limit=None): |
725 |
"""return values for the given relation if it's cached on the instance, |
|
726 |
else raise `KeyError` |
|
727 |
""" |
|
728 |
res = self._related_cache['%s_%s' % (rtype, role)][entities] |
|
3680 | 729 |
if limit is not None and limit < len(res): |
0 | 730 |
if entities: |
731 |
res = res[:limit] |
|
732 |
else: |
|
733 |
res = res.limit(limit) |
|
734 |
return res |
|
1474 | 735 |
|
0 | 736 |
def set_related_cache(self, rtype, role, rset, col=0): |
737 |
"""set cached values for the given relation""" |
|
738 |
if rset: |
|
739 |
related = list(rset.entities(col)) |
|
3378
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
740 |
rschema = self._cw.vreg.schema.rschema(rtype) |
0 | 741 |
if role == 'subject': |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
742 |
rcard = rschema.rdef(self.e_schema, related[0].e_schema).cardinality[1] |
0 | 743 |
target = 'object' |
744 |
else: |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
745 |
rcard = rschema.rdef(related[0].e_schema, self.e_schema).cardinality[0] |
0 | 746 |
target = 'subject' |
747 |
if rcard in '?1': |
|
748 |
for rentity in related: |
|
3552
8facb3324170
[entity] put cached related entities as tuple to be sure no one is modifying
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3546
diff
changeset
|
749 |
rentity._related_cache['%s_%s' % (rtype, target)] = ( |
8facb3324170
[entity] put cached related entities as tuple to be sure no one is modifying
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3546
diff
changeset
|
750 |
self.as_rset(), (self,)) |
0 | 751 |
else: |
3552
8facb3324170
[entity] put cached related entities as tuple to be sure no one is modifying
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3546
diff
changeset
|
752 |
related = () |
0 | 753 |
self._related_cache['%s_%s' % (rtype, role)] = (rset, related) |
1474 | 754 |
|
0 | 755 |
def clear_related_cache(self, rtype=None, role=None): |
756 |
"""clear cached values for the given relation or the entire cache if |
|
757 |
no relation is given |
|
758 |
""" |
|
759 |
if rtype is None: |
|
760 |
self._related_cache = {} |
|
761 |
else: |
|
762 |
assert role |
|
763 |
self._related_cache.pop('%s_%s' % (rtype, role), None) |
|
1474 | 764 |
|
2942
77ebdbe93cf8
[entity] generic clear_all_caches methods, to use in test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2941
diff
changeset
|
765 |
def clear_all_caches(self): |
3665
42f3a66cab51
fix clear_all_cache: ensure we don't remove eid from entity's dict
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3664
diff
changeset
|
766 |
haseid = 'eid' in self |
2942
77ebdbe93cf8
[entity] generic clear_all_caches methods, to use in test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2941
diff
changeset
|
767 |
self.clear() |
77ebdbe93cf8
[entity] generic clear_all_caches methods, to use in test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2941
diff
changeset
|
768 |
for rschema, _, role in self.e_schema.relation_definitions(): |
77ebdbe93cf8
[entity] generic clear_all_caches methods, to use in test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2941
diff
changeset
|
769 |
self.clear_related_cache(rschema.type, role) |
3665
42f3a66cab51
fix clear_all_cache: ensure we don't remove eid from entity's dict
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3664
diff
changeset
|
770 |
# set eid if it was in, else we may get nasty error while editing this |
42f3a66cab51
fix clear_all_cache: ensure we don't remove eid from entity's dict
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3664
diff
changeset
|
771 |
# entity if it's bound to a repo session |
42f3a66cab51
fix clear_all_cache: ensure we don't remove eid from entity's dict
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3664
diff
changeset
|
772 |
if haseid: |
42f3a66cab51
fix clear_all_cache: ensure we don't remove eid from entity's dict
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3664
diff
changeset
|
773 |
self['eid'] = self.eid |
2942
77ebdbe93cf8
[entity] generic clear_all_caches methods, to use in test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2941
diff
changeset
|
774 |
|
0 | 775 |
# raw edition utilities ################################################### |
1474 | 776 |
|
2877
e469f3602858
new _cw_unsafe argument to Entity.set_attributes until something nicer is done (eg #344874)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2875
diff
changeset
|
777 |
def set_attributes(self, _cw_unsafe=False, **kwargs): |
0 | 778 |
assert kwargs |
779 |
relations = [] |
|
780 |
for key in kwargs: |
|
781 |
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
|
782 |
# 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
|
783 |
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
|
784 |
# and now update the database |
0 | 785 |
kwargs['x'] = self.eid |
2877
e469f3602858
new _cw_unsafe argument to Entity.set_attributes until something nicer is done (eg #344874)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2875
diff
changeset
|
786 |
if _cw_unsafe: |
3378
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
787 |
self._cw.unsafe_execute( |
2877
e469f3602858
new _cw_unsafe argument to Entity.set_attributes until something nicer is done (eg #344874)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2875
diff
changeset
|
788 |
'SET %s WHERE X eid %%(x)s' % ','.join(relations), kwargs, 'x') |
e469f3602858
new _cw_unsafe argument to Entity.set_attributes until something nicer is done (eg #344874)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2875
diff
changeset
|
789 |
else: |
3378
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
790 |
self._cw.execute('SET %s WHERE X eid %%(x)s' % ','.join(relations), |
2877
e469f3602858
new _cw_unsafe argument to Entity.set_attributes until something nicer is done (eg #344874)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2875
diff
changeset
|
791 |
kwargs, 'x') |
1474 | 792 |
|
3671
c765adac7f5c
new set_relations method on entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3665
diff
changeset
|
793 |
def set_relations(self, _cw_unsafe=False, **kwargs): |
3955
235a9fda6058
support x.set_relations(a_relation=None) to delete a_relation relations from this entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3919
diff
changeset
|
794 |
"""add relations to the given object. To set a relation where this entity |
235a9fda6058
support x.set_relations(a_relation=None) to delete a_relation relations from this entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3919
diff
changeset
|
795 |
is the object of the relation, use 'reverse_'<relation> as argument name. |
235a9fda6058
support x.set_relations(a_relation=None) to delete a_relation relations from this entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3919
diff
changeset
|
796 |
|
235a9fda6058
support x.set_relations(a_relation=None) to delete a_relation relations from this entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3919
diff
changeset
|
797 |
Values may be an entity, a list of entity, or None (meaning that all |
235a9fda6058
support x.set_relations(a_relation=None) to delete a_relation relations from this entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3919
diff
changeset
|
798 |
relations of the given type from or to this object should be deleted). |
235a9fda6058
support x.set_relations(a_relation=None) to delete a_relation relations from this entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3919
diff
changeset
|
799 |
""" |
3671
c765adac7f5c
new set_relations method on entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3665
diff
changeset
|
800 |
if _cw_unsafe: |
4049
5838a4b0766d
3.5 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4023
diff
changeset
|
801 |
execute = self._cw.unsafe_execute |
3671
c765adac7f5c
new set_relations method on entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3665
diff
changeset
|
802 |
else: |
4049
5838a4b0766d
3.5 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4023
diff
changeset
|
803 |
execute = self._cw.execute |
3955
235a9fda6058
support x.set_relations(a_relation=None) to delete a_relation relations from this entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3919
diff
changeset
|
804 |
# XXX update cache |
3671
c765adac7f5c
new set_relations method on entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3665
diff
changeset
|
805 |
for attr, values in kwargs.iteritems(): |
c765adac7f5c
new set_relations method on entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3665
diff
changeset
|
806 |
if attr.startswith('reverse_'): |
c765adac7f5c
new set_relations method on entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3665
diff
changeset
|
807 |
restr = 'Y %s X' % attr[len('reverse_'):] |
c765adac7f5c
new set_relations method on entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3665
diff
changeset
|
808 |
else: |
c765adac7f5c
new set_relations method on entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3665
diff
changeset
|
809 |
restr = 'X %s Y' % attr |
3955
235a9fda6058
support x.set_relations(a_relation=None) to delete a_relation relations from this entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3919
diff
changeset
|
810 |
if values is None: |
235a9fda6058
support x.set_relations(a_relation=None) to delete a_relation relations from this entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3919
diff
changeset
|
811 |
execute('DELETE %s WHERE X eid %%(x)s' % restr, |
235a9fda6058
support x.set_relations(a_relation=None) to delete a_relation relations from this entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3919
diff
changeset
|
812 |
{'x': self.eid}, 'x') |
235a9fda6058
support x.set_relations(a_relation=None) to delete a_relation relations from this entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3919
diff
changeset
|
813 |
continue |
3746
74192424b2c8
accepts single values as create_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
814 |
if not isinstance(values, (tuple, list, set, frozenset)): |
74192424b2c8
accepts single values as create_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
815 |
values = (values,) |
3671
c765adac7f5c
new set_relations method on entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3665
diff
changeset
|
816 |
execute('SET %s WHERE X eid %%(x)s, Y eid IN (%s)' % ( |
c765adac7f5c
new set_relations method on entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3665
diff
changeset
|
817 |
restr, ','.join(str(r.eid) for r in values)), |
c765adac7f5c
new set_relations method on entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3665
diff
changeset
|
818 |
{'x': self.eid}, 'x') |
c765adac7f5c
new set_relations method on entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3665
diff
changeset
|
819 |
|
0 | 820 |
def delete(self): |
821 |
assert self.has_eid(), self.eid |
|
3378
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
822 |
self._cw.execute('DELETE %s X WHERE X eid %%(x)s' % self.e_schema, |
0 | 823 |
{'x': self.eid}) |
1474 | 824 |
|
0 | 825 |
# server side utilities ################################################### |
1474 | 826 |
|
0 | 827 |
def set_defaults(self): |
828 |
"""set default values according to the schema""" |
|
829 |
self._default_set = set() |
|
830 |
for attr, value in self.e_schema.defaults(): |
|
831 |
if not self.has_key(attr): |
|
832 |
self[str(attr)] = value |
|
833 |
self._default_set.add(attr) |
|
834 |
||
835 |
def check(self, creation=False): |
|
836 |
"""check this entity against its schema. Only final relation |
|
837 |
are checked here, constraint on actual relations are checked in hooks |
|
838 |
""" |
|
839 |
# necessary since eid is handled specifically and yams require it to be |
|
840 |
# in the dictionary |
|
3378
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
841 |
if self._cw is None: |
0 | 842 |
_ = unicode |
843 |
else: |
|
3378
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
844 |
_ = self._cw._ |
0 | 845 |
self.e_schema.check(self, creation=creation, _=_) |
846 |
||
847 |
def fti_containers(self, _done=None): |
|
848 |
if _done is None: |
|
849 |
_done = set() |
|
850 |
_done.add(self.eid) |
|
851 |
containers = tuple(self.e_schema.fulltext_containers()) |
|
852 |
if containers: |
|
476
62968fa8845c
yield self if no ft container found
sylvain.thenault@logilab.fr
parents:
475
diff
changeset
|
853 |
yielded = False |
0 | 854 |
for rschema, target in containers: |
855 |
if target == 'object': |
|
856 |
targets = getattr(self, rschema.type) |
|
857 |
else: |
|
858 |
targets = getattr(self, 'reverse_%s' % rschema) |
|
859 |
for entity in targets: |
|
860 |
if entity.eid in _done: |
|
861 |
continue |
|
862 |
for container in entity.fti_containers(_done): |
|
863 |
yield container |
|
476
62968fa8845c
yield self if no ft container found
sylvain.thenault@logilab.fr
parents:
475
diff
changeset
|
864 |
yielded = True |
62968fa8845c
yield self if no ft container found
sylvain.thenault@logilab.fr
parents:
475
diff
changeset
|
865 |
if not yielded: |
62968fa8845c
yield self if no ft container found
sylvain.thenault@logilab.fr
parents:
475
diff
changeset
|
866 |
yield self |
0 | 867 |
else: |
868 |
yield self |
|
1474 | 869 |
|
0 | 870 |
def get_words(self): |
871 |
"""used by the full text indexer to get words to index |
|
872 |
||
873 |
this method should only be used on the repository side since it depends |
|
874 |
on the indexer package |
|
1474 | 875 |
|
0 | 876 |
:rtype: list |
877 |
:return: the list of indexable word of this entity |
|
878 |
""" |
|
879 |
from indexer.query_objects import tokenize |
|
3546
f0aecddf367e
fix bug when renaming a full text indexed attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3316
diff
changeset
|
880 |
# take care to cases where we're modyfying the schema |
3649
59eb710b9862
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3629
diff
changeset
|
881 |
pending = self._cw.transaction_data.setdefault('pendingrdefs', set()) |
0 | 882 |
words = [] |
883 |
for rschema in self.e_schema.indexable_attributes(): |
|
3546
f0aecddf367e
fix bug when renaming a full text indexed attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3316
diff
changeset
|
884 |
if (self.e_schema, rschema) in pending: |
f0aecddf367e
fix bug when renaming a full text indexed attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3316
diff
changeset
|
885 |
continue |
0 | 886 |
try: |
887 |
value = self.printable_value(rschema, format='text/plain') |
|
1132 | 888 |
except TransformError: |
0 | 889 |
continue |
890 |
except: |
|
891 |
self.exception("can't add value of %s to text index for entity %s", |
|
892 |
rschema, self.eid) |
|
893 |
continue |
|
894 |
if value: |
|
895 |
words += tokenize(value) |
|
1474 | 896 |
|
0 | 897 |
for rschema, role in self.e_schema.fulltext_relations(): |
898 |
if role == 'subject': |
|
899 |
for entity in getattr(self, rschema.type): |
|
900 |
words += entity.get_words() |
|
901 |
else: # if role == 'object': |
|
902 |
for entity in getattr(self, 'reverse_%s' % rschema.type): |
|
903 |
words += entity.get_words() |
|
904 |
return words |
|
905 |
||
906 |
||
907 |
# attribute and relation descriptors ########################################## |
|
908 |
||
909 |
class Attribute(object): |
|
910 |
"""descriptor that controls schema attribute access""" |
|
911 |
||
912 |
def __init__(self, attrname): |
|
913 |
assert attrname != 'eid' |
|
914 |
self._attrname = attrname |
|
915 |
||
916 |
def __get__(self, eobj, eclass): |
|
917 |
if eobj is None: |
|
918 |
return self |
|
919 |
return eobj.get_value(self._attrname) |
|
920 |
||
921 |
def __set__(self, eobj, value): |
|
922 |
eobj[self._attrname] = value |
|
2875
b7399ef8b3e0
towards better edited attributes handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2872
diff
changeset
|
923 |
if hasattr(eobj, 'edited_attributes'): |
b7399ef8b3e0
towards better edited attributes handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2872
diff
changeset
|
924 |
eobj.edited_attributes.add(self._attrname) |
0 | 925 |
|
926 |
class Relation(object): |
|
927 |
"""descriptor that controls schema relation access""" |
|
928 |
_role = None # for pylint |
|
929 |
||
930 |
def __init__(self, rschema): |
|
931 |
self._rschema = rschema |
|
932 |
self._rtype = rschema.type |
|
933 |
||
934 |
def __get__(self, eobj, eclass): |
|
935 |
if eobj is None: |
|
936 |
raise AttributeError('%s cannot be only be accessed from instances' |
|
937 |
% self._rtype) |
|
938 |
return eobj.related(self._rtype, self._role, entities=True) |
|
1474 | 939 |
|
0 | 940 |
def __set__(self, eobj, value): |
941 |
raise NotImplementedError |
|
942 |
||
943 |
||
944 |
class SubjectRelation(Relation): |
|
945 |
"""descriptor that controls schema relation access""" |
|
946 |
_role = 'subject' |
|
1474 | 947 |
|
0 | 948 |
class ObjectRelation(Relation): |
949 |
"""descriptor that controls schema relation access""" |
|
950 |
_role = 'object' |
|
951 |
||
952 |
from logging import getLogger |
|
953 |
from cubicweb import set_log_methods |
|
954 |
set_log_methods(Entity, getLogger('cubicweb.entity')) |