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