author | Pierre-Yves David <pierre-yves.david@logilab.fr> |
Tue, 19 Jan 2010 09:51:38 +0100 | |
branch | stable |
changeset 4255 | 9d9400cb3f8c |
parent 4212 | ab6573088b4a |
child 4252 | 6c4f109c2b03 |
permissions | -rw-r--r-- |
0 | 1 |
"""classes to define schemas for CubicWeb |
2 |
||
3 |
:organization: Logilab |
|
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4132
diff
changeset
|
4 |
:copyright: 2001-2010 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 |
|
2613
5e19c2bb370e
R [all] logilab.common 0.44 provides only deprecated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2531
diff
changeset
|
120 |
__builtins__['display_name'] = deprecated('display_name should be imported from cubicweb.schema')(display_name) |
0 | 121 |
|
3978
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
122 |
|
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
123 |
# rql expression utilities function ############################################ |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
124 |
|
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
125 |
def guess_rrqlexpr_mainvars(expression): |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
126 |
defined = set(split_expression(expression)) |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
127 |
mainvars = [] |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
128 |
if 'S' in defined: |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
129 |
mainvars.append('S') |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
130 |
if 'O' in defined: |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
131 |
mainvars.append('O') |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
132 |
if 'U' in defined: |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
133 |
mainvars.append('U') |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
134 |
if not mainvars: |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
135 |
raise Exception('unable to guess selection variables') |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
136 |
return ','.join(mainvars) |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
137 |
|
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
138 |
def split_expression(rqlstring): |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
139 |
for expr in rqlstring.split(','): |
4132
440d383367eb
fix mainvars detection bug with EXISTS (paren, actually)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3985
diff
changeset
|
140 |
for noparen in expr.split('('): |
440d383367eb
fix mainvars detection bug with EXISTS (paren, actually)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3985
diff
changeset
|
141 |
for word in noparen.split(): |
440d383367eb
fix mainvars detection bug with EXISTS (paren, actually)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3985
diff
changeset
|
142 |
yield word |
3978
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
143 |
|
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
144 |
def normalize_expression(rqlstring): |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
145 |
"""normalize an rql expression to ease schema synchronization (avoid |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
146 |
suppressing and reinserting an expression if only a space has been added/removed |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
147 |
for instance) |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
148 |
""" |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
149 |
return u', '.join(' '.join(expr.split()) for expr in rqlstring.split(',')) |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
150 |
|
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
151 |
|
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
152 |
# Schema objects definition ################################################### |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
153 |
|
0 | 154 |
def ERSchema_display_name(self, req, form=''): |
155 |
"""return a internationalized string for the entity/relation type name in |
|
156 |
a given form |
|
157 |
""" |
|
158 |
return display_name(req, self.type, form) |
|
159 |
ERSchema.display_name = ERSchema_display_name |
|
160 |
||
161 |
@cached |
|
162 |
def ERSchema_get_groups(self, action): |
|
163 |
"""return the groups authorized to perform <action> on entities of |
|
164 |
this type |
|
165 |
||
166 |
:type action: str |
|
167 |
:param action: the name of a permission |
|
168 |
||
169 |
:rtype: tuple |
|
170 |
:return: names of the groups with the given permission |
|
171 |
""" |
|
172 |
assert action in self.ACTIONS, action |
|
173 |
#assert action in self._groups, '%s %s' % (self, action) |
|
174 |
try: |
|
175 |
return frozenset(g for g in self._groups[action] if isinstance(g, basestring)) |
|
176 |
except KeyError: |
|
177 |
return () |
|
178 |
ERSchema.get_groups = ERSchema_get_groups |
|
179 |
||
180 |
def ERSchema_set_groups(self, action, groups): |
|
181 |
"""set the groups allowed to perform <action> on entities of this type. Don't |
|
182 |
change rql expressions for the same action. |
|
183 |
||
184 |
:type action: str |
|
185 |
:param action: the name of a permission |
|
186 |
||
187 |
:type groups: list or tuple |
|
188 |
:param groups: names of the groups granted to do the given action |
|
189 |
""" |
|
190 |
assert action in self.ACTIONS, action |
|
191 |
clear_cache(self, 'ERSchema_get_groups') |
|
192 |
self._groups[action] = tuple(groups) + self.get_rqlexprs(action) |
|
193 |
ERSchema.set_groups = ERSchema_set_groups |
|
194 |
||
195 |
@cached |
|
196 |
def ERSchema_get_rqlexprs(self, action): |
|
197 |
"""return the rql expressions representing queries to check the user is allowed |
|
198 |
to perform <action> on entities of this type |
|
199 |
||
200 |
:type action: str |
|
201 |
:param action: the name of a permission |
|
202 |
||
203 |
:rtype: tuple |
|
204 |
:return: the rql expressions with the given permission |
|
205 |
""" |
|
206 |
assert action in self.ACTIONS, action |
|
207 |
#assert action in self._rqlexprs, '%s %s' % (self, action) |
|
208 |
try: |
|
209 |
return tuple(g for g in self._groups[action] if not isinstance(g, basestring)) |
|
210 |
except KeyError: |
|
211 |
return () |
|
212 |
ERSchema.get_rqlexprs = ERSchema_get_rqlexprs |
|
213 |
||
214 |
def ERSchema_set_rqlexprs(self, action, rqlexprs): |
|
215 |
"""set the rql expression allowing to perform <action> on entities of this type. Don't |
|
216 |
change groups for the same action. |
|
217 |
||
218 |
:type action: str |
|
219 |
:param action: the name of a permission |
|
220 |
||
221 |
:type rqlexprs: list or tuple |
|
222 |
:param rqlexprs: the rql expressions allowing the given action |
|
223 |
""" |
|
224 |
assert action in self.ACTIONS, action |
|
225 |
clear_cache(self, 'ERSchema_get_rqlexprs') |
|
226 |
self._groups[action] = tuple(self.get_groups(action)) + tuple(rqlexprs) |
|
227 |
ERSchema.set_rqlexprs = ERSchema_set_rqlexprs |
|
228 |
||
229 |
def ERSchema_set_permissions(self, action, permissions): |
|
230 |
"""set the groups and rql expressions allowing to perform <action> on |
|
231 |
entities of this type |
|
232 |
||
233 |
:type action: str |
|
234 |
:param action: the name of a permission |
|
235 |
||
236 |
:type permissions: tuple |
|
237 |
:param permissions: the groups and rql expressions allowing the given action |
|
238 |
""" |
|
239 |
assert action in self.ACTIONS, action |
|
240 |
clear_cache(self, 'ERSchema_get_rqlexprs') |
|
241 |
clear_cache(self, 'ERSchema_get_groups') |
|
242 |
self._groups[action] = tuple(permissions) |
|
243 |
ERSchema.set_permissions = ERSchema_set_permissions |
|
244 |
||
245 |
def ERSchema_has_perm(self, session, action, *args, **kwargs): |
|
246 |
"""return true if the action is granted globaly or localy""" |
|
247 |
try: |
|
248 |
self.check_perm(session, action, *args, **kwargs) |
|
249 |
return True |
|
250 |
except Unauthorized: |
|
251 |
return False |
|
252 |
ERSchema.has_perm = ERSchema_has_perm |
|
253 |
||
254 |
def ERSchema_has_local_role(self, action): |
|
255 |
"""return true if the action *may* be granted localy (eg either rql |
|
256 |
expressions or the owners group are used in security definition) |
|
257 |
||
258 |
XXX this method is only there since we don't know well how to deal with |
|
259 |
'add' action checking. Also find a better name would be nice. |
|
260 |
""" |
|
261 |
assert action in self.ACTIONS, action |
|
262 |
if self.get_rqlexprs(action): |
|
263 |
return True |
|
264 |
if action in ('update', 'delete'): |
|
265 |
return self.has_group(action, 'owners') |
|
266 |
return False |
|
267 |
ERSchema.has_local_role = ERSchema_has_local_role |
|
268 |
||
269 |
||
270 |
class CubicWebEntitySchema(EntitySchema): |
|
271 |
"""a entity has a type, a set of subject and or object relations |
|
272 |
the entity schema defines the possible relations for a given type and some |
|
273 |
constraints on those relations |
|
274 |
""" |
|
275 |
def __init__(self, schema=None, edef=None, eid=None, **kwargs): |
|
276 |
super(CubicWebEntitySchema, self).__init__(schema, edef, **kwargs) |
|
277 |
if eid is None and edef is not None: |
|
278 |
eid = getattr(edef, 'eid', None) |
|
279 |
self.eid = eid |
|
280 |
# take care: no _groups attribute when deep-copying |
|
1451 | 281 |
if getattr(self, '_groups', None): |
0 | 282 |
for groups in self._groups.itervalues(): |
283 |
for group_or_rqlexpr in groups: |
|
284 |
if isinstance(group_or_rqlexpr, RRQLExpression): |
|
285 |
msg = "can't use RRQLExpression on an entity type, use an ERQLExpression (%s)" |
|
286 |
raise BadSchemaDefinition(msg % self.type) |
|
1451 | 287 |
|
0 | 288 |
def attribute_definitions(self): |
289 |
"""return an iterator on attribute definitions |
|
1451 | 290 |
|
0 | 291 |
attribute relations are a subset of subject relations where the |
292 |
object's type is a final entity |
|
1451 | 293 |
|
0 | 294 |
an attribute definition is a 2-uple : |
295 |
* name of the relation |
|
296 |
* schema of the destination entity type |
|
297 |
""" |
|
298 |
iter = super(CubicWebEntitySchema, self).attribute_definitions() |
|
299 |
for rschema, attrschema in iter: |
|
300 |
if rschema.type == 'has_text': |
|
301 |
continue |
|
302 |
yield rschema, attrschema |
|
1451 | 303 |
|
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
|
304 |
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
|
305 |
"""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
|
306 |
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
|
307 |
""" |
464edb198faa
drop @ wildcard in relation subject/object, override main_attribute for proper behaviour
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2126
diff
changeset
|
308 |
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
|
309 |
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
|
310 |
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
|
311 |
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
|
312 |
|
0 | 313 |
def add_subject_relation(self, rschema): |
314 |
"""register the relation schema as possible subject relation""" |
|
315 |
super(CubicWebEntitySchema, self).add_subject_relation(rschema) |
|
316 |
self._update_has_text() |
|
317 |
||
318 |
def del_subject_relation(self, rtype): |
|
319 |
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
|
320 |
self._update_has_text(True) |
1451 | 321 |
|
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 |
def _update_has_text(self, deletion=False): |
0 | 323 |
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
|
324 |
need_has_text = None |
0 | 325 |
for rschema in self.subject_relations(): |
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3554
diff
changeset
|
326 |
if rschema.final: |
0 | 327 |
if rschema == 'has_text': |
328 |
has_has_text = True |
|
329 |
elif self.rproperty(rschema, 'fulltextindexed'): |
|
330 |
may_need_has_text = True |
|
331 |
elif rschema.fulltext_container: |
|
332 |
if rschema.fulltext_container == 'subject': |
|
333 |
may_need_has_text = True |
|
334 |
else: |
|
335 |
need_has_text = False |
|
336 |
for rschema in self.object_relations(): |
|
337 |
if rschema.fulltext_container: |
|
338 |
if rschema.fulltext_container == 'object': |
|
339 |
may_need_has_text = True |
|
340 |
else: |
|
341 |
need_has_text = False |
|
342 |
if need_has_text is None: |
|
343 |
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
|
344 |
if need_has_text and not has_has_text and not deletion: |
0 | 345 |
rdef = ybo.RelationDefinition(self.type, 'has_text', 'String') |
346 |
self.schema.add_relation_def(rdef) |
|
347 |
elif not need_has_text and has_has_text: |
|
348 |
self.schema.del_relation_def(self.type, 'has_text', 'String') |
|
1451 | 349 |
|
0 | 350 |
def schema_entity(self): |
351 |
"""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
|
352 |
return self.type in SCHEMA_TYPES |
0 | 353 |
|
354 |
def check_perm(self, session, action, eid=None): |
|
355 |
# NB: session may be a server session or a request object |
|
356 |
user = session.user |
|
357 |
# check user is in an allowed group, if so that's enough |
|
358 |
# internal sessions should always stop there |
|
359 |
if user.matching_groups(self.get_groups(action)): |
|
360 |
return |
|
361 |
# if 'owners' in allowed groups, check if the user actually owns this |
|
362 |
# object, if so that's enough |
|
363 |
if eid is not None and 'owners' in self.get_groups(action) and \ |
|
364 |
user.owns(eid): |
|
365 |
return |
|
366 |
# else if there is some rql expressions, check them |
|
367 |
if any(rqlexpr.check(session, eid) |
|
368 |
for rqlexpr in self.get_rqlexprs(action)): |
|
1451 | 369 |
return |
0 | 370 |
raise Unauthorized(action, str(self)) |
371 |
||
372 |
def rql_expression(self, expression, mainvars=None, eid=None): |
|
373 |
"""rql expression factory""" |
|
374 |
return ERQLExpression(expression, mainvars, eid) |
|
1451 | 375 |
|
2252 | 376 |
|
0 | 377 |
class CubicWebRelationSchema(RelationSchema): |
378 |
RelationSchema._RPROPERTIES['eid'] = None |
|
379 |
_perms_checked = False |
|
1451 | 380 |
|
0 | 381 |
def __init__(self, schema=None, rdef=None, eid=None, **kwargs): |
382 |
if rdef is not None: |
|
383 |
# if this relation is inlined |
|
384 |
self.inlined = rdef.inlined |
|
385 |
super(CubicWebRelationSchema, self).__init__(schema, rdef, **kwargs) |
|
386 |
if eid is None and rdef is not None: |
|
387 |
eid = getattr(rdef, 'eid', None) |
|
388 |
self.eid = eid |
|
1451 | 389 |
|
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
|
390 |
@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
|
391 |
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
|
392 |
return self.type in META_RTYPES |
1451 | 393 |
|
0 | 394 |
def update(self, subjschema, objschema, rdef): |
395 |
super(CubicWebRelationSchema, self).update(subjschema, objschema, rdef) |
|
396 |
if not self._perms_checked and self._groups: |
|
397 |
for action, groups in self._groups.iteritems(): |
|
398 |
for group_or_rqlexpr in groups: |
|
399 |
if action == 'read' and \ |
|
400 |
isinstance(group_or_rqlexpr, RQLExpression): |
|
401 |
msg = "can't use rql expression for read permission of "\ |
|
402 |
"a relation type (%s)" |
|
403 |
raise BadSchemaDefinition(msg % self.type) |
|
404 |
elif self.final and isinstance(group_or_rqlexpr, RRQLExpression): |
|
405 |
if self.schema.reading_from_database: |
|
406 |
# we didn't have final relation earlier, so turn |
|
407 |
# RRQLExpression into ERQLExpression now |
|
408 |
rqlexpr = group_or_rqlexpr |
|
409 |
newrqlexprs = [x for x in self.get_rqlexprs(action) if not x is rqlexpr] |
|
410 |
newrqlexprs.append(ERQLExpression(rqlexpr.expression, |
|
411 |
rqlexpr.mainvars, |
|
412 |
rqlexpr.eid)) |
|
1451 | 413 |
self.set_rqlexprs(action, newrqlexprs) |
0 | 414 |
else: |
415 |
msg = "can't use RRQLExpression on a final relation "\ |
|
416 |
"type (eg attribute relation), use an ERQLExpression (%s)" |
|
417 |
raise BadSchemaDefinition(msg % self.type) |
|
418 |
elif not self.final and \ |
|
419 |
isinstance(group_or_rqlexpr, ERQLExpression): |
|
420 |
msg = "can't use ERQLExpression on a relation type, use "\ |
|
421 |
"a RRQLExpression (%s)" |
|
422 |
raise BadSchemaDefinition(msg % self.type) |
|
423 |
self._perms_checked = True |
|
1451 | 424 |
|
0 | 425 |
def cardinality(self, subjtype, objtype, target): |
426 |
card = self.rproperty(subjtype, objtype, 'cardinality') |
|
427 |
return (target == 'subject' and card[0]) or \ |
|
428 |
(target == 'object' and card[1]) |
|
1451 | 429 |
|
0 | 430 |
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
|
431 |
"""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
|
432 |
return self.type in SCHEMA_TYPES |
1451 | 433 |
|
0 | 434 |
def physical_mode(self): |
435 |
"""return an appropriate mode for physical storage of this relation type: |
|
436 |
* 'subjectinline' if every possible subject cardinalities are 1 or ? |
|
437 |
* 'objectinline' if 'subjectinline' mode is not possible but every |
|
438 |
possible object cardinalities are 1 or ? |
|
439 |
* None if neither 'subjectinline' and 'objectinline' |
|
440 |
""" |
|
441 |
assert not self.final |
|
442 |
return self.inlined and 'subjectinline' or None |
|
443 |
||
444 |
def check_perm(self, session, action, *args, **kwargs): |
|
445 |
# NB: session may be a server session or a request object check user is |
|
446 |
# in an allowed group, if so that's enough internal sessions should |
|
447 |
# always stop there |
|
448 |
if session.user.matching_groups(self.get_groups(action)): |
|
1451 | 449 |
return |
0 | 450 |
# else if there is some rql expressions, check them |
451 |
if any(rqlexpr.check(session, *args, **kwargs) |
|
452 |
for rqlexpr in self.get_rqlexprs(action)): |
|
453 |
return |
|
454 |
raise Unauthorized(action, str(self)) |
|
455 |
||
456 |
def rql_expression(self, expression, mainvars=None, eid=None): |
|
457 |
"""rql expression factory""" |
|
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3554
diff
changeset
|
458 |
if self.final: |
0 | 459 |
return ERQLExpression(expression, mainvars, eid) |
460 |
return RRQLExpression(expression, mainvars, eid) |
|
461 |
||
1451 | 462 |
|
0 | 463 |
class CubicWebSchema(Schema): |
464 |
"""set of entities and relations schema defining the possible data sets |
|
465 |
used in an application |
|
466 |
||
467 |
||
468 |
:type name: str |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2460
diff
changeset
|
469 |
:ivar name: name of the schema, usually the instance identifier |
1451 | 470 |
|
0 | 471 |
:type base: str |
472 |
:ivar base: path of the directory where the schema is defined |
|
473 |
""" |
|
1451 | 474 |
reading_from_database = False |
0 | 475 |
entity_class = CubicWebEntitySchema |
476 |
relation_class = CubicWebRelationSchema |
|
2958
44e5446b649b
no inference for the identity relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2926
diff
changeset
|
477 |
no_specialization_inference = ('identity',) |
0 | 478 |
|
479 |
def __init__(self, *args, **kwargs): |
|
480 |
self._eid_index = {} |
|
481 |
super(CubicWebSchema, self).__init__(*args, **kwargs) |
|
482 |
ybo.register_base_types(self) |
|
2300
c8151d004e06
meta has been drop
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2293
diff
changeset
|
483 |
rschema = self.add_relation_type(ybo.RelationType('eid')) |
0 | 484 |
rschema.final = True |
485 |
rschema.set_default_groups() |
|
2300
c8151d004e06
meta has been drop
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2293
diff
changeset
|
486 |
rschema = self.add_relation_type(ybo.RelationType('has_text')) |
0 | 487 |
rschema.final = True |
488 |
rschema.set_default_groups() |
|
2300
c8151d004e06
meta has been drop
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2293
diff
changeset
|
489 |
rschema = self.add_relation_type(ybo.RelationType('identity')) |
0 | 490 |
rschema.final = False |
491 |
rschema.set_default_groups() |
|
1451 | 492 |
|
0 | 493 |
def add_entity_type(self, edef): |
494 |
edef.name = edef.name.encode() |
|
495 |
edef.name = bw_normalize_etype(edef.name) |
|
496 |
assert re.match(r'[A-Z][A-Za-z0-9]*[a-z]+[0-9]*$', edef.name), repr(edef.name) |
|
497 |
eschema = super(CubicWebSchema, self).add_entity_type(edef) |
|
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3554
diff
changeset
|
498 |
if not eschema.final: |
0 | 499 |
# automatically add the eid relation to non final entity types |
500 |
rdef = ybo.RelationDefinition(eschema.type, 'eid', 'Int', |
|
501 |
cardinality='11', uid=True) |
|
502 |
self.add_relation_def(rdef) |
|
503 |
rdef = ybo.RelationDefinition(eschema.type, 'identity', eschema.type) |
|
504 |
self.add_relation_def(rdef) |
|
505 |
self._eid_index[eschema.eid] = eschema |
|
506 |
return eschema |
|
1451 | 507 |
|
0 | 508 |
def add_relation_type(self, rdef): |
509 |
rdef.name = rdef.name.lower().encode() |
|
510 |
rschema = super(CubicWebSchema, self).add_relation_type(rdef) |
|
511 |
self._eid_index[rschema.eid] = rschema |
|
512 |
return rschema |
|
1451 | 513 |
|
0 | 514 |
def add_relation_def(self, rdef): |
515 |
"""build a part of a relation schema |
|
516 |
(i.e. add a relation between two specific entity's types) |
|
517 |
||
518 |
:type subject: str |
|
519 |
:param subject: entity's type that is subject of the relation |
|
520 |
||
521 |
:type rtype: str |
|
522 |
:param rtype: the relation's type (i.e. the name of the relation) |
|
523 |
||
524 |
:type obj: str |
|
525 |
:param obj: entity's type that is object of the relation |
|
526 |
||
527 |
:rtype: RelationSchema |
|
528 |
:param: the newly created or just completed relation schema |
|
529 |
""" |
|
530 |
rdef.name = rdef.name.lower() |
|
531 |
rdef.subject = bw_normalize_etype(rdef.subject) |
|
532 |
rdef.object = bw_normalize_etype(rdef.object) |
|
1034
0356bbfb2f26
fix to pass arguments to base class
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
533 |
if super(CubicWebSchema, self).add_relation_def(rdef): |
0356bbfb2f26
fix to pass arguments to base class
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
534 |
try: |
0356bbfb2f26
fix to pass arguments to base class
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
535 |
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
|
536 |
self.rschema(rdef.name), |
0356bbfb2f26
fix to pass arguments to base class
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
537 |
self.eschema(rdef.object)) |
0356bbfb2f26
fix to pass arguments to base class
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
538 |
except AttributeError: |
0356bbfb2f26
fix to pass arguments to base class
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
539 |
pass # not a serialized schema |
1451 | 540 |
|
0 | 541 |
def del_relation_type(self, rtype): |
542 |
rschema = self.rschema(rtype) |
|
543 |
self._eid_index.pop(rschema.eid, None) |
|
544 |
super(CubicWebSchema, self).del_relation_type(rtype) |
|
1451 | 545 |
|
0 | 546 |
def del_relation_def(self, subjtype, rtype, objtype): |
547 |
for k, v in self._eid_index.items(): |
|
548 |
if v == (subjtype, rtype, objtype): |
|
549 |
del self._eid_index[k] |
|
2718
3a56b87bb5d6
[schema] break when found
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2716
diff
changeset
|
550 |
break |
0 | 551 |
super(CubicWebSchema, self).del_relation_def(subjtype, rtype, objtype) |
1451 | 552 |
|
0 | 553 |
def del_entity_type(self, etype): |
554 |
eschema = self.eschema(etype) |
|
555 |
self._eid_index.pop(eschema.eid, None) |
|
556 |
# deal with has_text first, else its automatic deletion (see above) |
|
557 |
# may trigger an error in ancestor's del_entity_type method |
|
558 |
if 'has_text' in eschema.subject_relations(): |
|
559 |
self.del_relation_def(etype, 'has_text', 'String') |
|
560 |
super(CubicWebSchema, self).del_entity_type(etype) |
|
1451 | 561 |
|
0 | 562 |
def schema_by_eid(self, eid): |
563 |
return self._eid_index[eid] |
|
564 |
||
565 |
||
566 |
# Possible constraints ######################################################## |
|
567 |
||
3978
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
568 |
class BaseRQLConstraint(BaseConstraint): |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
569 |
"""base class for rql constraints |
0 | 570 |
""" |
1451 | 571 |
|
3961
d1cbf77db999
fix RQLUniqueConstraint behaviour by using a DISTINCT query and allowing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
572 |
def __init__(self, restriction, mainvars=None): |
3963
0d592677e55f
nicer mainvars/expression handling when initializing rql constraints
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3961
diff
changeset
|
573 |
self.restriction = normalize_expression(restriction) |
3961
d1cbf77db999
fix RQLUniqueConstraint behaviour by using a DISTINCT query and allowing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
574 |
if mainvars is None: |
d1cbf77db999
fix RQLUniqueConstraint behaviour by using a DISTINCT query and allowing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
575 |
mainvars = guess_rrqlexpr_mainvars(restriction) |
3963
0d592677e55f
nicer mainvars/expression handling when initializing rql constraints
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3961
diff
changeset
|
576 |
else: |
0d592677e55f
nicer mainvars/expression handling when initializing rql constraints
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3961
diff
changeset
|
577 |
normmainvars = [] |
0d592677e55f
nicer mainvars/expression handling when initializing rql constraints
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3961
diff
changeset
|
578 |
for mainvar in mainvars.split(','): |
0d592677e55f
nicer mainvars/expression handling when initializing rql constraints
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3961
diff
changeset
|
579 |
mainvar = mainvar.strip() |
0d592677e55f
nicer mainvars/expression handling when initializing rql constraints
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3961
diff
changeset
|
580 |
if not mainvar.isalpha(): |
0d592677e55f
nicer mainvars/expression handling when initializing rql constraints
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3961
diff
changeset
|
581 |
raise Exception('bad mainvars %s' % mainvars) |
0d592677e55f
nicer mainvars/expression handling when initializing rql constraints
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3961
diff
changeset
|
582 |
normmainvars.append(mainvar) |
0d592677e55f
nicer mainvars/expression handling when initializing rql constraints
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3961
diff
changeset
|
583 |
assert mainvars, 'bad mainvars %s' % mainvars |
0d592677e55f
nicer mainvars/expression handling when initializing rql constraints
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3961
diff
changeset
|
584 |
mainvars = ','.join(sorted(normmainvars)) |
3961
d1cbf77db999
fix RQLUniqueConstraint behaviour by using a DISTINCT query and allowing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
585 |
self.mainvars = mainvars |
0 | 586 |
|
587 |
def serialize(self): |
|
3961
d1cbf77db999
fix RQLUniqueConstraint behaviour by using a DISTINCT query and allowing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
588 |
# start with a comma for bw compat, see below |
d1cbf77db999
fix RQLUniqueConstraint behaviour by using a DISTINCT query and allowing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
589 |
return ';' + self.mainvars + ';' + self.restriction |
1451 | 590 |
|
0 | 591 |
def deserialize(cls, value): |
3961
d1cbf77db999
fix RQLUniqueConstraint behaviour by using a DISTINCT query and allowing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
592 |
# XXX < 3.5.10 bw compat |
d1cbf77db999
fix RQLUniqueConstraint behaviour by using a DISTINCT query and allowing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
593 |
if not value.startswith(';'): |
d1cbf77db999
fix RQLUniqueConstraint behaviour by using a DISTINCT query and allowing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
594 |
return cls(value) |
d1cbf77db999
fix RQLUniqueConstraint behaviour by using a DISTINCT query and allowing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
595 |
_, mainvars, restriction = value.split(';', 2) |
d1cbf77db999
fix RQLUniqueConstraint behaviour by using a DISTINCT query and allowing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
596 |
return cls(restriction, mainvars) |
0 | 597 |
deserialize = classmethod(deserialize) |
1451 | 598 |
|
0 | 599 |
def check(self, entity, rtype, value): |
600 |
"""return true if the value satisfy the constraint, else false""" |
|
601 |
# implemented as a hook in the repository |
|
602 |
return 1 |
|
603 |
||
604 |
def repo_check(self, session, eidfrom, rtype, eidto): |
|
605 |
"""raise ValidationError if the relation doesn't satisfy the constraint |
|
606 |
""" |
|
3554
26e586f3c15c
[schema] make RQL* constraints usable w/ attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3347
diff
changeset
|
607 |
pass # this is a vocabulary constraint, not enforce XXX why? |
1451 | 608 |
|
0 | 609 |
def __str__(self): |
3965
94f95928f5ae
nicer __str__ and __repr__ methods on rql constraints
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3964
diff
changeset
|
610 |
return '%s(Any %s WHERE %s)' % (self.__class__.__name__, self.mainvars, |
94f95928f5ae
nicer __str__ and __repr__ methods on rql constraints
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3964
diff
changeset
|
611 |
self.restriction) |
0 | 612 |
|
613 |
def __repr__(self): |
|
3965
94f95928f5ae
nicer __str__ and __repr__ methods on rql constraints
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3964
diff
changeset
|
614 |
return '<%s @%#x>' % (self.__str__(), id(self)) |
0 | 615 |
|
616 |
||
3978
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
617 |
class RQLVocabularyConstraint(BaseRQLConstraint): |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
618 |
"""the rql vocabulary constraint : |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
619 |
|
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
620 |
limit the proposed values to a set of entities returned by a rql query, |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
621 |
but this is not enforced at the repository level |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
622 |
|
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
623 |
restriction is additional rql restriction that will be added to |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
624 |
a predefined query, where the S and O variables respectivly represent |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
625 |
the subject and the object of the relation |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
626 |
|
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
627 |
mainvars is a string that should be used as selection variable (eg |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
628 |
`'Any %s WHERE ...' % mainvars`). If not specified, an attempt will be |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
629 |
done to guess it according to variable used in the expression. |
0 | 630 |
""" |
3978
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
631 |
|
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
632 |
|
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
633 |
class RepoEnforcedRQLConstraintMixIn(object): |
3961
d1cbf77db999
fix RQLUniqueConstraint behaviour by using a DISTINCT query and allowing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
634 |
|
3964
21387ffb0731
refactor so that we can specify an extra argument to constraints which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3963
diff
changeset
|
635 |
def __init__(self, restriction, mainvars=None, msg=None): |
3978
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
636 |
super(RepoEnforcedRQLConstraintMixIn, self).__init__(restriction, mainvars) |
3964
21387ffb0731
refactor so that we can specify an extra argument to constraints which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3963
diff
changeset
|
637 |
self.msg = msg |
21387ffb0731
refactor so that we can specify an extra argument to constraints which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3963
diff
changeset
|
638 |
|
21387ffb0731
refactor so that we can specify an extra argument to constraints which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3963
diff
changeset
|
639 |
def serialize(self): |
3968
e8dbad65a7a2
fix format string
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3967
diff
changeset
|
640 |
# start with a semicolon for bw compat, see below |
e8dbad65a7a2
fix format string
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3967
diff
changeset
|
641 |
return ';%s;%s\n%s' % (self.mainvars, self.restriction, |
3964
21387ffb0731
refactor so that we can specify an extra argument to constraints which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3963
diff
changeset
|
642 |
self.msg or '') |
21387ffb0731
refactor so that we can specify an extra argument to constraints which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3963
diff
changeset
|
643 |
|
21387ffb0731
refactor so that we can specify an extra argument to constraints which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3963
diff
changeset
|
644 |
def deserialize(cls, value): |
21387ffb0731
refactor so that we can specify an extra argument to constraints which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3963
diff
changeset
|
645 |
# XXX < 3.5.10 bw compat |
21387ffb0731
refactor so that we can specify an extra argument to constraints which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3963
diff
changeset
|
646 |
if not value.startswith(';'): |
21387ffb0731
refactor so that we can specify an extra argument to constraints which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3963
diff
changeset
|
647 |
return cls(value) |
21387ffb0731
refactor so that we can specify an extra argument to constraints which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3963
diff
changeset
|
648 |
value, msg = value.split('\n', 1) |
21387ffb0731
refactor so that we can specify an extra argument to constraints which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3963
diff
changeset
|
649 |
_, mainvars, restriction = value.split(';', 2) |
21387ffb0731
refactor so that we can specify an extra argument to constraints which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3963
diff
changeset
|
650 |
return cls(restriction, mainvars, msg) |
21387ffb0731
refactor so that we can specify an extra argument to constraints which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3963
diff
changeset
|
651 |
deserialize = classmethod(deserialize) |
21387ffb0731
refactor so that we can specify an extra argument to constraints which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3963
diff
changeset
|
652 |
|
3554
26e586f3c15c
[schema] make RQL* constraints usable w/ attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3347
diff
changeset
|
653 |
def repo_check(self, session, eidfrom, rtype, eidto=None): |
0 | 654 |
"""raise ValidationError if the relation doesn't satisfy the constraint |
655 |
""" |
|
3978
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
656 |
if not self.match_condition(session, eidfrom, eidto): |
3964
21387ffb0731
refactor so that we can specify an extra argument to constraints which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3963
diff
changeset
|
657 |
# XXX at this point if both or neither of S and O are in mainvar we |
21387ffb0731
refactor so that we can specify an extra argument to constraints which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3963
diff
changeset
|
658 |
# dunno if the validation error `occured` on eidfrom or eidto (from |
21387ffb0731
refactor so that we can specify an extra argument to constraints which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3963
diff
changeset
|
659 |
# user interface point of view) |
21387ffb0731
refactor so that we can specify an extra argument to constraints which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3963
diff
changeset
|
660 |
if eidto is None or 'S' in self.mainvars or not 'O' in self.mainvars: |
21387ffb0731
refactor so that we can specify an extra argument to constraints which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3963
diff
changeset
|
661 |
maineid = eidfrom |
21387ffb0731
refactor so that we can specify an extra argument to constraints which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3963
diff
changeset
|
662 |
else: |
21387ffb0731
refactor so that we can specify an extra argument to constraints which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3963
diff
changeset
|
663 |
maineid = eidto |
21387ffb0731
refactor so that we can specify an extra argument to constraints which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3963
diff
changeset
|
664 |
if self.msg: |
21387ffb0731
refactor so that we can specify an extra argument to constraints which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3963
diff
changeset
|
665 |
msg = session._(self.msg) |
21387ffb0731
refactor so that we can specify an extra argument to constraints which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3963
diff
changeset
|
666 |
else: |
21387ffb0731
refactor so that we can specify an extra argument to constraints which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3963
diff
changeset
|
667 |
msg = '%(constraint)s %(restriction)s failed' % { |
21387ffb0731
refactor so that we can specify an extra argument to constraints which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3963
diff
changeset
|
668 |
'constraint': session._(self.type()), |
21387ffb0731
refactor so that we can specify an extra argument to constraints which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3963
diff
changeset
|
669 |
'restriction': self.restriction} |
21387ffb0731
refactor so that we can specify an extra argument to constraints which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3963
diff
changeset
|
670 |
raise ValidationError(maineid, {rtype: msg}) |
0 | 671 |
|
3978
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
672 |
def exec_query(self, session, eidfrom, eidto): |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
673 |
if eidto is None: |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
674 |
# checking constraint for an attribute relation |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
675 |
restriction = 'S eid %(s)s, ' + self.restriction |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
676 |
args, ck = {'s': eidfrom}, 's' |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
677 |
else: |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
678 |
restriction = 'S eid %(s)s, O eid %(o)s, ' + self.restriction |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
679 |
args, ck = {'s': eidfrom, 'o': eidto}, ('s', 'o') |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
680 |
rql = 'Any %s WHERE %s' % (self.mainvars, restriction) |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
681 |
if self.distinct_query: |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
682 |
rql = 'DISTINCT ' + rql |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
683 |
return session.unsafe_execute(rql, args, ck, build_descr=False) |
0 | 684 |
|
3978
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
685 |
|
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
686 |
class RQLConstraint(RepoEnforcedRQLConstraintMixIn, RQLVocabularyConstraint): |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
687 |
"""the rql constraint is similar to the RQLVocabularyConstraint but |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
688 |
are also enforced at the repository level |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
689 |
""" |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
690 |
distinct_query = False |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
691 |
|
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
692 |
def match_condition(self, session, eidfrom, eidto): |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
693 |
return self.exec_query(session, eidfrom, eidto) |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
694 |
|
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
695 |
|
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
696 |
class RQLUniqueConstraint(RepoEnforcedRQLConstraintMixIn, BaseRQLConstraint): |
0 | 697 |
"""the unique rql constraint check that the result of the query isn't |
698 |
greater than one |
|
699 |
""" |
|
3961
d1cbf77db999
fix RQLUniqueConstraint behaviour by using a DISTINCT query and allowing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
700 |
distinct_query = True |
d1cbf77db999
fix RQLUniqueConstraint behaviour by using a DISTINCT query and allowing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
701 |
|
3985
d5bf894fcf02
add some notes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3980
diff
changeset
|
702 |
# XXX turns mainvars into a required argument in __init__, since we've no |
d5bf894fcf02
add some notes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3980
diff
changeset
|
703 |
# way to guess it correctly (eg if using S,O or U the constraint will |
d5bf894fcf02
add some notes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3980
diff
changeset
|
704 |
# always be satisfied since we've to use a DISTINCT query) |
d5bf894fcf02
add some notes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3980
diff
changeset
|
705 |
|
3978
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
706 |
def match_condition(self, session, eidfrom, eidto): |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
707 |
return len(self.exec_query(session, eidfrom, eidto)) <= 1 |
0 | 708 |
|
709 |
||
710 |
class RQLExpression(object): |
|
711 |
def __init__(self, expression, mainvars, eid): |
|
712 |
self.eid = eid # eid of the entity representing this rql expression |
|
713 |
if not isinstance(mainvars, unicode): |
|
714 |
mainvars = unicode(mainvars) |
|
715 |
self.mainvars = mainvars |
|
716 |
self.expression = normalize_expression(expression) |
|
717 |
try: |
|
718 |
self.rqlst = parse(self.full_rql, print_errors=False).children[0] |
|
719 |
except RQLSyntaxError: |
|
720 |
raise RQLSyntaxError(expression) |
|
721 |
for mainvar in mainvars.split(','): |
|
722 |
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
|
723 |
_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
|
724 |
'expression %s', mainvar, self) |
3825 | 725 |
# syntax tree used by read security (inserted in queries when necessary) |
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
|
726 |
self.snippet_rqlst = parse(self.minimal_rql, print_errors=False).children[0] |
1451 | 727 |
|
0 | 728 |
def __str__(self): |
729 |
return self.full_rql |
|
730 |
def __repr__(self): |
|
731 |
return '%s(%s)' % (self.__class__.__name__, self.full_rql) |
|
1451 | 732 |
|
0 | 733 |
def __deepcopy__(self, memo): |
734 |
return self.__class__(self.expression, self.mainvars) |
|
735 |
def __getstate__(self): |
|
736 |
return (self.expression, self.mainvars) |
|
737 |
def __setstate__(self, state): |
|
738 |
self.__init__(*state) |
|
1451 | 739 |
|
0 | 740 |
@cached |
741 |
def transform_has_permission(self): |
|
742 |
found = None |
|
743 |
rqlst = self.rqlst |
|
744 |
for var in rqlst.defined_vars.itervalues(): |
|
745 |
for varref in var.references(): |
|
746 |
rel = varref.relation() |
|
747 |
if rel is None: |
|
748 |
continue |
|
749 |
try: |
|
750 |
prefix, action, suffix = rel.r_type.split('_') |
|
751 |
except ValueError: |
|
752 |
continue |
|
753 |
if prefix != 'has' or suffix != 'permission' or \ |
|
754 |
not action in ('add', 'delete', 'update', 'read'): |
|
755 |
continue |
|
756 |
if found is None: |
|
757 |
found = [] |
|
758 |
rqlst.save_state() |
|
759 |
assert rel.children[0].name == 'U' |
|
760 |
objvar = rel.children[1].children[0].variable |
|
761 |
rqlst.remove_node(rel) |
|
762 |
selected = [v.name for v in rqlst.get_selected_variables()] |
|
763 |
if objvar.name not in selected: |
|
764 |
colindex = len(selected) |
|
765 |
rqlst.add_selected(objvar) |
|
766 |
else: |
|
767 |
colindex = selected.index(objvar.name) |
|
768 |
found.append((action, objvar, colindex)) |
|
769 |
# remove U eid %(u)s if U is not used in any other relation |
|
770 |
uvrefs = rqlst.defined_vars['U'].references() |
|
771 |
if len(uvrefs) == 1: |
|
772 |
rqlst.remove_node(uvrefs[0].relation()) |
|
773 |
if found is not None: |
|
774 |
rql = rqlst.as_string() |
|
775 |
if len(rqlst.selection) == 1 and isinstance(rqlst.where, nodes.Relation): |
|
776 |
# only "Any X WHERE X eid %(x)s" remaining, no need to execute the rql |
|
777 |
keyarg = rqlst.selection[0].name.lower() |
|
778 |
else: |
|
779 |
keyarg = None |
|
780 |
rqlst.recover() |
|
781 |
return rql, found, keyarg |
|
782 |
return rqlst.as_string(), None, None |
|
1451 | 783 |
|
0 | 784 |
def _check(self, session, **kwargs): |
785 |
"""return True if the rql expression is matching the given relation |
|
786 |
between fromeid and toeid |
|
787 |
||
788 |
session may actually be a request as well |
|
789 |
""" |
|
790 |
if self.eid is not None: |
|
791 |
key = (self.eid, tuple(sorted(kwargs.iteritems()))) |
|
792 |
try: |
|
793 |
return session.local_perm_cache[key] |
|
794 |
except KeyError: |
|
795 |
pass |
|
796 |
rql, has_perm_defs, keyarg = self.transform_has_permission() |
|
797 |
if keyarg is None: |
|
798 |
# on the server side, use unsafe_execute, but this is not available |
|
799 |
# on the client side (session is actually a request) |
|
800 |
execute = getattr(session, 'unsafe_execute', session.execute) |
|
801 |
# XXX what if 'u' in kwargs |
|
802 |
cachekey = kwargs.keys() |
|
803 |
kwargs['u'] = session.user.eid |
|
804 |
try: |
|
805 |
rset = execute(rql, kwargs, cachekey, build_descr=True) |
|
806 |
except NotImplementedError: |
|
807 |
self.critical('cant check rql expression, unsupported rql %s', rql) |
|
808 |
if self.eid is not None: |
|
809 |
session.local_perm_cache[key] = False |
|
810 |
return False |
|
811 |
except TypeResolverException, ex: |
|
812 |
# some expression may not be resolvable with current kwargs |
|
813 |
# (type conflict) |
|
814 |
self.warning('%s: %s', rql, str(ex)) |
|
815 |
if self.eid is not None: |
|
816 |
session.local_perm_cache[key] = False |
|
817 |
return False |
|
818 |
else: |
|
819 |
rset = session.eid_rset(kwargs[keyarg]) |
|
820 |
# if no special has_*_permission relation in the rql expression, just |
|
821 |
# check the result set contains something |
|
822 |
if has_perm_defs is None: |
|
823 |
if rset: |
|
824 |
if self.eid is not None: |
|
825 |
session.local_perm_cache[key] = True |
|
826 |
return True |
|
827 |
elif rset: |
|
828 |
# check every special has_*_permission relation is satisfied |
|
829 |
get_eschema = session.vreg.schema.eschema |
|
830 |
try: |
|
831 |
for eaction, var, col in has_perm_defs: |
|
832 |
for i in xrange(len(rset)): |
|
833 |
eschema = get_eschema(rset.description[i][col]) |
|
834 |
eschema.check_perm(session, eaction, rset[i][col]) |
|
835 |
if self.eid is not None: |
|
836 |
session.local_perm_cache[key] = True |
|
837 |
return True |
|
838 |
except Unauthorized: |
|
839 |
pass |
|
840 |
if self.eid is not None: |
|
841 |
session.local_perm_cache[key] = False |
|
842 |
return False |
|
1451 | 843 |
|
0 | 844 |
@property |
845 |
def minimal_rql(self): |
|
846 |
return 'Any %s WHERE %s' % (self.mainvars, self.expression) |
|
847 |
||
848 |
||
849 |
class ERQLExpression(RQLExpression): |
|
850 |
def __init__(self, expression, mainvars=None, eid=None): |
|
851 |
RQLExpression.__init__(self, expression, mainvars or 'X', eid) |
|
852 |
||
853 |
@property |
|
854 |
def full_rql(self): |
|
855 |
rql = self.minimal_rql |
|
856 |
rqlst = getattr(self, 'rqlst', None) # may be not set yet |
|
857 |
if rqlst is not None: |
|
858 |
defined = rqlst.defined_vars |
|
859 |
else: |
|
860 |
defined = set(split_expression(self.expression)) |
|
861 |
if 'X' in defined: |
|
862 |
rql += ', X eid %(x)s' |
|
863 |
if 'U' in defined: |
|
864 |
rql += ', U eid %(u)s' |
|
865 |
return rql |
|
1451 | 866 |
|
0 | 867 |
def check(self, session, eid=None): |
868 |
if 'X' in self.rqlst.defined_vars: |
|
869 |
if eid is None: |
|
870 |
return False |
|
871 |
return self._check(session, x=eid) |
|
872 |
return self._check(session) |
|
1451 | 873 |
|
3961
d1cbf77db999
fix RQLUniqueConstraint behaviour by using a DISTINCT query and allowing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
874 |
|
0 | 875 |
class RRQLExpression(RQLExpression): |
876 |
def __init__(self, expression, mainvars=None, eid=None): |
|
877 |
if mainvars is None: |
|
3961
d1cbf77db999
fix RQLUniqueConstraint behaviour by using a DISTINCT query and allowing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
878 |
mainvars = guess_rrqlexpr_mainvars(expression) |
0 | 879 |
RQLExpression.__init__(self, expression, mainvars, eid) |
3827 | 880 |
# graph of links between variable, used by rql rewriter |
3826
0c0c051863cb
close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3825
diff
changeset
|
881 |
self.vargraph = {} |
0c0c051863cb
close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3825
diff
changeset
|
882 |
for relation in self.rqlst.get_nodes(nodes.Relation): |
0c0c051863cb
close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3825
diff
changeset
|
883 |
try: |
0c0c051863cb
close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3825
diff
changeset
|
884 |
rhsvarname = relation.children[1].children[0].variable.name |
0c0c051863cb
close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3825
diff
changeset
|
885 |
lhsvarname = relation.children[0].name |
0c0c051863cb
close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3825
diff
changeset
|
886 |
except AttributeError: |
0c0c051863cb
close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3825
diff
changeset
|
887 |
pass |
0c0c051863cb
close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3825
diff
changeset
|
888 |
else: |
0c0c051863cb
close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3825
diff
changeset
|
889 |
self.vargraph.setdefault(lhsvarname, []).append(rhsvarname) |
0c0c051863cb
close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3825
diff
changeset
|
890 |
self.vargraph.setdefault(rhsvarname, []).append(lhsvarname) |
0c0c051863cb
close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3825
diff
changeset
|
891 |
#self.vargraph[(lhsvarname, rhsvarname)] = relation.r_type |
0 | 892 |
|
893 |
@property |
|
894 |
def full_rql(self): |
|
895 |
rql = self.minimal_rql |
|
896 |
rqlst = getattr(self, 'rqlst', None) # may be not set yet |
|
897 |
if rqlst is not None: |
|
898 |
defined = rqlst.defined_vars |
|
899 |
else: |
|
900 |
defined = set(split_expression(self.expression)) |
|
901 |
if 'S' in defined: |
|
902 |
rql += ', S eid %(s)s' |
|
903 |
if 'O' in defined: |
|
904 |
rql += ', O eid %(o)s' |
|
905 |
if 'U' in defined: |
|
906 |
rql += ', U eid %(u)s' |
|
907 |
return rql |
|
1451 | 908 |
|
0 | 909 |
def check(self, session, fromeid=None, toeid=None): |
910 |
kwargs = {} |
|
911 |
if 'S' in self.rqlst.defined_vars: |
|
912 |
if fromeid is None: |
|
913 |
return False |
|
914 |
kwargs['s'] = fromeid |
|
915 |
if 'O' in self.rqlst.defined_vars: |
|
916 |
if toeid is None: |
|
917 |
return False |
|
918 |
kwargs['o'] = toeid |
|
919 |
return self._check(session, **kwargs) |
|
1451 | 920 |
|
0 | 921 |
|
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
|
922 |
# 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
|
923 |
|
2460
ce1a7ffc6c90
fix a couple NameErrors
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2459
diff
changeset
|
924 |
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
|
925 |
|
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
|
926 |
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
|
927 |
"""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
|
928 |
(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
|
929 |
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
|
930 |
""" |
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
|
931 |
def __new__(mcs, name, bases, classdict): |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
932 |
abstract = classdict.pop('__abstract__', False) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
933 |
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
|
934 |
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
|
935 |
if not abstract: |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
936 |
make_workflowable(cls) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
937 |
return cls |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
938 |
|
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
|
939 |
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
|
940 |
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
|
941 |
# 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
|
942 |
# cardinality, constraints and other relation definition properties |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
943 |
if 'custom_workflow' not in existing_rels: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
944 |
rdef = ybo.SubjectRelation('Workflow') |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
945 |
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
|
946 |
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
|
947 |
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
|
948 |
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
|
949 |
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
|
950 |
rdef = ybo.ObjectRelation('TrInfo') |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
951 |
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
|
952 |
|
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
|
953 |
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
|
954 |
__metaclass__ = workflowable_definition |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
955 |
__abstract__ = True |
1451 | 956 |
|
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
|
957 |
|
0 | 958 |
# schema loading ############################################################## |
959 |
||
960 |
CONSTRAINTS['RQLConstraint'] = RQLConstraint |
|
961 |
CONSTRAINTS['RQLUniqueConstraint'] = RQLUniqueConstraint |
|
962 |
CONSTRAINTS['RQLVocabularyConstraint'] = RQLVocabularyConstraint |
|
3980
b17cf4a2e09e
this constraint is actually unusable in yams schema for cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3979
diff
changeset
|
963 |
CONSTRAINTS.pop('MultipleStaticVocabularyConstraint', None) # don't want this in cw yams schema |
0 | 964 |
PyFileReader.context.update(CONSTRAINTS) |
965 |
||
966 |
||
967 |
class BootstrapSchemaLoader(SchemaLoader): |
|
968 |
"""cubicweb specific schema loader, loading only schema necessary to read |
|
969 |
the persistent schema |
|
970 |
""" |
|
971 |
schemacls = CubicWebSchema |
|
972 |
||
1034
0356bbfb2f26
fix to pass arguments to base class
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
973 |
def load(self, config, path=(), **kwargs): |
0 | 974 |
"""return a Schema instance from the schema definition read |
975 |
from <directory> |
|
976 |
""" |
|
977 |
return super(BootstrapSchemaLoader, self).load( |
|
1034
0356bbfb2f26
fix to pass arguments to base class
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
978 |
path, config.appid, register_base_types=False, **kwargs) |
1451 | 979 |
|
0 | 980 |
def _load_definition_files(self, cubes=None): |
981 |
# bootstraping, ignore cubes |
|
2735
39c942241b48
no need for lib_directory
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2730
diff
changeset
|
982 |
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
|
983 |
self.info('loading %s', filepath) |
098aa2075903
include_schema_files is useless
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
984 |
self.handle_file(filepath) |
1451 | 985 |
|
0 | 986 |
def unhandled_file(self, filepath): |
987 |
"""called when a file without handler associated has been found""" |
|
988 |
self.warning('ignoring file %r', filepath) |
|
989 |
||
990 |
||
991 |
class CubicWebSchemaLoader(BootstrapSchemaLoader): |
|
992 |
"""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
|
993 |
instance's schema |
0 | 994 |
""" |
995 |
||
1034
0356bbfb2f26
fix to pass arguments to base class
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
996 |
def load(self, config, **kwargs): |
0 | 997 |
"""return a Schema instance from the schema definition read |
998 |
from <directory> |
|
999 |
""" |
|
1000 |
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
|
1001 |
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
|
1002 |
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
|
1003 |
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
|
1004 |
self.extrapath[cubesdir] = 'cubes' |
372
a8a975a88368
check apphome is not None
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
1005 |
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
|
1006 |
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
|
1007 |
else: |
2598
a66fe74bd9fc
[R schema] dont give an iterator, it's a trap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
1008 |
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
|
1009 |
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
|
1010 |
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
|
1011 |
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
|
1012 |
# 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
|
1013 |
cleanup_sys_modules([join(cubicweb.CW_SOFTWARE_ROOT, 'schemas')]) |
0 | 1014 |
|
1015 |
def _load_definition_files(self, cubes): |
|
2741
148d0cea1fb5
[schema] oops
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2736
diff
changeset
|
1016 |
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
|
1017 |
join(cubicweb.CW_SOFTWARE_ROOT, 'schemas', 'base.py'), |
148d0cea1fb5
[schema] oops
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2736
diff
changeset
|
1018 |
join(cubicweb.CW_SOFTWARE_ROOT, 'schemas', 'workflow.py'), |
148d0cea1fb5
[schema] oops
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2736
diff
changeset
|
1019 |
join(cubicweb.CW_SOFTWARE_ROOT, 'schemas', 'Bookmark.py')): |
0 | 1020 |
self.info('loading %s', filepath) |
1021 |
self.handle_file(filepath) |
|
1022 |
for cube in cubes: |
|
1023 |
for filepath in self.get_schema_files(cube): |
|
1024 |
self.info('loading %s', filepath) |
|
1025 |
self.handle_file(filepath) |
|
1026 |
||
1027 |
||
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
|
1028 |
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
|
1029 |
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
|
1030 |
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
|
1031 |
|
0 | 1032 |
# _() is just there to add messages to the catalog, don't care about actual |
1033 |
# translation |
|
1034 |
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
|
1035 |
NEED_PERM_FORMATS = [_('text/cubicweb-page-template')] |
0 | 1036 |
|
2531
531ea4e7013e
[cleanup] nicer imports
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2526
diff
changeset
|
1037 |
@monkeypatch(FormatConstraint) |
3347
428f95118556
fix vocab param to avoid deprecation warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3287
diff
changeset
|
1038 |
def vocabulary(self, entity=None, form=None): |
428f95118556
fix vocab param to avoid deprecation warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3287
diff
changeset
|
1039 |
req = None |
428f95118556
fix vocab param to avoid deprecation warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3287
diff
changeset
|
1040 |
if form is None and entity is not None: |
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
|
1041 |
req = entity.req |
3347
428f95118556
fix vocab param to avoid deprecation warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3287
diff
changeset
|
1042 |
elif form is not None: |
428f95118556
fix vocab param to avoid deprecation warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3287
diff
changeset
|
1043 |
req = form.req |
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
|
1044 |
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
|
1045 |
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
|
1046 |
return self.regular_formats |
0 | 1047 |
|
1048 |
# XXX monkey patch PyFileReader.import_erschema until bw_normalize_etype is |
|
1049 |
# necessary |
|
1050 |
orig_import_erschema = PyFileReader.import_erschema |
|
1051 |
def bw_import_erschema(self, ertype, schemamod=None, instantiate=True): |
|
1052 |
return orig_import_erschema(self, bw_normalize_etype(ertype), schemamod, instantiate) |
|
1053 |
PyFileReader.import_erschema = bw_import_erschema |
|
1451 | 1054 |
|
0 | 1055 |
# XXX itou for some Statement methods |
1056 |
from rql import stmts |
|
1057 |
orig_get_etype = stmts.ScopeNode.get_etype |
|
1058 |
def bw_get_etype(self, name): |
|
1059 |
return orig_get_etype(self, bw_normalize_etype(name)) |
|
1060 |
stmts.ScopeNode.get_etype = bw_get_etype |
|
1061 |
||
1062 |
orig_add_main_variable_delete = stmts.Delete.add_main_variable |
|
1063 |
def bw_add_main_variable_delete(self, etype, vref): |
|
1064 |
return orig_add_main_variable_delete(self, bw_normalize_etype(etype), vref) |
|
1065 |
stmts.Delete.add_main_variable = bw_add_main_variable_delete |
|
1066 |
||
1067 |
orig_add_main_variable_insert = stmts.Insert.add_main_variable |
|
1068 |
def bw_add_main_variable_insert(self, etype, vref): |
|
1069 |
return orig_add_main_variable_insert(self, bw_normalize_etype(etype), vref) |
|
1070 |
stmts.Insert.add_main_variable = bw_add_main_variable_insert |
|
1071 |
||
1072 |
orig_set_statement_type = stmts.Select.set_statement_type |
|
1073 |
def bw_set_statement_type(self, etype): |
|
1074 |
return orig_set_statement_type(self, bw_normalize_etype(etype)) |
|
1075 |
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
|
1076 |
|
c5048502409f
restore backward compat, still imported a lot
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2735
diff
changeset
|
1077 |
# XXX deprecated |
3978
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
1078 |
|
2736
c5048502409f
restore backward compat, still imported a lot
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2735
diff
changeset
|
1079 |
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
|
1080 |
from yams.buildobjs import RichString |
3978
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
1081 |
|
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
1082 |
PyFileReader.context['ERQLExpression'] = yobsolete(ERQLExpression) |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
1083 |
PyFileReader.context['RRQLExpression'] = yobsolete(RRQLExpression) |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
1084 |
PyFileReader.context['WorkflowableEntityType'] = WorkflowableEntityType |
2742
9439c61eb84c
[bw compat] restore format_constraint into schema file context
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2741
diff
changeset
|
1085 |
PyFileReader.context['format_constraint'] = format_constraint |