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