author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Mon, 06 Jul 2009 19:55:18 +0200 | |
changeset 2293 | 7ded2a1416e4 |
parent 2234 | 1fbcf202882d |
parent 2252 | dd9758cb77e1 |
child 2300 | c8151d004e06 |
permissions | -rw-r--r-- |
0 | 1 |
"""classes to define schemas for CubicWeb |
2 |
||
3 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1498
diff
changeset
|
4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1498
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 7 |
""" |
8 |
__docformat__ = "restructuredtext en" |
|
2142
098aa2075903
include_schema_files is useless
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
9 |
_ = unicode |
0 | 10 |
|
11 |
import re |
|
2147
476a75ede2cc
merge and add missing import in schema.py
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2142
diff
changeset
|
12 |
from os.path import join |
0 | 13 |
from logging import getLogger |
1133 | 14 |
from warnings import warn |
0 | 15 |
|
624
258e5692ae06
provide a new RichString attribute type
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
479
diff
changeset
|
16 |
from logilab.common.decorators import cached, clear_cache, monkeypatch |
2201
ddc1f58c8e8b
deprecate display_name from builtin, ask for explicit import
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2157
diff
changeset
|
17 |
from logilab.common.deprecation import obsolete |
0 | 18 |
from logilab.common.compat import any |
19 |
||
20 |
from yams import BadSchemaDefinition, buildobjs as ybo |
|
21 |
from yams.schema import Schema, ERSchema, EntitySchema, RelationSchema |
|
22 |
from yams.constraints import BaseConstraint, StaticVocabularyConstraint |
|
2183
9dfa88ac9f49
RelationFileReader is dead
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2181
diff
changeset
|
23 |
from yams.reader import CONSTRAINTS, PyFileReader, SchemaLoader |
0 | 24 |
|
25 |
from rql import parse, nodes, RQLSyntaxError, TypeResolverException |
|
26 |
||
27 |
from cubicweb import ETYPE_NAME_MAP, ValidationError, Unauthorized |
|
1138
22f634977c95
make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents:
1133
diff
changeset
|
28 |
from cubicweb import set_log_methods |
0 | 29 |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
745
diff
changeset
|
30 |
# XXX <3.2 bw compat |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
745
diff
changeset
|
31 |
from yams import schema |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
745
diff
changeset
|
32 |
schema.use_py_datetime() |
1451 | 33 |
nodes.use_py_datetime() |
0 | 34 |
|
2126
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
35 |
# set of meta-relations available for every entity types |
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
36 |
META_RELATIONS_TYPES = set(( |
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
37 |
'owned_by', 'created_by', 'is', 'is_instance_of', 'identity', |
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
38 |
'eid', 'creation_date', 'modification_date', 'has_text', |
2184 | 39 |
)) |
0 | 40 |
|
2126
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
41 |
# set of entity and relation types used to build the schema |
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
42 |
SCHEMA_TYPES = set(( |
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
43 |
'CWEType', 'CWRType', 'CWAttribute', 'CWRelation', |
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
44 |
'CWConstraint', 'CWConstraintType', 'RQLExpression', |
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
45 |
'relation_type', 'from_entity', 'to_entity', |
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
46 |
'constrained_by', 'cstrtype', |
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
47 |
# XXX those are not really "schema" entity types |
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
48 |
# but we usually don't want them as @* targets |
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
49 |
'CWProperty', 'CWPermission', 'State', 'Transition', |
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
50 |
)) |
0 | 51 |
|
2142
098aa2075903
include_schema_files is useless
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
52 |
_LOGGER = getLogger('cubicweb.schemaloader') |
0 | 53 |
|
54 |
# schema entities created from serialized schema have an eid rproperty |
|
55 |
ybo.ETYPE_PROPERTIES += ('eid',) |
|
56 |
ybo.RTYPE_PROPERTIES += ('eid',) |
|
57 |
ybo.RDEF_PROPERTIES += ('eid',) |
|
58 |
||
59 |
def bw_normalize_etype(etype): |
|
60 |
if etype in ETYPE_NAME_MAP: |
|
61 |
msg = '%s has been renamed to %s, please update your code' % ( |
|
1451 | 62 |
etype, ETYPE_NAME_MAP[etype]) |
0 | 63 |
warn(msg, DeprecationWarning, stacklevel=4) |
64 |
etype = ETYPE_NAME_MAP[etype] |
|
65 |
return etype |
|
66 |
||
624
258e5692ae06
provide a new RichString attribute type
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
479
diff
changeset
|
67 |
|
258e5692ae06
provide a new RichString attribute type
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
479
diff
changeset
|
68 |
## cubicweb provides a RichString class for convenience |
258e5692ae06
provide a new RichString attribute type
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
479
diff
changeset
|
69 |
class RichString(ybo.String): |
258e5692ae06
provide a new RichString attribute type
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
479
diff
changeset
|
70 |
"""Convenience RichString attribute type |
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1477
diff
changeset
|
71 |
The following declaration:: |
1451 | 72 |
|
624
258e5692ae06
provide a new RichString attribute type
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
479
diff
changeset
|
73 |
class Card(EntityType): |
258e5692ae06
provide a new RichString attribute type
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
479
diff
changeset
|
74 |
content = RichString(fulltextindexed=True, default_format='text/rest') |
1451 | 75 |
|
624
258e5692ae06
provide a new RichString attribute type
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
479
diff
changeset
|
76 |
is equivalent to:: |
1451 | 77 |
|
624
258e5692ae06
provide a new RichString attribute type
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
479
diff
changeset
|
78 |
class Card(EntityType): |
258e5692ae06
provide a new RichString attribute type
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
479
diff
changeset
|
79 |
content_format = String(meta=True, internationalizable=True, |
258e5692ae06
provide a new RichString attribute type
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
479
diff
changeset
|
80 |
default='text/rest', constraints=[format_constraint]) |
258e5692ae06
provide a new RichString attribute type
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
479
diff
changeset
|
81 |
content = String(fulltextindexed=True) |
258e5692ae06
provide a new RichString attribute type
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
479
diff
changeset
|
82 |
""" |
258e5692ae06
provide a new RichString attribute type
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
479
diff
changeset
|
83 |
def __init__(self, default_format='text/plain', format_constraints=None, **kwargs): |
258e5692ae06
provide a new RichString attribute type
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
479
diff
changeset
|
84 |
self.default_format = default_format |
258e5692ae06
provide a new RichString attribute type
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
479
diff
changeset
|
85 |
self.format_constraints = format_constraints or [format_constraint] |
258e5692ae06
provide a new RichString attribute type
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
479
diff
changeset
|
86 |
super(RichString, self).__init__(**kwargs) |
258e5692ae06
provide a new RichString attribute type
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
479
diff
changeset
|
87 |
|
258e5692ae06
provide a new RichString attribute type
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
479
diff
changeset
|
88 |
PyFileReader.context['RichString'] = RichString |
258e5692ae06
provide a new RichString attribute type
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
479
diff
changeset
|
89 |
|
258e5692ae06
provide a new RichString attribute type
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
479
diff
changeset
|
90 |
## need to monkeypatch yams' _add_relation function to handle RichString |
258e5692ae06
provide a new RichString attribute type
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
479
diff
changeset
|
91 |
yams_add_relation = ybo._add_relation |
258e5692ae06
provide a new RichString attribute type
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
479
diff
changeset
|
92 |
@monkeypatch(ybo) |
258e5692ae06
provide a new RichString attribute type
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
479
diff
changeset
|
93 |
def _add_relation(relations, rdef, name=None, insertidx=None): |
258e5692ae06
provide a new RichString attribute type
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
479
diff
changeset
|
94 |
if isinstance(rdef, RichString): |
258e5692ae06
provide a new RichString attribute type
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
479
diff
changeset
|
95 |
format_attrdef = ybo.String(meta=True, internationalizable=True, |
258e5692ae06
provide a new RichString attribute type
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
479
diff
changeset
|
96 |
default=rdef.default_format, maxsize=50, |
258e5692ae06
provide a new RichString attribute type
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
479
diff
changeset
|
97 |
constraints=rdef.format_constraints) |
258e5692ae06
provide a new RichString attribute type
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
479
diff
changeset
|
98 |
yams_add_relation(relations, format_attrdef, name+'_format', insertidx) |
258e5692ae06
provide a new RichString attribute type
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
479
diff
changeset
|
99 |
yams_add_relation(relations, rdef, name, insertidx) |
1451 | 100 |
|
2222
81130047390d
[schema] fix Entity.add_relation(RichString(...)...)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2201
diff
changeset
|
101 |
|
81130047390d
[schema] fix Entity.add_relation(RichString(...)...)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2201
diff
changeset
|
102 |
yams_EntityType_add_relation = ybo.EntityType.add_relation |
81130047390d
[schema] fix Entity.add_relation(RichString(...)...)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2201
diff
changeset
|
103 |
@monkeypatch(ybo.EntityType) |
81130047390d
[schema] fix Entity.add_relation(RichString(...)...)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2201
diff
changeset
|
104 |
def add_relation(self, rdef, name=None): |
81130047390d
[schema] fix Entity.add_relation(RichString(...)...)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2201
diff
changeset
|
105 |
yams_EntityType_add_relation(self, rdef, name) |
81130047390d
[schema] fix Entity.add_relation(RichString(...)...)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2201
diff
changeset
|
106 |
if isinstance(rdef, RichString) and not rdef in self._defined: |
81130047390d
[schema] fix Entity.add_relation(RichString(...)...)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2201
diff
changeset
|
107 |
format_attr_name = (name or rdef.name) + '_format' |
81130047390d
[schema] fix Entity.add_relation(RichString(...)...)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2201
diff
changeset
|
108 |
rdef = self.get_relations(format_attr_name).next() |
81130047390d
[schema] fix Entity.add_relation(RichString(...)...)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2201
diff
changeset
|
109 |
self._ensure_relation_type(rdef) |
81130047390d
[schema] fix Entity.add_relation(RichString(...)...)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2201
diff
changeset
|
110 |
|
0 | 111 |
def display_name(req, key, form=''): |
112 |
"""return a internationalized string for the key (schema entity or relation |
|
113 |
name) in a given form |
|
114 |
""" |
|
115 |
assert form in ('', 'plural', 'subject', 'object') |
|
116 |
if form == 'subject': |
|
117 |
form = '' |
|
118 |
if form: |
|
119 |
key = key + '_' + form |
|
120 |
# ensure unicode |
|
121 |
# added .lower() in case no translation are available |
|
122 |
return unicode(req._(key)).lower() |
|
2201
ddc1f58c8e8b
deprecate display_name from builtin, ask for explicit import
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2157
diff
changeset
|
123 |
__builtins__['display_name'] = obsolete('display_name should be imported from cubicweb.schema')(display_name) |
0 | 124 |
|
125 |
def ERSchema_display_name(self, req, form=''): |
|
126 |
"""return a internationalized string for the entity/relation type name in |
|
127 |
a given form |
|
128 |
""" |
|
129 |
return display_name(req, self.type, form) |
|
130 |
ERSchema.display_name = ERSchema_display_name |
|
131 |
||
132 |
@cached |
|
133 |
def ERSchema_get_groups(self, action): |
|
134 |
"""return the groups authorized to perform <action> on entities of |
|
135 |
this type |
|
136 |
||
137 |
:type action: str |
|
138 |
:param action: the name of a permission |
|
139 |
||
140 |
:rtype: tuple |
|
141 |
:return: names of the groups with the given permission |
|
142 |
""" |
|
143 |
assert action in self.ACTIONS, action |
|
144 |
#assert action in self._groups, '%s %s' % (self, action) |
|
145 |
try: |
|
146 |
return frozenset(g for g in self._groups[action] if isinstance(g, basestring)) |
|
147 |
except KeyError: |
|
148 |
return () |
|
149 |
ERSchema.get_groups = ERSchema_get_groups |
|
150 |
||
151 |
def ERSchema_set_groups(self, action, groups): |
|
152 |
"""set the groups allowed to perform <action> on entities of this type. Don't |
|
153 |
change rql expressions for the same action. |
|
154 |
||
155 |
:type action: str |
|
156 |
:param action: the name of a permission |
|
157 |
||
158 |
:type groups: list or tuple |
|
159 |
:param groups: names of the groups granted to do the given action |
|
160 |
""" |
|
161 |
assert action in self.ACTIONS, action |
|
162 |
clear_cache(self, 'ERSchema_get_groups') |
|
163 |
self._groups[action] = tuple(groups) + self.get_rqlexprs(action) |
|
164 |
ERSchema.set_groups = ERSchema_set_groups |
|
165 |
||
166 |
@cached |
|
167 |
def ERSchema_get_rqlexprs(self, action): |
|
168 |
"""return the rql expressions representing queries to check the user is allowed |
|
169 |
to perform <action> on entities of this type |
|
170 |
||
171 |
:type action: str |
|
172 |
:param action: the name of a permission |
|
173 |
||
174 |
:rtype: tuple |
|
175 |
:return: the rql expressions with the given permission |
|
176 |
""" |
|
177 |
assert action in self.ACTIONS, action |
|
178 |
#assert action in self._rqlexprs, '%s %s' % (self, action) |
|
179 |
try: |
|
180 |
return tuple(g for g in self._groups[action] if not isinstance(g, basestring)) |
|
181 |
except KeyError: |
|
182 |
return () |
|
183 |
ERSchema.get_rqlexprs = ERSchema_get_rqlexprs |
|
184 |
||
185 |
def ERSchema_set_rqlexprs(self, action, rqlexprs): |
|
186 |
"""set the rql expression allowing to perform <action> on entities of this type. Don't |
|
187 |
change groups for the same action. |
|
188 |
||
189 |
:type action: str |
|
190 |
:param action: the name of a permission |
|
191 |
||
192 |
:type rqlexprs: list or tuple |
|
193 |
:param rqlexprs: the rql expressions allowing the given action |
|
194 |
""" |
|
195 |
assert action in self.ACTIONS, action |
|
196 |
clear_cache(self, 'ERSchema_get_rqlexprs') |
|
197 |
self._groups[action] = tuple(self.get_groups(action)) + tuple(rqlexprs) |
|
198 |
ERSchema.set_rqlexprs = ERSchema_set_rqlexprs |
|
199 |
||
200 |
def ERSchema_set_permissions(self, action, permissions): |
|
201 |
"""set the groups and rql expressions allowing to perform <action> on |
|
202 |
entities of this type |
|
203 |
||
204 |
:type action: str |
|
205 |
:param action: the name of a permission |
|
206 |
||
207 |
:type permissions: tuple |
|
208 |
:param permissions: the groups and rql expressions allowing the given action |
|
209 |
""" |
|
210 |
assert action in self.ACTIONS, action |
|
211 |
clear_cache(self, 'ERSchema_get_rqlexprs') |
|
212 |
clear_cache(self, 'ERSchema_get_groups') |
|
213 |
self._groups[action] = tuple(permissions) |
|
214 |
ERSchema.set_permissions = ERSchema_set_permissions |
|
215 |
||
216 |
def ERSchema_has_perm(self, session, action, *args, **kwargs): |
|
217 |
"""return true if the action is granted globaly or localy""" |
|
218 |
try: |
|
219 |
self.check_perm(session, action, *args, **kwargs) |
|
220 |
return True |
|
221 |
except Unauthorized: |
|
222 |
return False |
|
223 |
ERSchema.has_perm = ERSchema_has_perm |
|
224 |
||
225 |
def ERSchema_has_local_role(self, action): |
|
226 |
"""return true if the action *may* be granted localy (eg either rql |
|
227 |
expressions or the owners group are used in security definition) |
|
228 |
||
229 |
XXX this method is only there since we don't know well how to deal with |
|
230 |
'add' action checking. Also find a better name would be nice. |
|
231 |
""" |
|
232 |
assert action in self.ACTIONS, action |
|
233 |
if self.get_rqlexprs(action): |
|
234 |
return True |
|
235 |
if action in ('update', 'delete'): |
|
236 |
return self.has_group(action, 'owners') |
|
237 |
return False |
|
238 |
ERSchema.has_local_role = ERSchema_has_local_role |
|
239 |
||
240 |
||
241 |
def system_etypes(schema): |
|
242 |
"""return system entity types only: skip final, schema and application entities |
|
243 |
""" |
|
244 |
for eschema in schema.entities(): |
|
2126
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
245 |
if eschema.is_final() or eschema.schema_entity(): |
0 | 246 |
continue |
247 |
yield eschema.type |
|
248 |
||
249 |
# Schema objects definition ################################################### |
|
250 |
||
251 |
class CubicWebEntitySchema(EntitySchema): |
|
252 |
"""a entity has a type, a set of subject and or object relations |
|
253 |
the entity schema defines the possible relations for a given type and some |
|
254 |
constraints on those relations |
|
255 |
""" |
|
256 |
def __init__(self, schema=None, edef=None, eid=None, **kwargs): |
|
257 |
super(CubicWebEntitySchema, self).__init__(schema, edef, **kwargs) |
|
258 |
if eid is None and edef is not None: |
|
259 |
eid = getattr(edef, 'eid', None) |
|
260 |
self.eid = eid |
|
261 |
# take care: no _groups attribute when deep-copying |
|
1451 | 262 |
if getattr(self, '_groups', None): |
0 | 263 |
for groups in self._groups.itervalues(): |
264 |
for group_or_rqlexpr in groups: |
|
265 |
if isinstance(group_or_rqlexpr, RRQLExpression): |
|
266 |
msg = "can't use RRQLExpression on an entity type, use an ERQLExpression (%s)" |
|
267 |
raise BadSchemaDefinition(msg % self.type) |
|
1451 | 268 |
|
0 | 269 |
def attribute_definitions(self): |
270 |
"""return an iterator on attribute definitions |
|
1451 | 271 |
|
0 | 272 |
attribute relations are a subset of subject relations where the |
273 |
object's type is a final entity |
|
1451 | 274 |
|
0 | 275 |
an attribute definition is a 2-uple : |
276 |
* name of the relation |
|
277 |
* schema of the destination entity type |
|
278 |
""" |
|
279 |
iter = super(CubicWebEntitySchema, self).attribute_definitions() |
|
280 |
for rschema, attrschema in iter: |
|
281 |
if rschema.type == 'has_text': |
|
282 |
continue |
|
283 |
yield rschema, attrschema |
|
1451 | 284 |
|
2128
464edb198faa
drop @ wildcard in relation subject/object, override main_attribute for proper behaviour
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2126
diff
changeset
|
285 |
def main_attribute(self): |
464edb198faa
drop @ wildcard in relation subject/object, override main_attribute for proper behaviour
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2126
diff
changeset
|
286 |
"""convenience method that returns the *main* (i.e. the first non meta) |
464edb198faa
drop @ wildcard in relation subject/object, override main_attribute for proper behaviour
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2126
diff
changeset
|
287 |
attribute defined in the entity schema |
464edb198faa
drop @ wildcard in relation subject/object, override main_attribute for proper behaviour
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2126
diff
changeset
|
288 |
""" |
464edb198faa
drop @ wildcard in relation subject/object, override main_attribute for proper behaviour
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2126
diff
changeset
|
289 |
for rschema, _ in self.attribute_definitions(): |
464edb198faa
drop @ wildcard in relation subject/object, override main_attribute for proper behaviour
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2126
diff
changeset
|
290 |
if not (rschema in META_RELATIONS_TYPES |
464edb198faa
drop @ wildcard in relation subject/object, override main_attribute for proper behaviour
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2126
diff
changeset
|
291 |
or self.is_metadata(rschema)): |
464edb198faa
drop @ wildcard in relation subject/object, override main_attribute for proper behaviour
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2126
diff
changeset
|
292 |
return rschema |
464edb198faa
drop @ wildcard in relation subject/object, override main_attribute for proper behaviour
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2126
diff
changeset
|
293 |
|
0 | 294 |
def add_subject_relation(self, rschema): |
295 |
"""register the relation schema as possible subject relation""" |
|
296 |
super(CubicWebEntitySchema, self).add_subject_relation(rschema) |
|
297 |
self._update_has_text() |
|
298 |
||
299 |
def del_subject_relation(self, rtype): |
|
300 |
super(CubicWebEntitySchema, self).del_subject_relation(rtype) |
|
301 |
self._update_has_text(False) |
|
1451 | 302 |
|
0 | 303 |
def _update_has_text(self, need_has_text=None): |
304 |
may_need_has_text, has_has_text = False, False |
|
305 |
for rschema in self.subject_relations(): |
|
306 |
if rschema.is_final(): |
|
307 |
if rschema == 'has_text': |
|
308 |
has_has_text = True |
|
309 |
elif self.rproperty(rschema, 'fulltextindexed'): |
|
310 |
may_need_has_text = True |
|
311 |
elif rschema.fulltext_container: |
|
312 |
if rschema.fulltext_container == 'subject': |
|
313 |
may_need_has_text = True |
|
314 |
else: |
|
315 |
need_has_text = False |
|
316 |
for rschema in self.object_relations(): |
|
317 |
if rschema.fulltext_container: |
|
318 |
if rschema.fulltext_container == 'object': |
|
319 |
may_need_has_text = True |
|
320 |
else: |
|
321 |
need_has_text = False |
|
322 |
break |
|
323 |
if need_has_text is None: |
|
324 |
need_has_text = may_need_has_text |
|
325 |
if need_has_text and not has_has_text: |
|
326 |
rdef = ybo.RelationDefinition(self.type, 'has_text', 'String') |
|
327 |
self.schema.add_relation_def(rdef) |
|
328 |
elif not need_has_text and has_has_text: |
|
329 |
self.schema.del_relation_def(self.type, 'has_text', 'String') |
|
1451 | 330 |
|
0 | 331 |
def schema_entity(self): |
332 |
"""return True if this entity type is used to build the schema""" |
|
2126
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
333 |
return self.type in SCHEMA_TYPES |
0 | 334 |
|
335 |
def check_perm(self, session, action, eid=None): |
|
336 |
# NB: session may be a server session or a request object |
|
337 |
user = session.user |
|
338 |
# check user is in an allowed group, if so that's enough |
|
339 |
# internal sessions should always stop there |
|
340 |
if user.matching_groups(self.get_groups(action)): |
|
341 |
return |
|
342 |
# if 'owners' in allowed groups, check if the user actually owns this |
|
343 |
# object, if so that's enough |
|
344 |
if eid is not None and 'owners' in self.get_groups(action) and \ |
|
345 |
user.owns(eid): |
|
346 |
return |
|
347 |
# else if there is some rql expressions, check them |
|
348 |
if any(rqlexpr.check(session, eid) |
|
349 |
for rqlexpr in self.get_rqlexprs(action)): |
|
1451 | 350 |
return |
0 | 351 |
raise Unauthorized(action, str(self)) |
352 |
||
353 |
def rql_expression(self, expression, mainvars=None, eid=None): |
|
354 |
"""rql expression factory""" |
|
355 |
return ERQLExpression(expression, mainvars, eid) |
|
1451 | 356 |
|
2252 | 357 |
|
0 | 358 |
class CubicWebRelationSchema(RelationSchema): |
359 |
RelationSchema._RPROPERTIES['eid'] = None |
|
360 |
_perms_checked = False |
|
1451 | 361 |
|
0 | 362 |
def __init__(self, schema=None, rdef=None, eid=None, **kwargs): |
363 |
if rdef is not None: |
|
364 |
# if this relation is inlined |
|
365 |
self.inlined = rdef.inlined |
|
366 |
super(CubicWebRelationSchema, self).__init__(schema, rdef, **kwargs) |
|
367 |
if eid is None and rdef is not None: |
|
368 |
eid = getattr(rdef, 'eid', None) |
|
369 |
self.eid = eid |
|
1451 | 370 |
|
2126
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
371 |
@property |
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
372 |
def meta(self): |
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
373 |
return self.type in META_RELATIONS_TYPES |
1451 | 374 |
|
0 | 375 |
def update(self, subjschema, objschema, rdef): |
376 |
super(CubicWebRelationSchema, self).update(subjschema, objschema, rdef) |
|
377 |
if not self._perms_checked and self._groups: |
|
378 |
for action, groups in self._groups.iteritems(): |
|
379 |
for group_or_rqlexpr in groups: |
|
380 |
if action == 'read' and \ |
|
381 |
isinstance(group_or_rqlexpr, RQLExpression): |
|
382 |
msg = "can't use rql expression for read permission of "\ |
|
383 |
"a relation type (%s)" |
|
384 |
raise BadSchemaDefinition(msg % self.type) |
|
385 |
elif self.final and isinstance(group_or_rqlexpr, RRQLExpression): |
|
386 |
if self.schema.reading_from_database: |
|
387 |
# we didn't have final relation earlier, so turn |
|
388 |
# RRQLExpression into ERQLExpression now |
|
389 |
rqlexpr = group_or_rqlexpr |
|
390 |
newrqlexprs = [x for x in self.get_rqlexprs(action) if not x is rqlexpr] |
|
391 |
newrqlexprs.append(ERQLExpression(rqlexpr.expression, |
|
392 |
rqlexpr.mainvars, |
|
393 |
rqlexpr.eid)) |
|
1451 | 394 |
self.set_rqlexprs(action, newrqlexprs) |
0 | 395 |
else: |
396 |
msg = "can't use RRQLExpression on a final relation "\ |
|
397 |
"type (eg attribute relation), use an ERQLExpression (%s)" |
|
398 |
raise BadSchemaDefinition(msg % self.type) |
|
399 |
elif not self.final and \ |
|
400 |
isinstance(group_or_rqlexpr, ERQLExpression): |
|
401 |
msg = "can't use ERQLExpression on a relation type, use "\ |
|
402 |
"a RRQLExpression (%s)" |
|
403 |
raise BadSchemaDefinition(msg % self.type) |
|
404 |
self._perms_checked = True |
|
1451 | 405 |
|
0 | 406 |
def cardinality(self, subjtype, objtype, target): |
407 |
card = self.rproperty(subjtype, objtype, 'cardinality') |
|
408 |
return (target == 'subject' and card[0]) or \ |
|
409 |
(target == 'object' and card[1]) |
|
1451 | 410 |
|
0 | 411 |
def schema_relation(self): |
2126
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
412 |
"""return True if this relation type is used to build the schema""" |
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
413 |
return self.type in SCHEMA_TYPES |
1451 | 414 |
|
0 | 415 |
def physical_mode(self): |
416 |
"""return an appropriate mode for physical storage of this relation type: |
|
417 |
* 'subjectinline' if every possible subject cardinalities are 1 or ? |
|
418 |
* 'objectinline' if 'subjectinline' mode is not possible but every |
|
419 |
possible object cardinalities are 1 or ? |
|
420 |
* None if neither 'subjectinline' and 'objectinline' |
|
421 |
""" |
|
422 |
assert not self.final |
|
423 |
return self.inlined and 'subjectinline' or None |
|
424 |
||
425 |
def check_perm(self, session, action, *args, **kwargs): |
|
426 |
# NB: session may be a server session or a request object check user is |
|
427 |
# in an allowed group, if so that's enough internal sessions should |
|
428 |
# always stop there |
|
429 |
if session.user.matching_groups(self.get_groups(action)): |
|
1451 | 430 |
return |
0 | 431 |
# else if there is some rql expressions, check them |
432 |
if any(rqlexpr.check(session, *args, **kwargs) |
|
433 |
for rqlexpr in self.get_rqlexprs(action)): |
|
434 |
return |
|
435 |
raise Unauthorized(action, str(self)) |
|
436 |
||
437 |
def rql_expression(self, expression, mainvars=None, eid=None): |
|
438 |
"""rql expression factory""" |
|
439 |
if self.is_final(): |
|
440 |
return ERQLExpression(expression, mainvars, eid) |
|
441 |
return RRQLExpression(expression, mainvars, eid) |
|
442 |
||
1451 | 443 |
|
0 | 444 |
class CubicWebSchema(Schema): |
445 |
"""set of entities and relations schema defining the possible data sets |
|
446 |
used in an application |
|
447 |
||
448 |
||
449 |
:type name: str |
|
450 |
:ivar name: name of the schema, usually the application identifier |
|
1451 | 451 |
|
0 | 452 |
:type base: str |
453 |
:ivar base: path of the directory where the schema is defined |
|
454 |
""" |
|
1451 | 455 |
reading_from_database = False |
0 | 456 |
entity_class = CubicWebEntitySchema |
457 |
relation_class = CubicWebRelationSchema |
|
458 |
||
459 |
def __init__(self, *args, **kwargs): |
|
460 |
self._eid_index = {} |
|
461 |
super(CubicWebSchema, self).__init__(*args, **kwargs) |
|
462 |
ybo.register_base_types(self) |
|
463 |
rschema = self.add_relation_type(ybo.RelationType('eid', meta=True)) |
|
464 |
rschema.final = True |
|
465 |
rschema.set_default_groups() |
|
466 |
rschema = self.add_relation_type(ybo.RelationType('has_text', meta=True)) |
|
467 |
rschema.final = True |
|
468 |
rschema.set_default_groups() |
|
469 |
rschema = self.add_relation_type(ybo.RelationType('identity', meta=True)) |
|
470 |
rschema.final = False |
|
471 |
rschema.set_default_groups() |
|
1451 | 472 |
|
0 | 473 |
def add_entity_type(self, edef): |
474 |
edef.name = edef.name.encode() |
|
475 |
edef.name = bw_normalize_etype(edef.name) |
|
476 |
assert re.match(r'[A-Z][A-Za-z0-9]*[a-z]+[0-9]*$', edef.name), repr(edef.name) |
|
477 |
eschema = super(CubicWebSchema, self).add_entity_type(edef) |
|
478 |
if not eschema.is_final(): |
|
479 |
# automatically add the eid relation to non final entity types |
|
480 |
rdef = ybo.RelationDefinition(eschema.type, 'eid', 'Int', |
|
481 |
cardinality='11', uid=True) |
|
482 |
self.add_relation_def(rdef) |
|
483 |
rdef = ybo.RelationDefinition(eschema.type, 'identity', eschema.type) |
|
484 |
self.add_relation_def(rdef) |
|
485 |
self._eid_index[eschema.eid] = eschema |
|
486 |
return eschema |
|
1451 | 487 |
|
0 | 488 |
def add_relation_type(self, rdef): |
489 |
rdef.name = rdef.name.lower().encode() |
|
490 |
rschema = super(CubicWebSchema, self).add_relation_type(rdef) |
|
491 |
self._eid_index[rschema.eid] = rschema |
|
492 |
return rschema |
|
1451 | 493 |
|
0 | 494 |
def add_relation_def(self, rdef): |
495 |
"""build a part of a relation schema |
|
496 |
(i.e. add a relation between two specific entity's types) |
|
497 |
||
498 |
:type subject: str |
|
499 |
:param subject: entity's type that is subject of the relation |
|
500 |
||
501 |
:type rtype: str |
|
502 |
:param rtype: the relation's type (i.e. the name of the relation) |
|
503 |
||
504 |
:type obj: str |
|
505 |
:param obj: entity's type that is object of the relation |
|
506 |
||
507 |
:rtype: RelationSchema |
|
508 |
:param: the newly created or just completed relation schema |
|
509 |
""" |
|
510 |
rdef.name = rdef.name.lower() |
|
511 |
rdef.subject = bw_normalize_etype(rdef.subject) |
|
512 |
rdef.object = bw_normalize_etype(rdef.object) |
|
1034
0356bbfb2f26
fix to pass arguments to base class
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
513 |
if super(CubicWebSchema, self).add_relation_def(rdef): |
0356bbfb2f26
fix to pass arguments to base class
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
514 |
try: |
0356bbfb2f26
fix to pass arguments to base class
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
515 |
self._eid_index[rdef.eid] = (self.eschema(rdef.subject), |
0356bbfb2f26
fix to pass arguments to base class
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
516 |
self.rschema(rdef.name), |
0356bbfb2f26
fix to pass arguments to base class
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
517 |
self.eschema(rdef.object)) |
0356bbfb2f26
fix to pass arguments to base class
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
518 |
except AttributeError: |
0356bbfb2f26
fix to pass arguments to base class
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
519 |
pass # not a serialized schema |
1451 | 520 |
|
0 | 521 |
def del_relation_type(self, rtype): |
522 |
rschema = self.rschema(rtype) |
|
523 |
self._eid_index.pop(rschema.eid, None) |
|
524 |
super(CubicWebSchema, self).del_relation_type(rtype) |
|
1451 | 525 |
|
0 | 526 |
def del_relation_def(self, subjtype, rtype, objtype): |
527 |
for k, v in self._eid_index.items(): |
|
528 |
if v == (subjtype, rtype, objtype): |
|
529 |
del self._eid_index[k] |
|
530 |
super(CubicWebSchema, self).del_relation_def(subjtype, rtype, objtype) |
|
1451 | 531 |
|
0 | 532 |
def del_entity_type(self, etype): |
533 |
eschema = self.eschema(etype) |
|
534 |
self._eid_index.pop(eschema.eid, None) |
|
535 |
# deal with has_text first, else its automatic deletion (see above) |
|
536 |
# may trigger an error in ancestor's del_entity_type method |
|
537 |
if 'has_text' in eschema.subject_relations(): |
|
538 |
self.del_relation_def(etype, 'has_text', 'String') |
|
539 |
super(CubicWebSchema, self).del_entity_type(etype) |
|
1451 | 540 |
|
0 | 541 |
def schema_by_eid(self, eid): |
542 |
return self._eid_index[eid] |
|
543 |
||
544 |
||
545 |
# Possible constraints ######################################################## |
|
546 |
||
547 |
class RQLVocabularyConstraint(BaseConstraint): |
|
548 |
"""the rql vocabulary constraint : |
|
549 |
||
550 |
limit the proposed values to a set of entities returned by a rql query, |
|
551 |
but this is not enforced at the repository level |
|
1451 | 552 |
|
0 | 553 |
restriction is additional rql restriction that will be added to |
554 |
a predefined query, where the S and O variables respectivly represent |
|
555 |
the subject and the object of the relation |
|
556 |
""" |
|
1451 | 557 |
|
0 | 558 |
def __init__(self, restriction): |
559 |
self.restriction = restriction |
|
560 |
||
561 |
def serialize(self): |
|
562 |
return self.restriction |
|
1451 | 563 |
|
0 | 564 |
def deserialize(cls, value): |
565 |
return cls(value) |
|
566 |
deserialize = classmethod(deserialize) |
|
1451 | 567 |
|
0 | 568 |
def check(self, entity, rtype, value): |
569 |
"""return true if the value satisfy the constraint, else false""" |
|
570 |
# implemented as a hook in the repository |
|
571 |
return 1 |
|
572 |
||
573 |
def repo_check(self, session, eidfrom, rtype, eidto): |
|
574 |
"""raise ValidationError if the relation doesn't satisfy the constraint |
|
575 |
""" |
|
576 |
pass # this is a vocabulary constraint, not enforce |
|
1451 | 577 |
|
0 | 578 |
def __str__(self): |
579 |
return self.restriction |
|
580 |
||
581 |
def __repr__(self): |
|
582 |
return '<%s : %s>' % (self.__class__.__name__, repr(self.restriction)) |
|
583 |
||
584 |
||
585 |
class RQLConstraint(RQLVocabularyConstraint): |
|
586 |
"""the rql constraint is similar to the RQLVocabularyConstraint but |
|
587 |
are also enforced at the repository level |
|
588 |
""" |
|
589 |
def exec_query(self, session, eidfrom, eidto): |
|
590 |
rql = 'Any S,O WHERE S eid %(s)s, O eid %(o)s, ' + self.restriction |
|
591 |
return session.unsafe_execute(rql, {'s': eidfrom, 'o': eidto}, |
|
592 |
('s', 'o'), build_descr=False) |
|
593 |
def error(self, eid, rtype, msg): |
|
594 |
raise ValidationError(eid, {rtype: msg}) |
|
1451 | 595 |
|
0 | 596 |
def repo_check(self, session, eidfrom, rtype, eidto): |
597 |
"""raise ValidationError if the relation doesn't satisfy the constraint |
|
598 |
""" |
|
599 |
if not self.exec_query(session, eidfrom, eidto): |
|
600 |
# XXX at this point dunno if the validation error `occured` on |
|
601 |
# eidfrom or eidto (from user interface point of view) |
|
602 |
self.error(eidfrom, rtype, 'constraint %s failed' % self) |
|
603 |
||
604 |
||
605 |
class RQLUniqueConstraint(RQLConstraint): |
|
606 |
"""the unique rql constraint check that the result of the query isn't |
|
607 |
greater than one |
|
608 |
""" |
|
609 |
def repo_check(self, session, eidfrom, rtype, eidto): |
|
610 |
"""raise ValidationError if the relation doesn't satisfy the constraint |
|
611 |
""" |
|
612 |
if len(self.exec_query(session, eidfrom, eidto)) > 1: |
|
613 |
# XXX at this point dunno if the validation error `occured` on |
|
614 |
# eidfrom or eidto (from user interface point of view) |
|
615 |
self.error(eidfrom, rtype, 'unique constraint %s failed' % self) |
|
616 |
||
1451 | 617 |
|
0 | 618 |
def split_expression(rqlstring): |
619 |
for expr in rqlstring.split(','): |
|
620 |
for word in expr.split(): |
|
621 |
yield word |
|
1451 | 622 |
|
0 | 623 |
def normalize_expression(rqlstring): |
624 |
"""normalize an rql expression to ease schema synchronization (avoid |
|
625 |
suppressing and reinserting an expression if only a space has been added/removed |
|
626 |
for instance) |
|
627 |
""" |
|
628 |
return u', '.join(' '.join(expr.split()) for expr in rqlstring.split(',')) |
|
629 |
||
630 |
||
631 |
class RQLExpression(object): |
|
632 |
def __init__(self, expression, mainvars, eid): |
|
633 |
self.eid = eid # eid of the entity representing this rql expression |
|
634 |
if not isinstance(mainvars, unicode): |
|
635 |
mainvars = unicode(mainvars) |
|
636 |
self.mainvars = mainvars |
|
637 |
self.expression = normalize_expression(expression) |
|
638 |
try: |
|
639 |
self.rqlst = parse(self.full_rql, print_errors=False).children[0] |
|
640 |
except RQLSyntaxError: |
|
641 |
raise RQLSyntaxError(expression) |
|
642 |
for mainvar in mainvars.split(','): |
|
643 |
if len(self.rqlst.defined_vars[mainvar].references()) <= 2: |
|
2142
098aa2075903
include_schema_files is useless
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
644 |
_LOGGER.warn('You did not use the %s variable in your RQL ' |
098aa2075903
include_schema_files is useless
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
645 |
'expression %s', mainvar, self) |
1451 | 646 |
|
0 | 647 |
def __str__(self): |
648 |
return self.full_rql |
|
649 |
def __repr__(self): |
|
650 |
return '%s(%s)' % (self.__class__.__name__, self.full_rql) |
|
1451 | 651 |
|
0 | 652 |
def __deepcopy__(self, memo): |
653 |
return self.__class__(self.expression, self.mainvars) |
|
654 |
def __getstate__(self): |
|
655 |
return (self.expression, self.mainvars) |
|
656 |
def __setstate__(self, state): |
|
657 |
self.__init__(*state) |
|
1451 | 658 |
|
0 | 659 |
@cached |
660 |
def transform_has_permission(self): |
|
661 |
found = None |
|
662 |
rqlst = self.rqlst |
|
663 |
for var in rqlst.defined_vars.itervalues(): |
|
664 |
for varref in var.references(): |
|
665 |
rel = varref.relation() |
|
666 |
if rel is None: |
|
667 |
continue |
|
668 |
try: |
|
669 |
prefix, action, suffix = rel.r_type.split('_') |
|
670 |
except ValueError: |
|
671 |
continue |
|
672 |
if prefix != 'has' or suffix != 'permission' or \ |
|
673 |
not action in ('add', 'delete', 'update', 'read'): |
|
674 |
continue |
|
675 |
if found is None: |
|
676 |
found = [] |
|
677 |
rqlst.save_state() |
|
678 |
assert rel.children[0].name == 'U' |
|
679 |
objvar = rel.children[1].children[0].variable |
|
680 |
rqlst.remove_node(rel) |
|
681 |
selected = [v.name for v in rqlst.get_selected_variables()] |
|
682 |
if objvar.name not in selected: |
|
683 |
colindex = len(selected) |
|
684 |
rqlst.add_selected(objvar) |
|
685 |
else: |
|
686 |
colindex = selected.index(objvar.name) |
|
687 |
found.append((action, objvar, colindex)) |
|
688 |
# remove U eid %(u)s if U is not used in any other relation |
|
689 |
uvrefs = rqlst.defined_vars['U'].references() |
|
690 |
if len(uvrefs) == 1: |
|
691 |
rqlst.remove_node(uvrefs[0].relation()) |
|
692 |
if found is not None: |
|
693 |
rql = rqlst.as_string() |
|
694 |
if len(rqlst.selection) == 1 and isinstance(rqlst.where, nodes.Relation): |
|
695 |
# only "Any X WHERE X eid %(x)s" remaining, no need to execute the rql |
|
696 |
keyarg = rqlst.selection[0].name.lower() |
|
697 |
else: |
|
698 |
keyarg = None |
|
699 |
rqlst.recover() |
|
700 |
return rql, found, keyarg |
|
701 |
return rqlst.as_string(), None, None |
|
1451 | 702 |
|
0 | 703 |
def _check(self, session, **kwargs): |
704 |
"""return True if the rql expression is matching the given relation |
|
705 |
between fromeid and toeid |
|
706 |
||
707 |
session may actually be a request as well |
|
708 |
""" |
|
709 |
if self.eid is not None: |
|
710 |
key = (self.eid, tuple(sorted(kwargs.iteritems()))) |
|
711 |
try: |
|
712 |
return session.local_perm_cache[key] |
|
713 |
except KeyError: |
|
714 |
pass |
|
715 |
rql, has_perm_defs, keyarg = self.transform_has_permission() |
|
716 |
if keyarg is None: |
|
717 |
# on the server side, use unsafe_execute, but this is not available |
|
718 |
# on the client side (session is actually a request) |
|
719 |
execute = getattr(session, 'unsafe_execute', session.execute) |
|
720 |
# XXX what if 'u' in kwargs |
|
721 |
cachekey = kwargs.keys() |
|
722 |
kwargs['u'] = session.user.eid |
|
723 |
try: |
|
724 |
rset = execute(rql, kwargs, cachekey, build_descr=True) |
|
725 |
except NotImplementedError: |
|
726 |
self.critical('cant check rql expression, unsupported rql %s', rql) |
|
727 |
if self.eid is not None: |
|
728 |
session.local_perm_cache[key] = False |
|
729 |
return False |
|
730 |
except TypeResolverException, ex: |
|
731 |
# some expression may not be resolvable with current kwargs |
|
732 |
# (type conflict) |
|
733 |
self.warning('%s: %s', rql, str(ex)) |
|
734 |
if self.eid is not None: |
|
735 |
session.local_perm_cache[key] = False |
|
736 |
return False |
|
737 |
else: |
|
738 |
rset = session.eid_rset(kwargs[keyarg]) |
|
739 |
# if no special has_*_permission relation in the rql expression, just |
|
740 |
# check the result set contains something |
|
741 |
if has_perm_defs is None: |
|
742 |
if rset: |
|
743 |
if self.eid is not None: |
|
744 |
session.local_perm_cache[key] = True |
|
745 |
return True |
|
746 |
elif rset: |
|
747 |
# check every special has_*_permission relation is satisfied |
|
748 |
get_eschema = session.vreg.schema.eschema |
|
749 |
try: |
|
750 |
for eaction, var, col in has_perm_defs: |
|
751 |
for i in xrange(len(rset)): |
|
752 |
eschema = get_eschema(rset.description[i][col]) |
|
753 |
eschema.check_perm(session, eaction, rset[i][col]) |
|
754 |
if self.eid is not None: |
|
755 |
session.local_perm_cache[key] = True |
|
756 |
return True |
|
757 |
except Unauthorized: |
|
758 |
pass |
|
759 |
if self.eid is not None: |
|
760 |
session.local_perm_cache[key] = False |
|
761 |
return False |
|
1451 | 762 |
|
0 | 763 |
@property |
764 |
def minimal_rql(self): |
|
765 |
return 'Any %s WHERE %s' % (self.mainvars, self.expression) |
|
766 |
||
767 |
||
768 |
class ERQLExpression(RQLExpression): |
|
769 |
def __init__(self, expression, mainvars=None, eid=None): |
|
770 |
RQLExpression.__init__(self, expression, mainvars or 'X', eid) |
|
771 |
# syntax tree used by read security (inserted in queries when necessary |
|
772 |
self.snippet_rqlst = parse(self.minimal_rql, print_errors=False).children[0] |
|
773 |
||
774 |
@property |
|
775 |
def full_rql(self): |
|
776 |
rql = self.minimal_rql |
|
777 |
rqlst = getattr(self, 'rqlst', None) # may be not set yet |
|
778 |
if rqlst is not None: |
|
779 |
defined = rqlst.defined_vars |
|
780 |
else: |
|
781 |
defined = set(split_expression(self.expression)) |
|
782 |
if 'X' in defined: |
|
783 |
rql += ', X eid %(x)s' |
|
784 |
if 'U' in defined: |
|
785 |
rql += ', U eid %(u)s' |
|
786 |
return rql |
|
1451 | 787 |
|
0 | 788 |
def check(self, session, eid=None): |
789 |
if 'X' in self.rqlst.defined_vars: |
|
790 |
if eid is None: |
|
791 |
return False |
|
792 |
return self._check(session, x=eid) |
|
793 |
return self._check(session) |
|
1451 | 794 |
|
0 | 795 |
PyFileReader.context['ERQLExpression'] = ERQLExpression |
1451 | 796 |
|
0 | 797 |
class RRQLExpression(RQLExpression): |
798 |
def __init__(self, expression, mainvars=None, eid=None): |
|
799 |
if mainvars is None: |
|
800 |
defined = set(split_expression(expression)) |
|
801 |
mainvars = [] |
|
802 |
if 'S' in defined: |
|
803 |
mainvars.append('S') |
|
804 |
if 'O' in defined: |
|
805 |
mainvars.append('O') |
|
806 |
if not mainvars: |
|
807 |
raise Exception('unable to guess selection variables') |
|
808 |
mainvars = ','.join(mainvars) |
|
809 |
RQLExpression.__init__(self, expression, mainvars, eid) |
|
810 |
||
811 |
@property |
|
812 |
def full_rql(self): |
|
813 |
rql = self.minimal_rql |
|
814 |
rqlst = getattr(self, 'rqlst', None) # may be not set yet |
|
815 |
if rqlst is not None: |
|
816 |
defined = rqlst.defined_vars |
|
817 |
else: |
|
818 |
defined = set(split_expression(self.expression)) |
|
819 |
if 'S' in defined: |
|
820 |
rql += ', S eid %(s)s' |
|
821 |
if 'O' in defined: |
|
822 |
rql += ', O eid %(o)s' |
|
823 |
if 'U' in defined: |
|
824 |
rql += ', U eid %(u)s' |
|
825 |
return rql |
|
1451 | 826 |
|
0 | 827 |
def check(self, session, fromeid=None, toeid=None): |
828 |
kwargs = {} |
|
829 |
if 'S' in self.rqlst.defined_vars: |
|
830 |
if fromeid is None: |
|
831 |
return False |
|
832 |
kwargs['s'] = fromeid |
|
833 |
if 'O' in self.rqlst.defined_vars: |
|
834 |
if toeid is None: |
|
835 |
return False |
|
836 |
kwargs['o'] = toeid |
|
837 |
return self._check(session, **kwargs) |
|
1451 | 838 |
|
0 | 839 |
PyFileReader.context['RRQLExpression'] = RRQLExpression |
840 |
||
629
59b6542f5729
provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
628
diff
changeset
|
841 |
# workflow extensions ######################################################### |
59b6542f5729
provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
628
diff
changeset
|
842 |
|
59b6542f5729
provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
628
diff
changeset
|
843 |
class workflowable_definition(ybo.metadefinition): |
59b6542f5729
provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
628
diff
changeset
|
844 |
"""extends default EntityType's metaclass to add workflow relations |
59b6542f5729
provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
628
diff
changeset
|
845 |
(i.e. in_state and wf_info_for). |
59b6542f5729
provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
628
diff
changeset
|
846 |
This is the default metaclass for WorkflowableEntityType |
59b6542f5729
provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
628
diff
changeset
|
847 |
""" |
59b6542f5729
provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
628
diff
changeset
|
848 |
def __new__(mcs, name, bases, classdict): |
59b6542f5729
provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
628
diff
changeset
|
849 |
abstract = classdict.pop('abstract', False) |
59b6542f5729
provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
628
diff
changeset
|
850 |
defclass = super(workflowable_definition, mcs).__new__(mcs, name, bases, classdict) |
59b6542f5729
provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
628
diff
changeset
|
851 |
if not abstract: |
59b6542f5729
provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
628
diff
changeset
|
852 |
existing_rels = set(rdef.name for rdef in defclass.__relations__) |
59b6542f5729
provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
628
diff
changeset
|
853 |
if 'in_state' not in existing_rels and 'wf_info_for' not in existing_rels: |
59b6542f5729
provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
628
diff
changeset
|
854 |
in_state = ybo.SubjectRelation('State', cardinality='1*', |
59b6542f5729
provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
628
diff
changeset
|
855 |
# XXX automatize this |
59b6542f5729
provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
628
diff
changeset
|
856 |
constraints=[RQLConstraint('S is ET, O state_of ET')], |
59b6542f5729
provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
628
diff
changeset
|
857 |
description=_('account state')) |
59b6542f5729
provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
628
diff
changeset
|
858 |
yams_add_relation(defclass.__relations__, in_state, 'in_state') |
59b6542f5729
provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
628
diff
changeset
|
859 |
wf_info_for = ybo.ObjectRelation('TrInfo', cardinality='1*', composite='object') |
59b6542f5729
provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
628
diff
changeset
|
860 |
yams_add_relation(defclass.__relations__, wf_info_for, 'wf_info_for') |
59b6542f5729
provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
628
diff
changeset
|
861 |
return defclass |
59b6542f5729
provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
628
diff
changeset
|
862 |
|
59b6542f5729
provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
628
diff
changeset
|
863 |
class WorkflowableEntityType(ybo.EntityType): |
59b6542f5729
provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
628
diff
changeset
|
864 |
__metaclass__ = workflowable_definition |
59b6542f5729
provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
628
diff
changeset
|
865 |
abstract = True |
1451 | 866 |
|
629
59b6542f5729
provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
628
diff
changeset
|
867 |
PyFileReader.context['WorkflowableEntityType'] = WorkflowableEntityType |
59b6542f5729
provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
628
diff
changeset
|
868 |
|
0 | 869 |
# schema loading ############################################################## |
870 |
||
871 |
CONSTRAINTS['RQLConstraint'] = RQLConstraint |
|
872 |
CONSTRAINTS['RQLUniqueConstraint'] = RQLUniqueConstraint |
|
873 |
CONSTRAINTS['RQLVocabularyConstraint'] = RQLVocabularyConstraint |
|
874 |
PyFileReader.context.update(CONSTRAINTS) |
|
875 |
||
876 |
||
877 |
class BootstrapSchemaLoader(SchemaLoader): |
|
878 |
"""cubicweb specific schema loader, loading only schema necessary to read |
|
879 |
the persistent schema |
|
880 |
""" |
|
881 |
schemacls = CubicWebSchema |
|
882 |
||
1034
0356bbfb2f26
fix to pass arguments to base class
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
883 |
def load(self, config, path=(), **kwargs): |
0 | 884 |
"""return a Schema instance from the schema definition read |
885 |
from <directory> |
|
886 |
""" |
|
887 |
self.lib_directory = config.schemas_lib_dir() |
|
888 |
return super(BootstrapSchemaLoader, self).load( |
|
1034
0356bbfb2f26
fix to pass arguments to base class
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
889 |
path, config.appid, register_base_types=False, **kwargs) |
1451 | 890 |
|
0 | 891 |
def _load_definition_files(self, cubes=None): |
892 |
# bootstraping, ignore cubes |
|
2142
098aa2075903
include_schema_files is useless
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
893 |
filepath = join(self.lib_directory, 'bootstrap.py') |
098aa2075903
include_schema_files is useless
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
894 |
self.info('loading %s', filepath) |
098aa2075903
include_schema_files is useless
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
895 |
self.handle_file(filepath) |
1451 | 896 |
|
0 | 897 |
def unhandled_file(self, filepath): |
898 |
"""called when a file without handler associated has been found""" |
|
899 |
self.warning('ignoring file %r', filepath) |
|
900 |
||
901 |
||
902 |
class CubicWebSchemaLoader(BootstrapSchemaLoader): |
|
903 |
"""cubicweb specific schema loader, automatically adding metadata to the |
|
904 |
application's schema |
|
905 |
""" |
|
906 |
||
1034
0356bbfb2f26
fix to pass arguments to base class
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
907 |
def load(self, config, **kwargs): |
0 | 908 |
"""return a Schema instance from the schema definition read |
909 |
from <directory> |
|
910 |
""" |
|
911 |
self.info('loading %s schemas', ', '.join(config.cubes())) |
|
372
a8a975a88368
check apphome is not None
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
912 |
if config.apphome: |
a8a975a88368
check apphome is not None
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
913 |
path = reversed([config.apphome] + config.cubes_path()) |
a8a975a88368
check apphome is not None
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
914 |
else: |
a8a975a88368
check apphome is not None
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
915 |
path = reversed(config.cubes_path()) |
1034
0356bbfb2f26
fix to pass arguments to base class
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
916 |
return super(CubicWebSchemaLoader, self).load(config, path=path, **kwargs) |
0 | 917 |
|
918 |
def _load_definition_files(self, cubes): |
|
2142
098aa2075903
include_schema_files is useless
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
919 |
for filepath in (join(self.lib_directory, 'bootstrap.py'), |
098aa2075903
include_schema_files is useless
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
920 |
join(self.lib_directory, 'base.py'), |
098aa2075903
include_schema_files is useless
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
921 |
join(self.lib_directory, 'workflow.py'), |
098aa2075903
include_schema_files is useless
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
922 |
join(self.lib_directory, 'Bookmark.py')): |
0 | 923 |
self.info('loading %s', filepath) |
924 |
self.handle_file(filepath) |
|
925 |
for cube in cubes: |
|
926 |
for filepath in self.get_schema_files(cube): |
|
927 |
self.info('loading %s', filepath) |
|
928 |
self.handle_file(filepath) |
|
929 |
||
930 |
||
931 |
# _() is just there to add messages to the catalog, don't care about actual |
|
932 |
# translation |
|
933 |
PERM_USE_TEMPLATE_FORMAT = _('use_template_format') |
|
934 |
||
935 |
class FormatConstraint(StaticVocabularyConstraint): |
|
715 | 936 |
need_perm_formats = [_('text/cubicweb-page-template')] |
1451 | 937 |
|
0 | 938 |
regular_formats = (_('text/rest'), |
939 |
_('text/html'), |
|
940 |
_('text/plain'), |
|
941 |
) |
|
942 |
def __init__(self): |
|
943 |
pass |
|
1451 | 944 |
|
0 | 945 |
def serialize(self): |
946 |
"""called to make persistent valuable data of a constraint""" |
|
947 |
return None |
|
948 |
||
949 |
@classmethod |
|
950 |
def deserialize(cls, value): |
|
951 |
"""called to restore serialized data of a constraint. Should return |
|
952 |
a `cls` instance |
|
953 |
""" |
|
954 |
return cls() |
|
1451 | 955 |
|
1045
5040a5835e4d
accept req as parameter for convenience
sylvain.thenault@logilab.fr
parents:
1034
diff
changeset
|
956 |
def vocabulary(self, entity=None, req=None): |
5040a5835e4d
accept req as parameter for convenience
sylvain.thenault@logilab.fr
parents:
1034
diff
changeset
|
957 |
if req is None and entity is not None: |
5040a5835e4d
accept req as parameter for convenience
sylvain.thenault@logilab.fr
parents:
1034
diff
changeset
|
958 |
req = entity.req |
5040a5835e4d
accept req as parameter for convenience
sylvain.thenault@logilab.fr
parents:
1034
diff
changeset
|
959 |
if req is not None and req.user.has_permission(PERM_USE_TEMPLATE_FORMAT): |
745 | 960 |
return self.regular_formats + tuple(self.need_perm_formats) |
0 | 961 |
return self.regular_formats |
1451 | 962 |
|
0 | 963 |
def __str__(self): |
964 |
return 'value in (%s)' % u', '.join(repr(unicode(word)) for word in self.vocabulary()) |
|
1451 | 965 |
|
966 |
||
0 | 967 |
format_constraint = FormatConstraint() |
968 |
CONSTRAINTS['FormatConstraint'] = FormatConstraint |
|
969 |
PyFileReader.context['format_constraint'] = format_constraint |
|
970 |
||
971 |
set_log_methods(CubicWebSchemaLoader, getLogger('cubicweb.schemaloader')) |
|
972 |
set_log_methods(BootstrapSchemaLoader, getLogger('cubicweb.bootstrapschemaloader')) |
|
973 |
set_log_methods(RQLExpression, getLogger('cubicweb.schema')) |
|
974 |
||
975 |
# XXX monkey patch PyFileReader.import_erschema until bw_normalize_etype is |
|
976 |
# necessary |
|
977 |
orig_import_erschema = PyFileReader.import_erschema |
|
978 |
def bw_import_erschema(self, ertype, schemamod=None, instantiate=True): |
|
979 |
return orig_import_erschema(self, bw_normalize_etype(ertype), schemamod, instantiate) |
|
980 |
PyFileReader.import_erschema = bw_import_erschema |
|
1451 | 981 |
|
0 | 982 |
# XXX itou for some Statement methods |
983 |
from rql import stmts |
|
984 |
orig_get_etype = stmts.ScopeNode.get_etype |
|
985 |
def bw_get_etype(self, name): |
|
986 |
return orig_get_etype(self, bw_normalize_etype(name)) |
|
987 |
stmts.ScopeNode.get_etype = bw_get_etype |
|
988 |
||
989 |
orig_add_main_variable_delete = stmts.Delete.add_main_variable |
|
990 |
def bw_add_main_variable_delete(self, etype, vref): |
|
991 |
return orig_add_main_variable_delete(self, bw_normalize_etype(etype), vref) |
|
992 |
stmts.Delete.add_main_variable = bw_add_main_variable_delete |
|
993 |
||
994 |
orig_add_main_variable_insert = stmts.Insert.add_main_variable |
|
995 |
def bw_add_main_variable_insert(self, etype, vref): |
|
996 |
return orig_add_main_variable_insert(self, bw_normalize_etype(etype), vref) |
|
997 |
stmts.Insert.add_main_variable = bw_add_main_variable_insert |
|
998 |
||
999 |
orig_set_statement_type = stmts.Select.set_statement_type |
|
1000 |
def bw_set_statement_type(self, etype): |
|
1001 |
return orig_set_statement_type(self, bw_normalize_etype(etype)) |
|
1002 |
stmts.Select.set_statement_type = bw_set_statement_type |