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