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