author | Aurelien Campeas <aurelien.campeas@logilab.fr> |
Fri, 16 Apr 2010 11:13:46 +0200 | |
branch | stable |
changeset 5305 | d98fccb0da7f |
parent 5199 | ebb50479d2ab |
child 5201 | 2b4267157f85 |
child 5320 | 848e92bb81cc |
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 |
|
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 |
||
3664
af7ca3597b8d
use typed_eid
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3626
diff
changeset
|
19 |
from cubicweb import Unauthorized, typed_eid |
0 | 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 |
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4831
diff
changeset
|
23 |
from cubicweb.req import _check_cw_unsafe |
2827
d1a89d165045
remove 3.2 bw compat code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2824
diff
changeset
|
24 |
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
|
25 |
from cubicweb.rqlrewrite import RQLRewriter |
709 | 26 |
|
4023
eae23c40627a
drop common subpackage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3998
diff
changeset
|
27 |
from cubicweb.uilib import printable_value, soup2xhtml |
eae23c40627a
drop common subpackage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3998
diff
changeset
|
28 |
from cubicweb.mixins import MI_REL_TRIGGERS |
eae23c40627a
drop common subpackage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3998
diff
changeset
|
29 |
from cubicweb.mttransforms import ENGINE |
0 | 30 |
|
31 |
_marker = object() |
|
32 |
||
33 |
def greater_card(rschema, subjtypes, objtypes, index): |
|
34 |
for subjtype in subjtypes: |
|
35 |
for objtype in objtypes: |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
36 |
card = rschema.rdef(subjtype, objtype).cardinality[index] |
0 | 37 |
if card in '+*': |
38 |
return card |
|
39 |
return '1' |
|
40 |
||
41 |
||
2656
a93ae0f6c0ad
R [base classes] only AppObject remaning, no more AppRsetObject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
42 |
class Entity(AppObject, dict): |
0 | 43 |
"""an entity instance has e_schema automagically set on |
44 |
the class and instances has access to their issuing cursor. |
|
1474 | 45 |
|
0 | 46 |
A property is set for each attribute and relation on each entity's type |
47 |
class. Becare that among attributes, 'eid' is *NEITHER* stored in the |
|
48 |
dict containment (which acts as a cache for other attributes dynamically |
|
49 |
fetched) |
|
50 |
||
51 |
:type e_schema: `cubicweb.schema.EntitySchema` |
|
52 |
:ivar e_schema: the entity's schema |
|
53 |
||
54 |
:type rest_var: str |
|
55 |
:cvar rest_var: indicates which attribute should be used to build REST urls |
|
56 |
If None is specified, the first non-meta attribute will |
|
57 |
be used |
|
1474 | 58 |
|
0 | 59 |
:type skip_copy_for: list |
60 |
:cvar skip_copy_for: a list of relations that should be skipped when copying |
|
61 |
this kind of entity. Note that some relations such |
|
62 |
as composite relations or relations that have '?1' as object |
|
4965
04543ed0bbdc
[source] only consider edited_attributes in source.preprocess_entity()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4913
diff
changeset
|
63 |
cardinality are always skipped. |
0 | 64 |
""" |
65 |
__registry__ = 'etypes' |
|
717 | 66 |
__select__ = yes() |
1264
fe2934a7df7f
cleanup, avoid spurious warning
sylvain.thenault@logilab.fr
parents:
1177
diff
changeset
|
67 |
|
1474 | 68 |
# class attributes that must be set in class definition |
0 | 69 |
rest_attr = None |
1138
22f634977c95
make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents:
1132
diff
changeset
|
70 |
fetch_attrs = None |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2877
diff
changeset
|
71 |
skip_copy_for = ('in_state',) |
1264
fe2934a7df7f
cleanup, avoid spurious warning
sylvain.thenault@logilab.fr
parents:
1177
diff
changeset
|
72 |
# class attributes set automatically at registration time |
fe2934a7df7f
cleanup, avoid spurious warning
sylvain.thenault@logilab.fr
parents:
1177
diff
changeset
|
73 |
e_schema = None |
1474 | 74 |
|
0 | 75 |
@classmethod |
2807
696ff03f9a58
__initialize__ needs the schema as argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
76 |
def __initialize__(cls, schema): |
0 | 77 |
"""initialize a specific entity class by adding descriptors to access |
78 |
entity type's attributes and relations |
|
79 |
""" |
|
3376
f5c69485381f
[appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3369
diff
changeset
|
80 |
etype = cls.__regid__ |
0 | 81 |
assert etype != 'Any', etype |
2807
696ff03f9a58
__initialize__ needs the schema as argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
82 |
cls.e_schema = eschema = schema.eschema(etype) |
0 | 83 |
for rschema, _ in eschema.attribute_definitions(): |
84 |
if rschema.type == 'eid': |
|
85 |
continue |
|
86 |
setattr(cls, rschema.type, Attribute(rschema.type)) |
|
87 |
mixins = [] |
|
2941
196493bd099c
[entity] use role instead of x
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2931
diff
changeset
|
88 |
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
|
89 |
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
|
90 |
mixin = MI_REL_TRIGGERS[(rschema, role)] |
0 | 91 |
if not (issubclass(cls, mixin) or mixin in mixins): # already mixed ? |
92 |
mixins.append(mixin) |
|
93 |
for iface in getattr(mixin, '__implements__', ()): |
|
94 |
if not interface.implements(cls, iface): |
|
95 |
interface.extend(cls, iface) |
|
2941
196493bd099c
[entity] use role instead of x
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2931
diff
changeset
|
96 |
if role == 'subject': |
0 | 97 |
setattr(cls, rschema.type, SubjectRelation(rschema)) |
98 |
else: |
|
99 |
attr = 'reverse_%s' % rschema.type |
|
100 |
setattr(cls, attr, ObjectRelation(rschema)) |
|
101 |
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
|
102 |
# 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
|
103 |
# 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
|
104 |
# 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
|
105 |
# 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
|
106 |
# 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
|
107 |
# |
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 |
# 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
|
109 |
# 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
|
110 |
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
|
111 |
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
|
112 |
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
|
113 |
cls.info('plugged %s mixins on %s', mixins, cls) |
1474 | 114 |
|
0 | 115 |
@classmethod |
116 |
def fetch_rql(cls, user, restriction=None, fetchattrs=None, mainvar='X', |
|
117 |
settype=True, ordermethod='fetch_order'): |
|
118 |
"""return a rql to fetch all entities of the class type""" |
|
119 |
restrictions = restriction or [] |
|
120 |
if settype: |
|
3376
f5c69485381f
[appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3369
diff
changeset
|
121 |
restrictions.append('%s is %s' % (mainvar, cls.__regid__)) |
0 | 122 |
if fetchattrs is None: |
123 |
fetchattrs = cls.fetch_attrs |
|
124 |
selection = [mainvar] |
|
125 |
orderby = [] |
|
126 |
# start from 26 to avoid possible conflicts with X |
|
127 |
varmaker = rqlvar_maker(index=26) |
|
128 |
cls._fetch_restrictions(mainvar, varmaker, fetchattrs, selection, |
|
129 |
orderby, restrictions, user, ordermethod) |
|
130 |
rql = 'Any %s' % ','.join(selection) |
|
131 |
if orderby: |
|
132 |
rql += ' ORDERBY %s' % ','.join(orderby) |
|
133 |
rql += ' WHERE %s' % ', '.join(restrictions) |
|
134 |
return rql |
|
1474 | 135 |
|
0 | 136 |
@classmethod |
137 |
def _fetch_restrictions(cls, mainvar, varmaker, fetchattrs, |
|
138 |
selection, orderby, restrictions, user, |
|
139 |
ordermethod='fetch_order', visited=None): |
|
140 |
eschema = cls.e_schema |
|
141 |
if visited is None: |
|
142 |
visited = set((eschema.type,)) |
|
143 |
elif eschema.type in visited: |
|
144 |
# avoid infinite recursion |
|
145 |
return |
|
146 |
else: |
|
147 |
visited.add(eschema.type) |
|
148 |
_fetchattrs = [] |
|
149 |
for attr in fetchattrs: |
|
150 |
try: |
|
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3683
diff
changeset
|
151 |
rschema = eschema.subjrels[attr] |
0 | 152 |
except KeyError: |
153 |
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
|
154 |
attr, cls.__regid__) |
0 | 155 |
continue |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
156 |
rdef = eschema.rdef(attr) |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
157 |
if not user.matching_groups(rdef.get_groups('read')): |
0 | 158 |
continue |
159 |
var = varmaker.next() |
|
160 |
selection.append(var) |
|
161 |
restriction = '%s %s %s' % (mainvar, attr, var) |
|
162 |
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
|
163 |
if not rschema.final: |
0 | 164 |
# XXX this does not handle several destination types |
165 |
desttype = rschema.objects(eschema.type)[0] |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
166 |
card = rdef.cardinality[0] |
0 | 167 |
if card not in '?1': |
2928
edfdb69df6e8
oops, not self but cls here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2877
diff
changeset
|
168 |
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
|
169 |
attr, cls) |
0 | 170 |
selection.pop() |
171 |
restrictions.pop() |
|
172 |
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
|
173 |
# 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
|
174 |
# (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
|
175 |
# 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
|
176 |
# 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
|
177 |
restrictions[-1] += '?' |
4049
5838a4b0766d
3.5 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4023
diff
changeset
|
178 |
# 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
|
179 |
destcls = user._cw.vreg['etypes'].etype_class(desttype) |
0 | 180 |
destcls._fetch_restrictions(var, varmaker, destcls.fetch_attrs, |
181 |
selection, orderby, restrictions, |
|
182 |
user, ordermethod, visited=visited) |
|
183 |
orderterm = getattr(cls, ordermethod)(attr, var) |
|
184 |
if orderterm: |
|
185 |
orderby.append(orderterm) |
|
186 |
return selection, orderby, restrictions |
|
187 |
||
1471
c889c3bcf5ec
new parent_classes method (cached)
sylvain.thenault@logilab.fr
parents:
1435
diff
changeset
|
188 |
@classmethod |
c889c3bcf5ec
new parent_classes method (cached)
sylvain.thenault@logilab.fr
parents:
1435
diff
changeset
|
189 |
@cached |
1840
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
190 |
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
|
191 |
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
|
192 |
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
|
193 |
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
|
194 |
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
|
195 |
else: |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
196 |
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
|
197 |
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
|
198 |
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
|
199 |
needcheck = False |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
200 |
break |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
201 |
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
|
202 |
needcheck = False |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
203 |
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
|
204 |
|
5199
ebb50479d2ab
[entity] move req.create_entity content to a cw_instantiate factory method on entity classes, so cube developpers will be able to easily customize instance creation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5144
diff
changeset
|
205 |
@classmethod |
ebb50479d2ab
[entity] move req.create_entity content to a cw_instantiate factory method on entity classes, so cube developpers will be able to easily customize instance creation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5144
diff
changeset
|
206 |
def cw_instantiate(cls, execute, **kwargs): |
ebb50479d2ab
[entity] move req.create_entity content to a cw_instantiate factory method on entity classes, so cube developpers will be able to easily customize instance creation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5144
diff
changeset
|
207 |
"""add a new entity of this given type |
ebb50479d2ab
[entity] move req.create_entity content to a cw_instantiate factory method on entity classes, so cube developpers will be able to easily customize instance creation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5144
diff
changeset
|
208 |
|
ebb50479d2ab
[entity] move req.create_entity content to a cw_instantiate factory method on entity classes, so cube developpers will be able to easily customize instance creation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5144
diff
changeset
|
209 |
Example (in a shell session): |
ebb50479d2ab
[entity] move req.create_entity content to a cw_instantiate factory method on entity classes, so cube developpers will be able to easily customize instance creation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5144
diff
changeset
|
210 |
|
ebb50479d2ab
[entity] move req.create_entity content to a cw_instantiate factory method on entity classes, so cube developpers will be able to easily customize instance creation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5144
diff
changeset
|
211 |
>>> companycls = vreg['etypes'].etype_class(('Company') |
ebb50479d2ab
[entity] move req.create_entity content to a cw_instantiate factory method on entity classes, so cube developpers will be able to easily customize instance creation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5144
diff
changeset
|
212 |
>>> personcls = vreg['etypes'].etype_class(('Person') |
ebb50479d2ab
[entity] move req.create_entity content to a cw_instantiate factory method on entity classes, so cube developpers will be able to easily customize instance creation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5144
diff
changeset
|
213 |
>>> c = companycls.cw_instantiate(req.execute, name=u'Logilab') |
ebb50479d2ab
[entity] move req.create_entity content to a cw_instantiate factory method on entity classes, so cube developpers will be able to easily customize instance creation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5144
diff
changeset
|
214 |
>>> personcls.cw_instantiate(req.execute, firstname=u'John', lastname=u'Doe', |
ebb50479d2ab
[entity] move req.create_entity content to a cw_instantiate factory method on entity classes, so cube developpers will be able to easily customize instance creation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5144
diff
changeset
|
215 |
... works_for=c) |
ebb50479d2ab
[entity] move req.create_entity content to a cw_instantiate factory method on entity classes, so cube developpers will be able to easily customize instance creation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5144
diff
changeset
|
216 |
|
ebb50479d2ab
[entity] move req.create_entity content to a cw_instantiate factory method on entity classes, so cube developpers will be able to easily customize instance creation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5144
diff
changeset
|
217 |
""" |
ebb50479d2ab
[entity] move req.create_entity content to a cw_instantiate factory method on entity classes, so cube developpers will be able to easily customize instance creation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5144
diff
changeset
|
218 |
rql = 'INSERT %s X' % cls.__regid__ |
ebb50479d2ab
[entity] move req.create_entity content to a cw_instantiate factory method on entity classes, so cube developpers will be able to easily customize instance creation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5144
diff
changeset
|
219 |
relations = [] |
ebb50479d2ab
[entity] move req.create_entity content to a cw_instantiate factory method on entity classes, so cube developpers will be able to easily customize instance creation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5144
diff
changeset
|
220 |
restrictions = set() |
ebb50479d2ab
[entity] move req.create_entity content to a cw_instantiate factory method on entity classes, so cube developpers will be able to easily customize instance creation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5144
diff
changeset
|
221 |
pending_relations = [] |
ebb50479d2ab
[entity] move req.create_entity content to a cw_instantiate factory method on entity classes, so cube developpers will be able to easily customize instance creation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5144
diff
changeset
|
222 |
for attr, value in kwargs.items(): |
ebb50479d2ab
[entity] move req.create_entity content to a cw_instantiate factory method on entity classes, so cube developpers will be able to easily customize instance creation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5144
diff
changeset
|
223 |
if isinstance(value, (tuple, list, set, frozenset)): |
ebb50479d2ab
[entity] move req.create_entity content to a cw_instantiate factory method on entity classes, so cube developpers will be able to easily customize instance creation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5144
diff
changeset
|
224 |
if len(value) == 1: |
ebb50479d2ab
[entity] move req.create_entity content to a cw_instantiate factory method on entity classes, so cube developpers will be able to easily customize instance creation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5144
diff
changeset
|
225 |
value = iter(value).next() |
ebb50479d2ab
[entity] move req.create_entity content to a cw_instantiate factory method on entity classes, so cube developpers will be able to easily customize instance creation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5144
diff
changeset
|
226 |
else: |
ebb50479d2ab
[entity] move req.create_entity content to a cw_instantiate factory method on entity classes, so cube developpers will be able to easily customize instance creation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5144
diff
changeset
|
227 |
del kwargs[attr] |
ebb50479d2ab
[entity] move req.create_entity content to a cw_instantiate factory method on entity classes, so cube developpers will be able to easily customize instance creation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5144
diff
changeset
|
228 |
pending_relations.append( (attr, value) ) |
ebb50479d2ab
[entity] move req.create_entity content to a cw_instantiate factory method on entity classes, so cube developpers will be able to easily customize instance creation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5144
diff
changeset
|
229 |
continue |
ebb50479d2ab
[entity] move req.create_entity content to a cw_instantiate factory method on entity classes, so cube developpers will be able to easily customize instance creation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5144
diff
changeset
|
230 |
if hasattr(value, 'eid'): # non final relation |
ebb50479d2ab
[entity] move req.create_entity content to a cw_instantiate factory method on entity classes, so cube developpers will be able to easily customize instance creation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5144
diff
changeset
|
231 |
rvar = attr.upper() |
ebb50479d2ab
[entity] move req.create_entity content to a cw_instantiate factory method on entity classes, so cube developpers will be able to easily customize instance creation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5144
diff
changeset
|
232 |
# XXX safer detection of object relation |
ebb50479d2ab
[entity] move req.create_entity content to a cw_instantiate factory method on entity classes, so cube developpers will be able to easily customize instance creation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5144
diff
changeset
|
233 |
if attr.startswith('reverse_'): |
ebb50479d2ab
[entity] move req.create_entity content to a cw_instantiate factory method on entity classes, so cube developpers will be able to easily customize instance creation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5144
diff
changeset
|
234 |
relations.append('%s %s X' % (rvar, attr[len('reverse_'):])) |
ebb50479d2ab
[entity] move req.create_entity content to a cw_instantiate factory method on entity classes, so cube developpers will be able to easily customize instance creation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5144
diff
changeset
|
235 |
else: |
ebb50479d2ab
[entity] move req.create_entity content to a cw_instantiate factory method on entity classes, so cube developpers will be able to easily customize instance creation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5144
diff
changeset
|
236 |
relations.append('X %s %s' % (attr, rvar)) |
ebb50479d2ab
[entity] move req.create_entity content to a cw_instantiate factory method on entity classes, so cube developpers will be able to easily customize instance creation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5144
diff
changeset
|
237 |
restriction = '%s eid %%(%s)s' % (rvar, attr) |
ebb50479d2ab
[entity] move req.create_entity content to a cw_instantiate factory method on entity classes, so cube developpers will be able to easily customize instance creation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5144
diff
changeset
|
238 |
if not restriction in restrictions: |
ebb50479d2ab
[entity] move req.create_entity content to a cw_instantiate factory method on entity classes, so cube developpers will be able to easily customize instance creation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5144
diff
changeset
|
239 |
restrictions.add(restriction) |
ebb50479d2ab
[entity] move req.create_entity content to a cw_instantiate factory method on entity classes, so cube developpers will be able to easily customize instance creation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5144
diff
changeset
|
240 |
kwargs[attr] = value.eid |
ebb50479d2ab
[entity] move req.create_entity content to a cw_instantiate factory method on entity classes, so cube developpers will be able to easily customize instance creation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5144
diff
changeset
|
241 |
else: # attribute |
ebb50479d2ab
[entity] move req.create_entity content to a cw_instantiate factory method on entity classes, so cube developpers will be able to easily customize instance creation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5144
diff
changeset
|
242 |
relations.append('X %s %%(%s)s' % (attr, attr)) |
ebb50479d2ab
[entity] move req.create_entity content to a cw_instantiate factory method on entity classes, so cube developpers will be able to easily customize instance creation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5144
diff
changeset
|
243 |
if relations: |
ebb50479d2ab
[entity] move req.create_entity content to a cw_instantiate factory method on entity classes, so cube developpers will be able to easily customize instance creation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5144
diff
changeset
|
244 |
rql = '%s: %s' % (rql, ', '.join(relations)) |
ebb50479d2ab
[entity] move req.create_entity content to a cw_instantiate factory method on entity classes, so cube developpers will be able to easily customize instance creation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5144
diff
changeset
|
245 |
if restrictions: |
ebb50479d2ab
[entity] move req.create_entity content to a cw_instantiate factory method on entity classes, so cube developpers will be able to easily customize instance creation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5144
diff
changeset
|
246 |
rql = '%s WHERE %s' % (rql, ', '.join(restrictions)) |
ebb50479d2ab
[entity] move req.create_entity content to a cw_instantiate factory method on entity classes, so cube developpers will be able to easily customize instance creation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5144
diff
changeset
|
247 |
created = execute(rql, kwargs).get_entity(0, 0) |
ebb50479d2ab
[entity] move req.create_entity content to a cw_instantiate factory method on entity classes, so cube developpers will be able to easily customize instance creation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5144
diff
changeset
|
248 |
for attr, values in pending_relations: |
ebb50479d2ab
[entity] move req.create_entity content to a cw_instantiate factory method on entity classes, so cube developpers will be able to easily customize instance creation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5144
diff
changeset
|
249 |
if attr.startswith('reverse_'): |
ebb50479d2ab
[entity] move req.create_entity content to a cw_instantiate factory method on entity classes, so cube developpers will be able to easily customize instance creation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5144
diff
changeset
|
250 |
restr = 'Y %s X' % attr[len('reverse_'):] |
ebb50479d2ab
[entity] move req.create_entity content to a cw_instantiate factory method on entity classes, so cube developpers will be able to easily customize instance creation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5144
diff
changeset
|
251 |
else: |
ebb50479d2ab
[entity] move req.create_entity content to a cw_instantiate factory method on entity classes, so cube developpers will be able to easily customize instance creation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5144
diff
changeset
|
252 |
restr = 'X %s Y' % attr |
ebb50479d2ab
[entity] move req.create_entity content to a cw_instantiate factory method on entity classes, so cube developpers will be able to easily customize instance creation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5144
diff
changeset
|
253 |
execute('SET %s WHERE X eid %%(x)s, Y eid IN (%s)' % ( |
ebb50479d2ab
[entity] move req.create_entity content to a cw_instantiate factory method on entity classes, so cube developpers will be able to easily customize instance creation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5144
diff
changeset
|
254 |
restr, ','.join(str(r.eid) for r in values)), |
ebb50479d2ab
[entity] move req.create_entity content to a cw_instantiate factory method on entity classes, so cube developpers will be able to easily customize instance creation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5144
diff
changeset
|
255 |
{'x': created.eid}, build_descr=False) |
ebb50479d2ab
[entity] move req.create_entity content to a cw_instantiate factory method on entity classes, so cube developpers will be able to easily customize instance creation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5144
diff
changeset
|
256 |
return created |
ebb50479d2ab
[entity] move req.create_entity content to a cw_instantiate factory method on entity classes, so cube developpers will be able to easily customize instance creation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5144
diff
changeset
|
257 |
|
1435
6cd6172718bb
allow to instantiate an entity without rset
sylvain.thenault@logilab.fr
parents:
1363
diff
changeset
|
258 |
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
|
259 |
AppObject.__init__(self, req, rset=rset, row=row, col=col) |
0 | 260 |
dict.__init__(self) |
261 |
self._related_cache = {} |
|
262 |
if rset is not None: |
|
263 |
self.eid = rset[row][col] |
|
264 |
else: |
|
265 |
self.eid = None |
|
266 |
self._is_saved = True |
|
1474 | 267 |
|
0 | 268 |
def __repr__(self): |
269 |
return '<Entity %s %s %s at %s>' % ( |
|
270 |
self.e_schema, self.eid, self.keys(), id(self)) |
|
271 |
||
272 |
def __nonzero__(self): |
|
273 |
return True |
|
274 |
||
275 |
def __hash__(self): |
|
276 |
return id(self) |
|
277 |
||
3047
ba86b1bdbcab
fix __cmp__ arguments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3000
diff
changeset
|
278 |
def __cmp__(self, other): |
3000
4e76477949d5
B [entities] #343385 comparing entities should raise NotImplementedError
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2928
diff
changeset
|
279 |
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
|
280 |
|
4970
1f3d8946ea84
fix security issue introduced by 4967:04543ed0bbdc: attributes explicitly set by hooks should not be checked by security hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
281 |
def __getitem__(self, key): |
1f3d8946ea84
fix security issue introduced by 4967:04543ed0bbdc: attributes explicitly set by hooks should not be checked by security hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
282 |
if key == 'eid': |
1f3d8946ea84
fix security issue introduced by 4967:04543ed0bbdc: attributes explicitly set by hooks should not be checked by security hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
283 |
warn('[3.7] entity["eid"] is deprecated, use entity.eid instead', |
1f3d8946ea84
fix security issue introduced by 4967:04543ed0bbdc: attributes explicitly set by hooks should not be checked by security hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
284 |
DeprecationWarning, stacklevel=2) |
1f3d8946ea84
fix security issue introduced by 4967:04543ed0bbdc: attributes explicitly set by hooks should not be checked by security hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
285 |
return self.eid |
1f3d8946ea84
fix security issue introduced by 4967:04543ed0bbdc: attributes explicitly set by hooks should not be checked by security hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
286 |
return super(Entity, self).__getitem__(key) |
1f3d8946ea84
fix security issue introduced by 4967:04543ed0bbdc: attributes explicitly set by hooks should not be checked by security hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
287 |
|
4965
04543ed0bbdc
[source] only consider edited_attributes in source.preprocess_entity()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4913
diff
changeset
|
288 |
def __setitem__(self, attr, value): |
04543ed0bbdc
[source] only consider edited_attributes in source.preprocess_entity()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4913
diff
changeset
|
289 |
"""override __setitem__ to update self.edited_attributes. |
04543ed0bbdc
[source] only consider edited_attributes in source.preprocess_entity()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4913
diff
changeset
|
290 |
|
4989
0e6dca27422c
[repo entity] we have to provide a __delitem__ implementation as well, see example in the docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4988
diff
changeset
|
291 |
Typically, a before_[update|add]_hook could do:: |
4965
04543ed0bbdc
[source] only consider edited_attributes in source.preprocess_entity()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4913
diff
changeset
|
292 |
|
04543ed0bbdc
[source] only consider edited_attributes in source.preprocess_entity()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4913
diff
changeset
|
293 |
entity['generated_attr'] = generated_value |
04543ed0bbdc
[source] only consider edited_attributes in source.preprocess_entity()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4913
diff
changeset
|
294 |
|
4989
0e6dca27422c
[repo entity] we have to provide a __delitem__ implementation as well, see example in the docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4988
diff
changeset
|
295 |
and this way, edited_attributes will be updated accordingly. Also, add |
0e6dca27422c
[repo entity] we have to provide a __delitem__ implementation as well, see example in the docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4988
diff
changeset
|
296 |
the attribute to skip_security since we don't want to check security |
0e6dca27422c
[repo entity] we have to provide a __delitem__ implementation as well, see example in the docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4988
diff
changeset
|
297 |
for such attributes set by hooks. |
4965
04543ed0bbdc
[source] only consider edited_attributes in source.preprocess_entity()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4913
diff
changeset
|
298 |
""" |
04543ed0bbdc
[source] only consider edited_attributes in source.preprocess_entity()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4913
diff
changeset
|
299 |
if attr == 'eid': |
04543ed0bbdc
[source] only consider edited_attributes in source.preprocess_entity()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4913
diff
changeset
|
300 |
warn('[3.7] entity["eid"] = value is deprecated, use entity.eid = value instead', |
04543ed0bbdc
[source] only consider edited_attributes in source.preprocess_entity()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4913
diff
changeset
|
301 |
DeprecationWarning, stacklevel=2) |
04543ed0bbdc
[source] only consider edited_attributes in source.preprocess_entity()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4913
diff
changeset
|
302 |
self.eid = value |
04543ed0bbdc
[source] only consider edited_attributes in source.preprocess_entity()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4913
diff
changeset
|
303 |
else: |
04543ed0bbdc
[source] only consider edited_attributes in source.preprocess_entity()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4913
diff
changeset
|
304 |
super(Entity, self).__setitem__(attr, value) |
4990
3bf481cf51cb
[security] don't add attribute into skip_security if already in edited_attributes, else we may accidentaly skip a desired security check
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4989
diff
changeset
|
305 |
# don't add attribute into skip_security if already in edited |
3bf481cf51cb
[security] don't add attribute into skip_security if already in edited_attributes, else we may accidentaly skip a desired security check
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4989
diff
changeset
|
306 |
# attributes, else we may accidentaly skip a desired security check |
3bf481cf51cb
[security] don't add attribute into skip_security if already in edited_attributes, else we may accidentaly skip a desired security check
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4989
diff
changeset
|
307 |
if hasattr(self, 'edited_attributes') and \ |
3bf481cf51cb
[security] don't add attribute into skip_security if already in edited_attributes, else we may accidentaly skip a desired security check
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4989
diff
changeset
|
308 |
attr not in self.edited_attributes: |
4965
04543ed0bbdc
[source] only consider edited_attributes in source.preprocess_entity()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4913
diff
changeset
|
309 |
self.edited_attributes.add(attr) |
4970
1f3d8946ea84
fix security issue introduced by 4967:04543ed0bbdc: attributes explicitly set by hooks should not be checked by security hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
310 |
self.skip_security_attributes.add(attr) |
4965
04543ed0bbdc
[source] only consider edited_attributes in source.preprocess_entity()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4913
diff
changeset
|
311 |
|
4989
0e6dca27422c
[repo entity] we have to provide a __delitem__ implementation as well, see example in the docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4988
diff
changeset
|
312 |
def __delitem__(self, attr): |
0e6dca27422c
[repo entity] we have to provide a __delitem__ implementation as well, see example in the docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4988
diff
changeset
|
313 |
"""override __delitem__ to update self.edited_attributes on cleanup of |
0e6dca27422c
[repo entity] we have to provide a __delitem__ implementation as well, see example in the docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4988
diff
changeset
|
314 |
undesired changes introduced in the entity's dict. For example, see the |
0e6dca27422c
[repo entity] we have to provide a __delitem__ implementation as well, see example in the docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4988
diff
changeset
|
315 |
code snippet below from the `forge` cube: |
0e6dca27422c
[repo entity] we have to provide a __delitem__ implementation as well, see example in the docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4988
diff
changeset
|
316 |
|
0e6dca27422c
[repo entity] we have to provide a __delitem__ implementation as well, see example in the docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4988
diff
changeset
|
317 |
.. sourcecode:: python |
0e6dca27422c
[repo entity] we have to provide a __delitem__ implementation as well, see example in the docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4988
diff
changeset
|
318 |
|
0e6dca27422c
[repo entity] we have to provide a __delitem__ implementation as well, see example in the docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4988
diff
changeset
|
319 |
edited = self.entity.edited_attributes |
0e6dca27422c
[repo entity] we have to provide a __delitem__ implementation as well, see example in the docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4988
diff
changeset
|
320 |
has_load_left = 'load_left' in edited |
0e6dca27422c
[repo entity] we have to provide a __delitem__ implementation as well, see example in the docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4988
diff
changeset
|
321 |
if 'load' in edited and self.entity.load_left is None: |
0e6dca27422c
[repo entity] we have to provide a __delitem__ implementation as well, see example in the docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4988
diff
changeset
|
322 |
self.entity.load_left = self.entity['load'] |
0e6dca27422c
[repo entity] we have to provide a __delitem__ implementation as well, see example in the docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4988
diff
changeset
|
323 |
elif not has_load_left and edited: |
0e6dca27422c
[repo entity] we have to provide a __delitem__ implementation as well, see example in the docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4988
diff
changeset
|
324 |
# cleanup, this may cause undesired changes |
0e6dca27422c
[repo entity] we have to provide a __delitem__ implementation as well, see example in the docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4988
diff
changeset
|
325 |
del self.entity['load_left'] |
0e6dca27422c
[repo entity] we have to provide a __delitem__ implementation as well, see example in the docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4988
diff
changeset
|
326 |
|
0e6dca27422c
[repo entity] we have to provide a __delitem__ implementation as well, see example in the docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4988
diff
changeset
|
327 |
""" |
0e6dca27422c
[repo entity] we have to provide a __delitem__ implementation as well, see example in the docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4988
diff
changeset
|
328 |
super(Entity, self).__delitem__(attr) |
0e6dca27422c
[repo entity] we have to provide a __delitem__ implementation as well, see example in the docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4988
diff
changeset
|
329 |
if hasattr(self, 'edited_attributes'): |
0e6dca27422c
[repo entity] we have to provide a __delitem__ implementation as well, see example in the docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4988
diff
changeset
|
330 |
self.edited_attributes.remove(attr) |
0e6dca27422c
[repo entity] we have to provide a __delitem__ implementation as well, see example in the docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4988
diff
changeset
|
331 |
|
4970
1f3d8946ea84
fix security issue introduced by 4967:04543ed0bbdc: attributes explicitly set by hooks should not be checked by security hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
332 |
def setdefault(self, attr, default): |
1f3d8946ea84
fix security issue introduced by 4967:04543ed0bbdc: attributes explicitly set by hooks should not be checked by security hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
333 |
"""override setdefault to update self.edited_attributes""" |
1f3d8946ea84
fix security issue introduced by 4967:04543ed0bbdc: attributes explicitly set by hooks should not be checked by security hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
334 |
super(Entity, self).setdefault(attr, default) |
4990
3bf481cf51cb
[security] don't add attribute into skip_security if already in edited_attributes, else we may accidentaly skip a desired security check
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4989
diff
changeset
|
335 |
# don't add attribute into skip_security if already in edited |
3bf481cf51cb
[security] don't add attribute into skip_security if already in edited_attributes, else we may accidentaly skip a desired security check
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4989
diff
changeset
|
336 |
# attributes, else we may accidentaly skip a desired security check |
3bf481cf51cb
[security] don't add attribute into skip_security if already in edited_attributes, else we may accidentaly skip a desired security check
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4989
diff
changeset
|
337 |
if hasattr(self, 'edited_attributes') and \ |
3bf481cf51cb
[security] don't add attribute into skip_security if already in edited_attributes, else we may accidentaly skip a desired security check
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4989
diff
changeset
|
338 |
attr not in self.edited_attributes: |
4970
1f3d8946ea84
fix security issue introduced by 4967:04543ed0bbdc: attributes explicitly set by hooks should not be checked by security hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
339 |
self.edited_attributes.add(attr) |
1f3d8946ea84
fix security issue introduced by 4967:04543ed0bbdc: attributes explicitly set by hooks should not be checked by security hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
340 |
self.skip_security_attributes.add(attr) |
4965
04543ed0bbdc
[source] only consider edited_attributes in source.preprocess_entity()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4913
diff
changeset
|
341 |
|
5009
2ac04bc976c3
[entity] we also have to override dict.pop to properly maintain edited_attributes consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4991
diff
changeset
|
342 |
def pop(self, attr, default=_marker): |
2ac04bc976c3
[entity] we also have to override dict.pop to properly maintain edited_attributes consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4991
diff
changeset
|
343 |
"""override pop to update self.edited_attributes on cleanup of |
2ac04bc976c3
[entity] we also have to override dict.pop to properly maintain edited_attributes consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4991
diff
changeset
|
344 |
undesired changes introduced in the entity's dict. See `__delitem__` |
2ac04bc976c3
[entity] we also have to override dict.pop to properly maintain edited_attributes consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4991
diff
changeset
|
345 |
""" |
2ac04bc976c3
[entity] we also have to override dict.pop to properly maintain edited_attributes consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4991
diff
changeset
|
346 |
if default is _marker: |
2ac04bc976c3
[entity] we also have to override dict.pop to properly maintain edited_attributes consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4991
diff
changeset
|
347 |
value = super(Entity, self).pop(attr) |
2ac04bc976c3
[entity] we also have to override dict.pop to properly maintain edited_attributes consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4991
diff
changeset
|
348 |
else: |
2ac04bc976c3
[entity] we also have to override dict.pop to properly maintain edited_attributes consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4991
diff
changeset
|
349 |
value = super(Entity, self).pop(attr, default) |
2ac04bc976c3
[entity] we also have to override dict.pop to properly maintain edited_attributes consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4991
diff
changeset
|
350 |
if hasattr(self, 'edited_attributes') and attr in self.edited_attributes: |
2ac04bc976c3
[entity] we also have to override dict.pop to properly maintain edited_attributes consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4991
diff
changeset
|
351 |
self.edited_attributes.remove(attr) |
2ac04bc976c3
[entity] we also have to override dict.pop to properly maintain edited_attributes consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4991
diff
changeset
|
352 |
return value |
2ac04bc976c3
[entity] we also have to override dict.pop to properly maintain edited_attributes consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4991
diff
changeset
|
353 |
|
5115
2e43ef618d14
[repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5009
diff
changeset
|
354 |
def update(self, values): |
2e43ef618d14
[repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5009
diff
changeset
|
355 |
"""override update to update self.edited_attributes. See `__setitem__` |
2e43ef618d14
[repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5009
diff
changeset
|
356 |
""" |
2e43ef618d14
[repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5009
diff
changeset
|
357 |
for attr, value in values.items(): |
2e43ef618d14
[repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5009
diff
changeset
|
358 |
self[attr] = value # use self.__setitem__ implementation |
2e43ef618d14
[repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5009
diff
changeset
|
359 |
|
4970
1f3d8946ea84
fix security issue introduced by 4967:04543ed0bbdc: attributes explicitly set by hooks should not be checked by security hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
360 |
def rql_set_value(self, attr, value): |
1f3d8946ea84
fix security issue introduced by 4967:04543ed0bbdc: attributes explicitly set by hooks should not be checked by security hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
361 |
"""call by rql execution plan when some attribute is modified |
1f3d8946ea84
fix security issue introduced by 4967:04543ed0bbdc: attributes explicitly set by hooks should not be checked by security hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
362 |
|
1f3d8946ea84
fix security issue introduced by 4967:04543ed0bbdc: attributes explicitly set by hooks should not be checked by security hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
363 |
don't use dict api in such case since we don't want attribute to be |
1f3d8946ea84
fix security issue introduced by 4967:04543ed0bbdc: attributes explicitly set by hooks should not be checked by security hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
364 |
added to skip_security_attributes. |
1f3d8946ea84
fix security issue introduced by 4967:04543ed0bbdc: attributes explicitly set by hooks should not be checked by security hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
365 |
""" |
1f3d8946ea84
fix security issue introduced by 4967:04543ed0bbdc: attributes explicitly set by hooks should not be checked by security hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
366 |
super(Entity, self).__setitem__(attr, value) |
4965
04543ed0bbdc
[source] only consider edited_attributes in source.preprocess_entity()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4913
diff
changeset
|
367 |
|
0 | 368 |
def pre_add_hook(self): |
369 |
"""hook called by the repository before doing anything to add the entity |
|
370 |
(before_add entity hooks have not been called yet). This give the |
|
371 |
occasion to do weird stuff such as autocast (File -> Image for instance). |
|
1474 | 372 |
|
0 | 373 |
This method must return the actual entity to be added. |
374 |
""" |
|
375 |
return self |
|
1474 | 376 |
|
0 | 377 |
def set_eid(self, eid): |
4965
04543ed0bbdc
[source] only consider edited_attributes in source.preprocess_entity()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4913
diff
changeset
|
378 |
self.eid = eid |
0 | 379 |
|
380 |
def has_eid(self): |
|
381 |
"""return True if the entity has an attributed eid (False |
|
382 |
meaning that the entity has to be created |
|
383 |
""" |
|
384 |
try: |
|
3664
af7ca3597b8d
use typed_eid
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3626
diff
changeset
|
385 |
typed_eid(self.eid) |
0 | 386 |
return True |
387 |
except (ValueError, TypeError): |
|
388 |
return False |
|
389 |
||
390 |
def is_saved(self): |
|
391 |
"""during entity creation, there is some time during which the entity |
|
392 |
has an eid attributed though it's not saved (eg during before_add_entity |
|
393 |
hooks). You can use this method to ensure the entity has an eid *and* is |
|
394 |
saved in its source. |
|
395 |
""" |
|
396 |
return self.has_eid() and self._is_saved |
|
1474 | 397 |
|
0 | 398 |
@cached |
399 |
def metainformation(self): |
|
3378
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
400 |
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
|
401 |
res['source'] = self._cw.source_defs()[res['source']] |
0 | 402 |
return res |
403 |
||
475
b32a5772ff06
should clear local perm cache if first attempt failed
sylvain.thenault@logilab.fr
parents:
413
diff
changeset
|
404 |
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
|
405 |
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
|
406 |
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
|
407 |
|
0 | 408 |
def check_perm(self, action): |
3890
d7a270f50f54
backport stable branch (one more time painfully)
Sylvain Thénault <sylvain.thenault@logilab.fr>
diff
changeset
|
409 |
self.e_schema.check_perm(self._cw, action, eid=self.eid) |
0 | 410 |
|
411 |
def has_perm(self, action): |
|
3890
d7a270f50f54
backport stable branch (one more time painfully)
Sylvain Thénault <sylvain.thenault@logilab.fr>
diff
changeset
|
412 |
return self.e_schema.has_perm(self._cw, action, eid=self.eid) |
1474 | 413 |
|
4406
b3f61c38526b
[entity] rename vid argument of .view(...) into __vid to avoid potential conflicts with kwargs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
414 |
def view(self, __vid, __registry='views', **kwargs): |
0 | 415 |
"""shortcut to apply a view on this entity""" |
4451
65f4665233e6
misc typos found on my way: NameError / SyntaxError / double __select__, <sigh ...>
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4436
diff
changeset
|
416 |
view = self._cw.vreg[__registry].select(__vid, self._cw, rset=self.cw_rset, |
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
|
417 |
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
|
418 |
**kwargs) |
3418
7b49fa7e942d
[api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3405
diff
changeset
|
419 |
return view.render(row=self.cw_row, col=self.cw_col, **kwargs) |
0 | 420 |
|
2059
af33833d7571
absolute_url / build_url refactoring to avoid potential name clash
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2058
diff
changeset
|
421 |
def absolute_url(self, *args, **kwargs): |
0 | 422 |
"""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
|
423 |
# 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
|
424 |
# 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
|
425 |
if args: |
af33833d7571
absolute_url / build_url refactoring to avoid potential name clash
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2058
diff
changeset
|
426 |
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
|
427 |
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
|
428 |
else: |
af33833d7571
absolute_url / build_url refactoring to avoid potential name clash
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2058
diff
changeset
|
429 |
method = None |
0 | 430 |
# in linksearch mode, we don't want external urls else selecting |
431 |
# the object for use in the relation is tricky |
|
432 |
# 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
|
433 |
if getattr(self._cw, 'search_state', ('normal',))[0] == 'normal': |
0 | 434 |
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
|
435 |
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
|
436 |
try: |
c7a22540d6f7
add some bw compat code for old rest_path prototype
sylvain.thenault@logilab.fr
parents:
1840
diff
changeset
|
437 |
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
|
438 |
except TypeError: |
3405
9d31c9cb8103
nicer deprecation warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3382
diff
changeset
|
439 |
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
|
440 |
'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
|
441 |
kwargs['_restpath'] = self.rest_path() |
0 | 442 |
else: |
443 |
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
|
444 |
return self._cw.build_url(method, **kwargs) |
0 | 445 |
|
1840
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
446 |
def rest_path(self, use_ext_eid=False): |
0 | 447 |
"""returns a REST-like (relative) path for this entity""" |
448 |
mainattr, needcheck = self._rest_attr_info() |
|
449 |
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
|
450 |
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
|
451 |
if mainattr != 'eid': |
0 | 452 |
value = getattr(self, mainattr) |
2125
19861294506f
https://www.logilab.net/cwo/ticket/343724
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2005
diff
changeset
|
453 |
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
|
454 |
mainattr = 'eid' |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
455 |
path += '/eid' |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
456 |
elif needcheck: |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
457 |
# 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
|
458 |
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
|
459 |
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
|
460 |
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
|
461 |
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
|
462 |
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
|
463 |
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
|
464 |
if nbresults != 1: # ambiguity? |
e23536d29231
minor refactoring of absolute_url()
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1842
diff
changeset
|
465 |
mainattr = 'eid' |
e23536d29231
minor refactoring of absolute_url()
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1842
diff
changeset
|
466 |
path += '/eid' |
0 | 467 |
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
|
468 |
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
|
469 |
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
|
470 |
else: |
f4b5c15d1147
test and fix #342997: local eid used for absolute_url of external entities
sylvain.thenault@logilab.fr
parents:
1804
diff
changeset
|
471 |
value = self.eid |
3378
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
472 |
return '%s/%s' % (path, self._cw.url_quote(value)) |
0 | 473 |
|
1360
13ae1121835e
rename attribute_metadata method to attr_metadata to save a few chars
sylvain.thenault@logilab.fr
parents:
1313
diff
changeset
|
474 |
def attr_metadata(self, attr, metadata): |
1101
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
1098
diff
changeset
|
475 |
"""return a metadata for an attribute (None if unspecified)""" |
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
1098
diff
changeset
|
476 |
value = getattr(self, '%s_%s' % (attr, metadata), None) |
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
1098
diff
changeset
|
477 |
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
|
478 |
value = self._cw.vreg.property_value('ui.encoding') |
1101
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
1098
diff
changeset
|
479 |
return value |
0 | 480 |
|
481 |
def printable_value(self, attr, value=_marker, attrtype=None, |
|
482 |
format='text/html', displaytime=True): |
|
483 |
"""return a displayable value (i.e. unicode string) which may contains |
|
484 |
html tags |
|
485 |
""" |
|
486 |
attr = str(attr) |
|
487 |
if value is _marker: |
|
488 |
value = getattr(self, attr) |
|
489 |
if isinstance(value, basestring): |
|
490 |
value = value.strip() |
|
491 |
if value is None or value == '': # don't use "not", 0 is an acceptable value |
|
492 |
return u'' |
|
493 |
if attrtype is None: |
|
494 |
attrtype = self.e_schema.destination(attr) |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
495 |
props = self.e_schema.rdef(attr) |
0 | 496 |
if attrtype == 'String': |
497 |
# internalinalized *and* formatted string such as schema |
|
498 |
# description... |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
499 |
if props.internationalizable: |
3378
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
500 |
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
|
501 |
attrformat = self.attr_metadata(attr, 'format') |
0 | 502 |
if attrformat: |
503 |
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
|
504 |
self._cw.encoding) |
0 | 505 |
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
|
506 |
attrformat = self.attr_metadata(attr, 'format') |
0 | 507 |
if attrformat: |
1360
13ae1121835e
rename attribute_metadata method to attr_metadata to save a few chars
sylvain.thenault@logilab.fr
parents:
1313
diff
changeset
|
508 |
encoding = self.attr_metadata(attr, 'encoding') |
0 | 509 |
return self.mtc_transform(value.getvalue(), attrformat, format, |
510 |
encoding) |
|
511 |
return u'' |
|
3378
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
512 |
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
|
513 |
displaytime=displaytime) |
0 | 514 |
if format == 'text/html': |
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2125
diff
changeset
|
515 |
value = xml_escape(value) |
0 | 516 |
return value |
517 |
||
518 |
def mtc_transform(self, data, format, target_format, encoding, |
|
519 |
_engine=ENGINE): |
|
520 |
trdata = TransformData(data, format, encoding, appobject=self) |
|
521 |
data = _engine.convert(trdata, target_format).decode() |
|
522 |
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
|
523 |
data = soup2xhtml(data, self._cw.encoding) |
0 | 524 |
return data |
1474 | 525 |
|
0 | 526 |
# entity cloning ########################################################## |
527 |
||
528 |
def copy_relations(self, ceid): |
|
3626
017869a514c3
[doc] updated docstrings
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
3552
diff
changeset
|
529 |
"""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
|
530 |
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
|
531 |
ceid designates the original entity). |
0 | 532 |
|
533 |
By default meta and composite relations are skipped. |
|
534 |
Overrides this if you want another behaviour |
|
535 |
""" |
|
536 |
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
|
537 |
execute = self._cw.execute |
0 | 538 |
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
|
539 |
if rschema.final or rschema.meta: |
0 | 540 |
continue |
541 |
# skip already defined relations |
|
542 |
if getattr(self, rschema.type): |
|
543 |
continue |
|
544 |
if rschema.type in self.skip_copy_for: |
|
545 |
continue |
|
546 |
# skip composite relation |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
547 |
rdef = self.e_schema.rdef(rschema) |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
548 |
if rdef.composite: |
0 | 549 |
continue |
550 |
# skip relation with card in ?1 else we either change the copied |
|
551 |
# object (inlined relation) or inserting some inconsistency |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
552 |
if rdef.cardinality[1] in '?1': |
0 | 553 |
continue |
554 |
rql = 'SET X %s V WHERE X eid %%(x)s, Y eid %%(y)s, Y %s V' % ( |
|
555 |
rschema.type, rschema.type) |
|
556 |
execute(rql, {'x': self.eid, 'y': ceid}, ('x', 'y')) |
|
557 |
self.clear_related_cache(rschema.type, 'subject') |
|
558 |
for rschema in self.e_schema.object_relations(): |
|
559 |
if rschema.meta: |
|
560 |
continue |
|
561 |
# skip already defined relations |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
562 |
if self.related(rschema.type, 'object'): |
0 | 563 |
continue |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
564 |
rdef = self.e_schema.rdef(rschema, 'object') |
0 | 565 |
# skip composite relation |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
566 |
if rdef.composite: |
0 | 567 |
continue |
568 |
# skip relation with card in ?1 else we either change the copied |
|
569 |
# object (inlined relation) or inserting some inconsistency |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
570 |
if rdef.cardinality[0] in '?1': |
0 | 571 |
continue |
572 |
rql = 'SET V %s X WHERE X eid %%(x)s, Y eid %%(y)s, V %s Y' % ( |
|
573 |
rschema.type, rschema.type) |
|
574 |
execute(rql, {'x': self.eid, 'y': ceid}, ('x', 'y')) |
|
575 |
self.clear_related_cache(rschema.type, 'object') |
|
576 |
||
577 |
# data fetching methods ################################################### |
|
578 |
||
579 |
@cached |
|
580 |
def as_rset(self): |
|
581 |
"""returns a resultset containing `self` information""" |
|
582 |
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
|
583 |
{'x': self.eid}, [(self.__regid__,)]) |
4850
bd640b137f50
[refactor] drop rset.vreg attribute, vreg should be accessed through rset.req. Also kill decorate_rset, simply set rset.req where we were calling this method.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4848
diff
changeset
|
584 |
rset.req = self._cw |
bd640b137f50
[refactor] drop rset.vreg attribute, vreg should be accessed through rset.req. Also kill decorate_rset, simply set rset.req where we were calling this method.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4848
diff
changeset
|
585 |
return rset |
1474 | 586 |
|
0 | 587 |
def to_complete_relations(self): |
588 |
"""by default complete final relations to when calling .complete()""" |
|
589 |
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
|
590 |
if rschema.final: |
0 | 591 |
continue |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
592 |
targets = rschema.objects(self.e_schema) |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
593 |
if len(targets) > 1: |
0 | 594 |
# ambigous relations, the querier doesn't handle |
595 |
# outer join correctly in this case |
|
596 |
continue |
|
597 |
if rschema.inlined: |
|
3378
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
598 |
matching_groups = self._cw.user.matching_groups |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
599 |
rdef = rschema.rdef(self.e_schema, targets[0]) |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
600 |
if matching_groups(rdef.get_groups('read')) and \ |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
601 |
all(matching_groups(e.get_groups('read')) for e in targets): |
0 | 602 |
yield rschema, 'subject' |
1474 | 603 |
|
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4850
diff
changeset
|
604 |
def to_complete_attributes(self, skip_bytes=True, skip_pwd=True): |
0 | 605 |
for rschema, attrschema in self.e_schema.attribute_definitions(): |
606 |
# skip binary data by default |
|
607 |
if skip_bytes and attrschema.type == 'Bytes': |
|
608 |
continue |
|
609 |
attr = rschema.type |
|
610 |
if attr == 'eid': |
|
611 |
continue |
|
612 |
# password retreival is blocked at the repository server level |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
613 |
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
|
614 |
if not self._cw.user.matching_groups(rdef.get_groups('read')) \ |
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4850
diff
changeset
|
615 |
or (attrschema.type == 'Password' and skip_pwd): |
0 | 616 |
self[attr] = None |
617 |
continue |
|
618 |
yield attr |
|
1474 | 619 |
|
4408
899b426087ab
[entity] small optimization: once an entity has been completed, don't redo it (for nothing)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4406
diff
changeset
|
620 |
_cw_completed = False |
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4850
diff
changeset
|
621 |
def complete(self, attributes=None, skip_bytes=True, skip_pwd=True): |
0 | 622 |
"""complete this entity by adding missing attributes (i.e. query the |
623 |
repository to fill the entity) |
|
624 |
||
625 |
:type skip_bytes: bool |
|
626 |
:param skip_bytes: |
|
627 |
if true, attribute of type Bytes won't be considered |
|
628 |
""" |
|
629 |
assert self.has_eid() |
|
4408
899b426087ab
[entity] small optimization: once an entity has been completed, don't redo it (for nothing)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4406
diff
changeset
|
630 |
if self._cw_completed: |
899b426087ab
[entity] small optimization: once an entity has been completed, don't redo it (for nothing)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4406
diff
changeset
|
631 |
return |
899b426087ab
[entity] small optimization: once an entity has been completed, don't redo it (for nothing)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4406
diff
changeset
|
632 |
if attributes is None: |
899b426087ab
[entity] small optimization: once an entity has been completed, don't redo it (for nothing)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4406
diff
changeset
|
633 |
self._cw_completed = True |
0 | 634 |
varmaker = rqlvar_maker() |
635 |
V = varmaker.next() |
|
636 |
rql = ['WHERE %s eid %%(x)s' % V] |
|
637 |
selected = [] |
|
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4850
diff
changeset
|
638 |
for attr in (attributes or self.to_complete_attributes(skip_bytes, skip_pwd)): |
0 | 639 |
# if attribute already in entity, nothing to do |
640 |
if self.has_key(attr): |
|
641 |
continue |
|
642 |
# case where attribute must be completed, but is not yet in entity |
|
643 |
var = varmaker.next() |
|
644 |
rql.append('%s %s %s' % (V, attr, var)) |
|
645 |
selected.append((attr, var)) |
|
646 |
# +1 since this doen't include the main variable |
|
647 |
lastattr = len(selected) + 1 |
|
648 |
if attributes is None: |
|
649 |
# fetch additional relations (restricted to 0..1 relations) |
|
650 |
for rschema, role in self.to_complete_relations(): |
|
651 |
rtype = rschema.type |
|
652 |
if self.relation_cached(rtype, role): |
|
653 |
continue |
|
654 |
var = varmaker.next() |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
655 |
targettype = rschema.targets(self.e_schema, role)[0] |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
656 |
rdef = rschema.role_rdef(self.e_schema, targettype, role) |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
657 |
card = rdef.role_cardinality(role) |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
658 |
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
|
659 |
role, card) |
0 | 660 |
if role == 'subject': |
661 |
if card == '1': |
|
662 |
rql.append('%s %s %s' % (V, rtype, var)) |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
663 |
else: |
0 | 664 |
rql.append('%s %s %s?' % (V, rtype, var)) |
665 |
else: |
|
666 |
if card == '1': |
|
667 |
rql.append('%s %s %s' % (var, rtype, V)) |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
668 |
else: |
0 | 669 |
rql.append('%s? %s %s' % (var, rtype, V)) |
670 |
selected.append(((rtype, role), var)) |
|
671 |
if selected: |
|
672 |
# select V, we need it as the left most selected variable |
|
673 |
# if some outer join are included to fetch inlined relations |
|
674 |
rql = 'Any %s,%s %s' % (V, ','.join(var for attr, var in selected), |
|
675 |
','.join(rql)) |
|
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4831
diff
changeset
|
676 |
rset = self._cw.execute(rql, {'x': self.eid}, 'x', |
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4831
diff
changeset
|
677 |
build_descr=False)[0] |
0 | 678 |
# handle attributes |
679 |
for i in xrange(1, lastattr): |
|
680 |
self[str(selected[i-1][0])] = rset[i] |
|
681 |
# handle relations |
|
682 |
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
|
683 |
rtype, role = selected[i-1][0] |
0 | 684 |
value = rset[i] |
685 |
if value is None: |
|
686 |
rrset = ResultSet([], rql, {'x': self.eid}) |
|
4850
bd640b137f50
[refactor] drop rset.vreg attribute, vreg should be accessed through rset.req. Also kill decorate_rset, simply set rset.req where we were calling this method.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4848
diff
changeset
|
687 |
rrset.req = self._cw |
0 | 688 |
else: |
3378
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
689 |
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
|
690 |
self.set_related_cache(rtype, role, rrset) |
1474 | 691 |
|
0 | 692 |
def get_value(self, name): |
693 |
"""get value for the attribute relation <name>, query the repository |
|
694 |
to get the value if necessary. |
|
695 |
||
696 |
:type name: str |
|
697 |
:param name: name of the attribute to get |
|
698 |
""" |
|
699 |
try: |
|
700 |
value = self[name] |
|
701 |
except KeyError: |
|
702 |
if not self.is_saved(): |
|
703 |
return None |
|
704 |
rql = "Any A WHERE X eid %%(x)s, X %s A" % name |
|
705 |
try: |
|
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4831
diff
changeset
|
706 |
rset = self._cw.execute(rql, {'x': self.eid}, 'x') |
0 | 707 |
except Unauthorized: |
708 |
self[name] = value = None |
|
709 |
else: |
|
710 |
assert rset.rowcount <= 1, (self, rql, rset.rowcount) |
|
711 |
try: |
|
712 |
self[name] = value = rset.rows[0][0] |
|
713 |
except IndexError: |
|
714 |
# probably a multisource error |
|
715 |
self.critical("can't get value for attribute %s of entity with eid %s", |
|
716 |
name, self.eid) |
|
717 |
if self.e_schema.destination(name) == 'String': |
|
2320 | 718 |
# 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
|
719 |
self[name] = value = self._cw._('unaccessible') |
0 | 720 |
else: |
721 |
self[name] = value = None |
|
722 |
return value |
|
723 |
||
724 |
def related(self, rtype, role='subject', limit=None, entities=False): |
|
725 |
"""returns a resultset of related entities |
|
1474 | 726 |
|
0 | 727 |
:param role: is the role played by 'self' in the relation ('subject' or 'object') |
728 |
:param limit: resultset's maximum size |
|
729 |
:param entities: if True, the entites are returned; if False, a result set is returned |
|
730 |
""" |
|
731 |
try: |
|
732 |
return self.related_cache(rtype, role, entities, limit) |
|
733 |
except KeyError: |
|
734 |
pass |
|
735 |
assert self.has_eid() |
|
736 |
rql = self.related_rql(rtype, role) |
|
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4831
diff
changeset
|
737 |
rset = self._cw.execute(rql, {'x': self.eid}, 'x') |
0 | 738 |
self.set_related_cache(rtype, role, rset) |
739 |
return self.related(rtype, role, limit, entities) |
|
740 |
||
3130
2486163c4630
allow to specify targettypes in related_rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3047
diff
changeset
|
741 |
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
|
742 |
rschema = self._cw.vreg.schema[rtype] |
0 | 743 |
if role == 'subject': |
3672
554a588ffaea
[entity] make related_rql honors its targettypes argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3671
diff
changeset
|
744 |
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
|
745 |
if targettypes is None: |
2486163c4630
allow to specify targettypes in related_rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3047
diff
changeset
|
746 |
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
|
747 |
else: |
3683
2e4794c97cf4
fix related_rql when target_type specified
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
3680
diff
changeset
|
748 |
restriction += ', X is IN (%s)' % ','.join(targettypes) |
0 | 749 |
card = greater_card(rschema, (self.e_schema,), targettypes, 0) |
750 |
else: |
|
3672
554a588ffaea
[entity] make related_rql honors its targettypes argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3671
diff
changeset
|
751 |
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
|
752 |
if targettypes is None: |
2486163c4630
allow to specify targettypes in related_rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3047
diff
changeset
|
753 |
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
|
754 |
else: |
3683
2e4794c97cf4
fix related_rql when target_type specified
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
3680
diff
changeset
|
755 |
restriction += ', X is IN (%s)' % ','.join(targettypes) |
0 | 756 |
card = greater_card(rschema, targettypes, (self.e_schema,), 1) |
757 |
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
|
758 |
fetchattrs_list = [] |
0 | 759 |
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
|
760 |
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
|
761 |
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
|
762 |
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
|
763 |
rql = etypecls.fetch_rql(self._cw.user, [restriction], fetchattrs, |
0 | 764 |
settype=False) |
765 |
else: |
|
3378
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
766 |
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
|
767 |
rql = etypecls.fetch_rql(self._cw.user, [restriction], settype=False) |
0 | 768 |
# optimisation: remove ORDERBY if cardinality is 1 or ? (though |
769 |
# greater_card return 1 for those both cases) |
|
770 |
if card == '1': |
|
771 |
if ' ORDERBY ' in rql: |
|
772 |
rql = '%s WHERE %s' % (rql.split(' ORDERBY ', 1)[0], |
|
773 |
rql.split(' WHERE ', 1)[1]) |
|
774 |
elif not ' ORDERBY ' in rql: |
|
4630
528dee042927
fix #694445: related entity generates weird RQL which in turn generates weird SQL which fails on SQL Server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4460
diff
changeset
|
775 |
args = rql.split(' WHERE ', 1) |
528dee042927
fix #694445: related entity generates weird RQL which in turn generates weird SQL which fails on SQL Server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4460
diff
changeset
|
776 |
# if modification_date already retreived, we should use it instead |
528dee042927
fix #694445: related entity generates weird RQL which in turn generates weird SQL which fails on SQL Server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4460
diff
changeset
|
777 |
# of adding another variable for sort. This should be be problematic |
528dee042927
fix #694445: related entity generates weird RQL which in turn generates weird SQL which fails on SQL Server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4460
diff
changeset
|
778 |
# but it's actually with sqlserver, see ticket #694445 |
528dee042927
fix #694445: related entity generates weird RQL which in turn generates weird SQL which fails on SQL Server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4460
diff
changeset
|
779 |
if 'X modification_date ' in args[1]: |
528dee042927
fix #694445: related entity generates weird RQL which in turn generates weird SQL which fails on SQL Server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4460
diff
changeset
|
780 |
var = args[1].split('X modification_date ', 1)[1].split(',', 1)[0] |
528dee042927
fix #694445: related entity generates weird RQL which in turn generates weird SQL which fails on SQL Server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4460
diff
changeset
|
781 |
args.insert(1, var.strip()) |
528dee042927
fix #694445: related entity generates weird RQL which in turn generates weird SQL which fails on SQL Server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4460
diff
changeset
|
782 |
rql = '%s ORDERBY %s DESC WHERE %s' % tuple(args) |
528dee042927
fix #694445: related entity generates weird RQL which in turn generates weird SQL which fails on SQL Server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4460
diff
changeset
|
783 |
else: |
528dee042927
fix #694445: related entity generates weird RQL which in turn generates weird SQL which fails on SQL Server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4460
diff
changeset
|
784 |
rql = '%s ORDERBY Z DESC WHERE X modification_date Z, %s' % \ |
528dee042927
fix #694445: related entity generates weird RQL which in turn generates weird SQL which fails on SQL Server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4460
diff
changeset
|
785 |
tuple(args) |
0 | 786 |
return rql |
1474 | 787 |
|
0 | 788 |
# generic vocabulary methods ############################################## |
789 |
||
790 |
def unrelated_rql(self, rtype, targettype, role, ordermethod=None, |
|
791 |
vocabconstraints=True): |
|
792 |
"""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
|
793 |
using (rtype, role) relation. |
1a6f7a0e7dbd
unrelated_rql now considers relation's add perm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3212
diff
changeset
|
794 |
|
1a6f7a0e7dbd
unrelated_rql now considers relation's add perm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3212
diff
changeset
|
795 |
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
|
796 |
linked by `rtype`. |
0 | 797 |
""" |
798 |
ordermethod = ordermethod or 'fetch_unrelated_order' |
|
799 |
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
|
800 |
rtype = self._cw.vreg.schema.rschema(rtype) |
0 | 801 |
if role == 'subject': |
802 |
evar, searchedvar = 'S', 'O' |
|
803 |
subjtype, objtype = self.e_schema, targettype |
|
804 |
else: |
|
805 |
searchedvar, evar = 'S', 'O' |
|
806 |
objtype, subjtype = self.e_schema, targettype |
|
807 |
if self.has_eid(): |
|
808 |
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
|
809 |
args = {'x': self.eid} |
1a6f7a0e7dbd
unrelated_rql now considers relation's add perm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3212
diff
changeset
|
810 |
if role == 'subject': |
1a6f7a0e7dbd
unrelated_rql now considers relation's add perm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3212
diff
changeset
|
811 |
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
|
812 |
else: |
1a6f7a0e7dbd
unrelated_rql now considers relation's add perm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3212
diff
changeset
|
813 |
securitycheck_args = {'toeid': self.eid} |
0 | 814 |
else: |
815 |
restriction = [] |
|
3241
1a6f7a0e7dbd
unrelated_rql now considers relation's add perm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3212
diff
changeset
|
816 |
args = {} |
1a6f7a0e7dbd
unrelated_rql now considers relation's add perm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3212
diff
changeset
|
817 |
securitycheck_args = {} |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
818 |
rdef = rtype.role_rdef(self.e_schema, targettype, role) |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
819 |
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
|
820 |
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
|
821 |
# XXX consider constraint.mainvars to check if constraint apply |
0 | 822 |
if vocabconstraints: |
823 |
# RQLConstraint is a subclass for RQLVocabularyConstraint, so they |
|
824 |
# will be included as well |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
825 |
restriction += [cstr.restriction for cstr in rdef.constraints |
0 | 826 |
if isinstance(cstr, RQLVocabularyConstraint)] |
827 |
else: |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
828 |
restriction += [cstr.restriction for cstr in rdef.constraints |
0 | 829 |
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
|
830 |
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
|
831 |
rql = etypecls.fetch_rql(self._cw.user, restriction, |
0 | 832 |
mainvar=searchedvar, ordermethod=ordermethod) |
833 |
# ensure we have an order defined |
|
834 |
if not ' ORDERBY ' in rql: |
|
835 |
before, after = rql.split(' WHERE ', 1) |
|
836 |
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
|
837 |
if insertsecurity: |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
838 |
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
|
839 |
rewriter = RQLRewriter(self._cw) |
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
840 |
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
|
841 |
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
|
842 |
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
|
843 |
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
|
844 |
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
|
845 |
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
|
846 |
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
|
847 |
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
|
848 |
rql = rqlst.as_string() |
1a6f7a0e7dbd
unrelated_rql now considers relation's add perm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3212
diff
changeset
|
849 |
return rql, args |
1474 | 850 |
|
0 | 851 |
def unrelated(self, rtype, targettype, role='subject', limit=None, |
852 |
ordermethod=None): |
|
853 |
"""return a result set of target type objects that may be related |
|
854 |
by a given relation, with self as subject or object |
|
855 |
""" |
|
3241
1a6f7a0e7dbd
unrelated_rql now considers relation's add perm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3212
diff
changeset
|
856 |
try: |
1a6f7a0e7dbd
unrelated_rql now considers relation's add perm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3212
diff
changeset
|
857 |
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
|
858 |
except Unauthorized: |
3378
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
859 |
return self._cw.empty_rset() |
0 | 860 |
if limit is not None: |
861 |
before, after = rql.split(' WHERE ', 1) |
|
862 |
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
|
863 |
return self._cw.execute(rql, args, tuple(args)) |
1474 | 864 |
|
0 | 865 |
# relations cache handling ################################################ |
1474 | 866 |
|
0 | 867 |
def relation_cached(self, rtype, role): |
868 |
"""return true if the given relation is already cached on the instance |
|
869 |
""" |
|
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
|
870 |
return self._related_cache.get('%s_%s' % (rtype, role)) |
1474 | 871 |
|
0 | 872 |
def related_cache(self, rtype, role, entities=True, limit=None): |
873 |
"""return values for the given relation if it's cached on the instance, |
|
874 |
else raise `KeyError` |
|
875 |
""" |
|
876 |
res = self._related_cache['%s_%s' % (rtype, role)][entities] |
|
3680 | 877 |
if limit is not None and limit < len(res): |
0 | 878 |
if entities: |
879 |
res = res[:limit] |
|
880 |
else: |
|
881 |
res = res.limit(limit) |
|
882 |
return res |
|
1474 | 883 |
|
0 | 884 |
def set_related_cache(self, rtype, role, rset, col=0): |
885 |
"""set cached values for the given relation""" |
|
886 |
if rset: |
|
887 |
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
|
888 |
rschema = self._cw.vreg.schema.rschema(rtype) |
0 | 889 |
if role == 'subject': |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
890 |
rcard = rschema.rdef(self.e_schema, related[0].e_schema).cardinality[1] |
0 | 891 |
target = 'object' |
892 |
else: |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3826
diff
changeset
|
893 |
rcard = rschema.rdef(related[0].e_schema, self.e_schema).cardinality[0] |
0 | 894 |
target = 'subject' |
895 |
if rcard in '?1': |
|
896 |
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
|
897 |
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
|
898 |
self.as_rset(), (self,)) |
0 | 899 |
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
|
900 |
related = () |
0 | 901 |
self._related_cache['%s_%s' % (rtype, role)] = (rset, related) |
1474 | 902 |
|
0 | 903 |
def clear_related_cache(self, rtype=None, role=None): |
904 |
"""clear cached values for the given relation or the entire cache if |
|
905 |
no relation is given |
|
906 |
""" |
|
907 |
if rtype is None: |
|
908 |
self._related_cache = {} |
|
909 |
else: |
|
910 |
assert role |
|
911 |
self._related_cache.pop('%s_%s' % (rtype, role), None) |
|
1474 | 912 |
|
2942
77ebdbe93cf8
[entity] generic clear_all_caches methods, to use in test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2941
diff
changeset
|
913 |
def clear_all_caches(self): |
4408
899b426087ab
[entity] small optimization: once an entity has been completed, don't redo it (for nothing)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4406
diff
changeset
|
914 |
"""flush all caches on this entity. Further attributes/relations access |
899b426087ab
[entity] small optimization: once an entity has been completed, don't redo it (for nothing)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4406
diff
changeset
|
915 |
will triggers new database queries to get back values. |
899b426087ab
[entity] small optimization: once an entity has been completed, don't redo it (for nothing)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4406
diff
changeset
|
916 |
|
899b426087ab
[entity] small optimization: once an entity has been completed, don't redo it (for nothing)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4406
diff
changeset
|
917 |
If you use custom caches on your entity class (take care to @cached!), |
899b426087ab
[entity] small optimization: once an entity has been completed, don't redo it (for nothing)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4406
diff
changeset
|
918 |
you should override this method to clear them as well. |
899b426087ab
[entity] small optimization: once an entity has been completed, don't redo it (for nothing)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4406
diff
changeset
|
919 |
""" |
899b426087ab
[entity] small optimization: once an entity has been completed, don't redo it (for nothing)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4406
diff
changeset
|
920 |
# clear attributes cache |
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
|
921 |
haseid = 'eid' in self |
4408
899b426087ab
[entity] small optimization: once an entity has been completed, don't redo it (for nothing)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4406
diff
changeset
|
922 |
self._cw_completed = False |
2942
77ebdbe93cf8
[entity] generic clear_all_caches methods, to use in test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2941
diff
changeset
|
923 |
self.clear() |
4408
899b426087ab
[entity] small optimization: once an entity has been completed, don't redo it (for nothing)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4406
diff
changeset
|
924 |
# clear relations cache |
899b426087ab
[entity] small optimization: once an entity has been completed, don't redo it (for nothing)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4406
diff
changeset
|
925 |
for rschema, _, role in self.e_schema.relation_definitions(): |
899b426087ab
[entity] small optimization: once an entity has been completed, don't redo it (for nothing)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4406
diff
changeset
|
926 |
self.clear_related_cache(rschema.type, role) |
4460
5c22869079b9
clear rest_path __unique cache in clear_all_caches, fix related test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4451
diff
changeset
|
927 |
# rest path unique cache |
5c22869079b9
clear rest_path __unique cache in clear_all_caches, fix related test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4451
diff
changeset
|
928 |
try: |
5c22869079b9
clear rest_path __unique cache in clear_all_caches, fix related test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4451
diff
changeset
|
929 |
del self.__unique |
5c22869079b9
clear rest_path __unique cache in clear_all_caches, fix related test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4451
diff
changeset
|
930 |
except AttributeError: |
5c22869079b9
clear rest_path __unique cache in clear_all_caches, fix related test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4451
diff
changeset
|
931 |
pass |
4759 | 932 |
|
0 | 933 |
# raw edition utilities ################################################### |
1474 | 934 |
|
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4831
diff
changeset
|
935 |
def set_attributes(self, **kwargs): |
5115
2e43ef618d14
[repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5009
diff
changeset
|
936 |
_check_cw_unsafe(kwargs) |
0 | 937 |
assert kwargs |
5127
3c2dda44e2f6
[entity] explain why the assert failed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5115
diff
changeset
|
938 |
assert self._is_saved, "should not call set_attributes while entity "\ |
3c2dda44e2f6
[entity] explain why the assert failed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5115
diff
changeset
|
939 |
"hasn't been saved yet" |
0 | 940 |
relations = [] |
941 |
for key in kwargs: |
|
942 |
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
|
943 |
# and now update the database |
0 | 944 |
kwargs['x'] = self.eid |
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4831
diff
changeset
|
945 |
self._cw.execute('SET %s WHERE X eid %%(x)s' % ','.join(relations), |
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4831
diff
changeset
|
946 |
kwargs, 'x') |
5115
2e43ef618d14
[repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5009
diff
changeset
|
947 |
kwargs.pop('x') |
2e43ef618d14
[repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5009
diff
changeset
|
948 |
# update current local object _after_ the rql query to avoid |
2e43ef618d14
[repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5009
diff
changeset
|
949 |
# interferences between the query execution itself and the |
2e43ef618d14
[repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5009
diff
changeset
|
950 |
# edited_attributes / skip_security_attributes machinery |
2e43ef618d14
[repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5009
diff
changeset
|
951 |
self.update(kwargs) |
1474 | 952 |
|
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4831
diff
changeset
|
953 |
def set_relations(self, **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
|
954 |
"""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
|
955 |
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
|
956 |
|
5144
5a09bea07302
[doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5127
diff
changeset
|
957 |
Values may be an entity, a list of entities, or None (meaning that all |
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
|
958 |
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
|
959 |
""" |
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
|
960 |
# XXX update cache |
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4831
diff
changeset
|
961 |
_check_cw_unsafe(kwargs) |
3671
c765adac7f5c
new set_relations method on entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3665
diff
changeset
|
962 |
for attr, values in kwargs.iteritems(): |
c765adac7f5c
new set_relations method on entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3665
diff
changeset
|
963 |
if attr.startswith('reverse_'): |
c765adac7f5c
new set_relations method on entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3665
diff
changeset
|
964 |
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
|
965 |
else: |
c765adac7f5c
new set_relations method on entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3665
diff
changeset
|
966 |
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
|
967 |
if values is None: |
4991
16f0cec3d008
[entity] fix dumb name error on .set_relations(rel=None)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4990
diff
changeset
|
968 |
self._cw.execute('DELETE %s WHERE X eid %%(x)s' % restr, |
16f0cec3d008
[entity] fix dumb name error on .set_relations(rel=None)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4990
diff
changeset
|
969 |
{'x': self.eid}, 'x') |
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
|
970 |
continue |
3746
74192424b2c8
accepts single values as create_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
971 |
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
|
972 |
values = (values,) |
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4831
diff
changeset
|
973 |
self._cw.execute('SET %s WHERE X eid %%(x)s, Y eid IN (%s)' % ( |
3671
c765adac7f5c
new set_relations method on entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3665
diff
changeset
|
974 |
restr, ','.join(str(r.eid) for r in values)), |
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4831
diff
changeset
|
975 |
{'x': self.eid}, 'x') |
3671
c765adac7f5c
new set_relations method on entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3665
diff
changeset
|
976 |
|
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4831
diff
changeset
|
977 |
def delete(self, **kwargs): |
0 | 978 |
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
|
979 |
self._cw.execute('DELETE %s X WHERE X eid %%(x)s' % self.e_schema, |
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4831
diff
changeset
|
980 |
{'x': self.eid}, **kwargs) |
1474 | 981 |
|
0 | 982 |
# server side utilities ################################################### |
1474 | 983 |
|
4970
1f3d8946ea84
fix security issue introduced by 4967:04543ed0bbdc: attributes explicitly set by hooks should not be checked by security hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
984 |
@property |
1f3d8946ea84
fix security issue introduced by 4967:04543ed0bbdc: attributes explicitly set by hooks should not be checked by security hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
985 |
def skip_security_attributes(self): |
1f3d8946ea84
fix security issue introduced by 4967:04543ed0bbdc: attributes explicitly set by hooks should not be checked by security hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
986 |
try: |
1f3d8946ea84
fix security issue introduced by 4967:04543ed0bbdc: attributes explicitly set by hooks should not be checked by security hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
987 |
return self._skip_security_attributes |
1f3d8946ea84
fix security issue introduced by 4967:04543ed0bbdc: attributes explicitly set by hooks should not be checked by security hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
988 |
except: |
1f3d8946ea84
fix security issue introduced by 4967:04543ed0bbdc: attributes explicitly set by hooks should not be checked by security hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
989 |
self._skip_security_attributes = set() |
1f3d8946ea84
fix security issue introduced by 4967:04543ed0bbdc: attributes explicitly set by hooks should not be checked by security hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
990 |
return self._skip_security_attributes |
1f3d8946ea84
fix security issue introduced by 4967:04543ed0bbdc: attributes explicitly set by hooks should not be checked by security hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
991 |
|
0 | 992 |
def set_defaults(self): |
993 |
"""set default values according to the schema""" |
|
994 |
for attr, value in self.e_schema.defaults(): |
|
995 |
if not self.has_key(attr): |
|
996 |
self[str(attr)] = value |
|
997 |
||
998 |
def check(self, creation=False): |
|
999 |
"""check this entity against its schema. Only final relation |
|
1000 |
are checked here, constraint on actual relations are checked in hooks |
|
1001 |
""" |
|
1002 |
# necessary since eid is handled specifically and yams require it to be |
|
1003 |
# in the dictionary |
|
3378
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
1004 |
if self._cw is None: |
0 | 1005 |
_ = unicode |
1006 |
else: |
|
3378
2f25f701301d
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3376
diff
changeset
|
1007 |
_ = self._cw._ |
4988
d85f639e9150
[yams] on creation, specify relations anyway so we don't get spurious warning for accessing eid through the dict api on yams validation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4970
diff
changeset
|
1008 |
if creation: |
4965
04543ed0bbdc
[source] only consider edited_attributes in source.preprocess_entity()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4913
diff
changeset
|
1009 |
# on creations, we want to check all relations, especially |
04543ed0bbdc
[source] only consider edited_attributes in source.preprocess_entity()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4913
diff
changeset
|
1010 |
# required attributes |
4988
d85f639e9150
[yams] on creation, specify relations anyway so we don't get spurious warning for accessing eid through the dict api on yams validation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4970
diff
changeset
|
1011 |
relations = [rschema for rschema in self.e_schema.subject_relations() |
d85f639e9150
[yams] on creation, specify relations anyway so we don't get spurious warning for accessing eid through the dict api on yams validation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4970
diff
changeset
|
1012 |
if rschema.final and rschema.type != 'eid'] |
d85f639e9150
[yams] on creation, specify relations anyway so we don't get spurious warning for accessing eid through the dict api on yams validation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4970
diff
changeset
|
1013 |
elif hasattr(self, 'edited_attributes'): |
4965
04543ed0bbdc
[source] only consider edited_attributes in source.preprocess_entity()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4913
diff
changeset
|
1014 |
relations = [self._cw.vreg.schema.rschema(rtype) |
04543ed0bbdc
[source] only consider edited_attributes in source.preprocess_entity()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4913
diff
changeset
|
1015 |
for rtype in self.edited_attributes] |
4988
d85f639e9150
[yams] on creation, specify relations anyway so we don't get spurious warning for accessing eid through the dict api on yams validation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4970
diff
changeset
|
1016 |
else: |
d85f639e9150
[yams] on creation, specify relations anyway so we don't get spurious warning for accessing eid through the dict api on yams validation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4970
diff
changeset
|
1017 |
relations = None |
4965
04543ed0bbdc
[source] only consider edited_attributes in source.preprocess_entity()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4913
diff
changeset
|
1018 |
self.e_schema.check(self, creation=creation, _=_, |
04543ed0bbdc
[source] only consider edited_attributes in source.preprocess_entity()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4913
diff
changeset
|
1019 |
relations=relations) |
0 | 1020 |
|
1021 |
def fti_containers(self, _done=None): |
|
1022 |
if _done is None: |
|
1023 |
_done = set() |
|
1024 |
_done.add(self.eid) |
|
1025 |
containers = tuple(self.e_schema.fulltext_containers()) |
|
1026 |
if containers: |
|
1027 |
for rschema, target in containers: |
|
1028 |
if target == 'object': |
|
1029 |
targets = getattr(self, rschema.type) |
|
1030 |
else: |
|
1031 |
targets = getattr(self, 'reverse_%s' % rschema) |
|
1032 |
for entity in targets: |
|
1033 |
if entity.eid in _done: |
|
1034 |
continue |
|
1035 |
for container in entity.fti_containers(_done): |
|
1036 |
yield container |
|
476
62968fa8845c
yield self if no ft container found
sylvain.thenault@logilab.fr
parents:
475
diff
changeset
|
1037 |
yielded = True |
0 | 1038 |
else: |
1039 |
yield self |
|
1474 | 1040 |
|
0 | 1041 |
def get_words(self): |
1042 |
"""used by the full text indexer to get words to index |
|
1043 |
||
1044 |
this method should only be used on the repository side since it depends |
|
4848
41f84eea63c9
rename logilab.db into logilab.database
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
1045 |
on the logilab.database package |
1474 | 1046 |
|
0 | 1047 |
:rtype: list |
1048 |
:return: the list of indexable word of this entity |
|
1049 |
""" |
|
4848
41f84eea63c9
rename logilab.db into logilab.database
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
1050 |
from logilab.database.fti import tokenize |
3546
f0aecddf367e
fix bug when renaming a full text indexed attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3316
diff
changeset
|
1051 |
# 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
|
1052 |
pending = self._cw.transaction_data.setdefault('pendingrdefs', set()) |
0 | 1053 |
words = [] |
1054 |
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
|
1055 |
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
|
1056 |
continue |
0 | 1057 |
try: |
1058 |
value = self.printable_value(rschema, format='text/plain') |
|
1132 | 1059 |
except TransformError: |
0 | 1060 |
continue |
1061 |
except: |
|
1062 |
self.exception("can't add value of %s to text index for entity %s", |
|
1063 |
rschema, self.eid) |
|
1064 |
continue |
|
1065 |
if value: |
|
1066 |
words += tokenize(value) |
|
1067 |
for rschema, role in self.e_schema.fulltext_relations(): |
|
1068 |
if role == 'subject': |
|
1069 |
for entity in getattr(self, rschema.type): |
|
1070 |
words += entity.get_words() |
|
1071 |
else: # if role == 'object': |
|
1072 |
for entity in getattr(self, 'reverse_%s' % rschema.type): |
|
1073 |
words += entity.get_words() |
|
1074 |
return words |
|
1075 |
||
1076 |
||
1077 |
# attribute and relation descriptors ########################################## |
|
1078 |
||
1079 |
class Attribute(object): |
|
1080 |
"""descriptor that controls schema attribute access""" |
|
1081 |
||
1082 |
def __init__(self, attrname): |
|
1083 |
assert attrname != 'eid' |
|
1084 |
self._attrname = attrname |
|
1085 |
||
1086 |
def __get__(self, eobj, eclass): |
|
1087 |
if eobj is None: |
|
1088 |
return self |
|
1089 |
return eobj.get_value(self._attrname) |
|
1090 |
||
1091 |
def __set__(self, eobj, value): |
|
1092 |
eobj[self._attrname] = value |
|
1093 |
||
1094 |
class Relation(object): |
|
1095 |
"""descriptor that controls schema relation access""" |
|
1096 |
_role = None # for pylint |
|
1097 |
||
1098 |
def __init__(self, rschema): |
|
1099 |
self._rschema = rschema |
|
1100 |
self._rtype = rschema.type |
|
1101 |
||
1102 |
def __get__(self, eobj, eclass): |
|
1103 |
if eobj is None: |
|
1104 |
raise AttributeError('%s cannot be only be accessed from instances' |
|
1105 |
% self._rtype) |
|
1106 |
return eobj.related(self._rtype, self._role, entities=True) |
|
1474 | 1107 |
|
0 | 1108 |
def __set__(self, eobj, value): |
1109 |
raise NotImplementedError |
|
1110 |
||
1111 |
||
1112 |
class SubjectRelation(Relation): |
|
1113 |
"""descriptor that controls schema relation access""" |
|
1114 |
_role = 'subject' |
|
1474 | 1115 |
|
0 | 1116 |
class ObjectRelation(Relation): |
1117 |
"""descriptor that controls schema relation access""" |
|
1118 |
_role = 'object' |
|
1119 |
||
1120 |
from logging import getLogger |
|
1121 |
from cubicweb import set_log_methods |
|
1122 |
set_log_methods(Entity, getLogger('cubicweb.entity')) |