author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Mon, 08 Feb 2010 12:02:32 +0100 | |
changeset 4492 | 150f1d57ecfd |
parent 4475 | 37c413a07216 |
child 4570 | ede247bbbf62 |
permissions | -rw-r--r-- |
0 | 1 |
"""functions for schema / permissions (de)serialization using RQL |
2 |
||
3 |
:organization: Logilab |
|
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3897
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:
1913
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 7 |
""" |
8 |
__docformat__ = "restructuredtext en" |
|
9 |
||
3854
8633cd05b6b5
no progress bar in apycot environment
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
10 |
import os |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
11 |
import sys |
3723
a961c7cb2644
be quiet w/ apycot
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
12 |
import os |
0 | 13 |
from itertools import chain |
14 |
||
15 |
from logilab.common.shellutils import ProgressBar |
|
16 |
||
17 |
from yams import schema as schemamod, buildobjs as ybo |
|
18 |
||
2596
d02eed70937f
[R repo, schema] use VIRTUAL_RTYPES const
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2458
diff
changeset
|
19 |
from cubicweb.schema import CONSTRAINTS, ETYPE_NAME_MAP, VIRTUAL_RTYPES |
1869
642a1a120a92
should use sql prefix here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1630
diff
changeset
|
20 |
from cubicweb.server import sqlutils |
0 | 21 |
|
22 |
def group_mapping(cursor, interactive=True): |
|
23 |
"""create a group mapping from an rql cursor |
|
24 |
||
25 |
A group mapping has standard group names as key (managers, owners at least) |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
26 |
and the actual CWGroup entity's eid as associated value. |
0 | 27 |
In interactive mode (the default), missing groups'eid will be prompted |
28 |
from the user. |
|
29 |
""" |
|
30 |
res = {} |
|
4312
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
31 |
for eid, name in cursor.execute('Any G, N WHERE G is CWGroup, G name N', |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
32 |
build_descr=False): |
0 | 33 |
res[name] = eid |
34 |
if not interactive: |
|
35 |
return res |
|
36 |
missing = [g for g in ('owners', 'managers', 'users', 'guests') if not g in res] |
|
37 |
if missing: |
|
38 |
print 'some native groups are missing but the following groups have been found:' |
|
39 |
print '\n'.join('* %s (%s)' % (n, eid) for n, eid in res.items()) |
|
1630
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
40 |
print |
0 | 41 |
print 'enter the eid of a to group to map to each missing native group' |
42 |
print 'or just type enter to skip permissions granted to a group' |
|
43 |
for group in missing: |
|
44 |
while True: |
|
45 |
value = raw_input('eid for group %s: ' % group).strip() |
|
46 |
if not value: |
|
47 |
continue |
|
48 |
try: |
|
49 |
res[group] = int(value) |
|
50 |
except ValueError: |
|
51 |
print 'eid should be an integer' |
|
52 |
continue |
|
53 |
return res |
|
54 |
||
55 |
# schema / perms deserialization ############################################## |
|
2044
d5589c5bc4e6
alter table in schema serial when a renamed type is detected
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
56 |
OLD_SCHEMA_TYPES = frozenset(('EFRDef', 'ENFRDef', 'ERType', 'EEType', |
d5589c5bc4e6
alter table in schema serial when a renamed type is detected
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
57 |
'EConstraintType', 'EConstraint', 'EGroup', |
d5589c5bc4e6
alter table in schema serial when a renamed type is detected
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
58 |
'EUser', 'ECache', 'EPermission', 'EProperty')) |
0 | 59 |
|
60 |
def deserialize_schema(schema, session): |
|
61 |
"""return a schema according to information stored in an rql database |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
62 |
as CWRType and CWEType entities |
0 | 63 |
""" |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
64 |
repo = session.repo |
2044
d5589c5bc4e6
alter table in schema serial when a renamed type is detected
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
65 |
dbhelper = repo.system_source.dbhelper |
4467
0e73d299730a
fix long-waiting symetric typo: should be spelled symmetric. Add auto database migration on schema deserialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4312
diff
changeset
|
66 |
# 3.6 migration |
4475
37c413a07216
kill most pre 3.2 bw compat code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4467
diff
changeset
|
67 |
sqlcu = session.pool['system'] |
4467
0e73d299730a
fix long-waiting symetric typo: should be spelled symmetric. Add auto database migration on schema deserialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4312
diff
changeset
|
68 |
sqlcu.execute("SELECT * FROM cw_CWRType WHERE cw_name='symetric'") |
0e73d299730a
fix long-waiting symetric typo: should be spelled symmetric. Add auto database migration on schema deserialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4312
diff
changeset
|
69 |
if sqlcu.fetchall(): |
0e73d299730a
fix long-waiting symetric typo: should be spelled symmetric. Add auto database migration on schema deserialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4312
diff
changeset
|
70 |
sql = dbhelper.sql_rename_col('cw_CWRType', 'cw_symetric', 'cw_symmetric', |
0e73d299730a
fix long-waiting symetric typo: should be spelled symmetric. Add auto database migration on schema deserialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4312
diff
changeset
|
71 |
dbhelper.TYPE_MAPPING['Boolean'], True) |
0e73d299730a
fix long-waiting symetric typo: should be spelled symmetric. Add auto database migration on schema deserialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4312
diff
changeset
|
72 |
sqlcu.execute(sql) |
0e73d299730a
fix long-waiting symetric typo: should be spelled symmetric. Add auto database migration on schema deserialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4312
diff
changeset
|
73 |
sqlcu.execute("UPDATE cw_CWRType SET cw_name='symmetric' WHERE cw_name='symetric'") |
4312
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
74 |
sidx = {} |
0 | 75 |
permsdict = deserialize_ertype_permissions(session) |
76 |
schema.reading_from_database = True |
|
4312
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
77 |
for eid, etype, desc in session.execute( |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
78 |
'Any X, N, D WHERE X is CWEType, X name N, X description D', |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
79 |
build_descr=False): |
0 | 80 |
# base types are already in the schema, skip them |
81 |
if etype in schemamod.BASE_TYPES: |
|
82 |
# just set the eid |
|
83 |
eschema = schema.eschema(etype) |
|
84 |
eschema.eid = eid |
|
4312
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
85 |
sidx[eid] = eschema |
0 | 86 |
continue |
2044
d5589c5bc4e6
alter table in schema serial when a renamed type is detected
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
87 |
if etype in ETYPE_NAME_MAP: |
d5589c5bc4e6
alter table in schema serial when a renamed type is detected
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
88 |
netype = ETYPE_NAME_MAP[etype] |
0 | 89 |
# can't use write rql queries at this point, use raw sql |
1869
642a1a120a92
should use sql prefix here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1630
diff
changeset
|
90 |
session.system_sql('UPDATE %(p)sCWEType SET %(p)sname=%%(n)s WHERE %(p)seid=%%(x)s' |
642a1a120a92
should use sql prefix here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1630
diff
changeset
|
91 |
% {'p': sqlutils.SQL_PREFIX}, |
2044
d5589c5bc4e6
alter table in schema serial when a renamed type is detected
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
92 |
{'x': eid, 'n': netype}) |
0 | 93 |
session.system_sql('UPDATE entities SET type=%(n)s WHERE type=%(x)s', |
2044
d5589c5bc4e6
alter table in schema serial when a renamed type is detected
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
94 |
{'x': etype, 'n': netype}) |
d5589c5bc4e6
alter table in schema serial when a renamed type is detected
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
95 |
# XXX should be donne as well on sqlite based sources |
d5589c5bc4e6
alter table in schema serial when a renamed type is detected
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
96 |
if not etype in OLD_SCHEMA_TYPES and \ |
2172
cf8f9180e63e
delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2044
diff
changeset
|
97 |
(getattr(dbhelper, 'case_sensitive', False) |
2044
d5589c5bc4e6
alter table in schema serial when a renamed type is detected
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
98 |
or etype.lower() != netype.lower()): |
d5589c5bc4e6
alter table in schema serial when a renamed type is detected
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
99 |
session.system_sql('ALTER TABLE %s%s RENAME TO %s%s' % ( |
d5589c5bc4e6
alter table in schema serial when a renamed type is detected
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
100 |
sqlutils.SQL_PREFIX, etype, sqlutils.SQL_PREFIX, netype)) |
0 | 101 |
session.commit(False) |
102 |
try: |
|
103 |
session.system_sql('UPDATE deleted_entities SET type=%(n)s WHERE type=%(x)s', |
|
2044
d5589c5bc4e6
alter table in schema serial when a renamed type is detected
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
104 |
{'x': etype, 'n': netype}) |
0 | 105 |
except: |
106 |
pass |
|
107 |
tocleanup = [eid] |
|
1630
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
108 |
tocleanup += (eid for eid, (eidetype, uri, extid) in repo._type_source_cache.items() |
0 | 109 |
if etype == eidetype) |
1630
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
110 |
repo.clear_caches(tocleanup) |
0 | 111 |
session.commit(False) |
2044
d5589c5bc4e6
alter table in schema serial when a renamed type is detected
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
112 |
etype = netype |
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:
2044
diff
changeset
|
113 |
etype = ybo.EntityType(name=etype, description=desc, eid=eid) |
0 | 114 |
eschema = schema.add_entity_type(etype) |
4312
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
115 |
sidx[eid] = eschema |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
116 |
set_perms(eschema, permsdict) |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
117 |
for etype, stype in session.execute( |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
118 |
'Any XN, ETN WHERE X is CWEType, X name XN, X specializes ET, ET name ETN', |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
119 |
build_descr=False): |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
120 |
schema.eschema(etype)._specialized_type = stype |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
121 |
schema.eschema(stype)._specialized_by.append(etype) |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
122 |
for eid, rtype, desc, sym, il, ftc in session.execute( |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
123 |
'Any X,N,D,S,I,FTC WHERE X is CWRType, X name N, X description D, ' |
4467
0e73d299730a
fix long-waiting symetric typo: should be spelled symmetric. Add auto database migration on schema deserialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4312
diff
changeset
|
124 |
'X symmetric S, X inlined I, X fulltext_container FTC', build_descr=False): |
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:
2044
diff
changeset
|
125 |
rtype = ybo.RelationType(name=rtype, description=desc, |
4467
0e73d299730a
fix long-waiting symetric typo: should be spelled symmetric. Add auto database migration on schema deserialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4312
diff
changeset
|
126 |
symmetric=bool(sym), inlined=bool(il), |
4312
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
127 |
fulltext_container=ftc, eid=eid) |
0 | 128 |
rschema = schema.add_relation_type(rtype) |
4312
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
129 |
sidx[eid] = rschema |
0 | 130 |
cstrsdict = deserialize_rdef_constraints(session) |
131 |
for values in session.execute( |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
132 |
'Any X,SE,RT,OE,CARD,ORD,DESC,IDX,FTIDX,I18N,DFLT WHERE X is CWAttribute,' |
0 | 133 |
'X relation_type RT, X cardinality CARD, X ordernum ORD, X indexed IDX,' |
134 |
'X description DESC, X internationalizable I18N, X defaultval DFLT,' |
|
135 |
'X fulltextindexed FTIDX, X from_entity SE, X to_entity OE', |
|
136 |
build_descr=False): |
|
137 |
rdefeid, seid, reid, teid, card, ord, desc, idx, ftidx, i18n, default = values |
|
4312
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
138 |
rdef = ybo.RelationDefinition(sidx[seid].type, sidx[reid].type, sidx[teid].type, |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
139 |
cardinality=card, |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
140 |
constraints=cstrsdict.get(rdefeid, ()), |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
141 |
order=ord, description=desc, |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
142 |
indexed=idx, fulltextindexed=ftidx, |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
143 |
internationalizable=i18n, |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
144 |
default=default, eid=rdefeid) |
4003
b9436fe77c9e
fix bad merge
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3998
diff
changeset
|
145 |
rdefs = schema.add_relation_def(rdef) |
4467
0e73d299730a
fix long-waiting symetric typo: should be spelled symmetric. Add auto database migration on schema deserialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4312
diff
changeset
|
146 |
# rdefs can be None on duplicated relation definitions (e.g. symmetrics) |
4312
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
147 |
if rdefs is not None: |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
148 |
set_perms(rdefs, permsdict) |
0 | 149 |
for values in session.execute( |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
150 |
'Any X,SE,RT,OE,CARD,ORD,DESC,C WHERE X is CWRelation, X relation_type RT,' |
0 | 151 |
'X cardinality CARD, X ordernum ORD, X description DESC, ' |
152 |
'X from_entity SE, X to_entity OE, X composite C', build_descr=False): |
|
153 |
rdefeid, seid, reid, teid, card, ord, desc, c = values |
|
4312
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
154 |
rdef = ybo.RelationDefinition(sidx[seid].type, sidx[reid].type, sidx[teid].type, |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
155 |
constraints=cstrsdict.get(rdefeid, ()), |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
156 |
cardinality=card, order=ord, description=desc, |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
157 |
composite=c, eid=rdefeid) |
4003
b9436fe77c9e
fix bad merge
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3998
diff
changeset
|
158 |
rdefs = schema.add_relation_def(rdef) |
4467
0e73d299730a
fix long-waiting symetric typo: should be spelled symmetric. Add auto database migration on schema deserialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4312
diff
changeset
|
159 |
# rdefs can be None on duplicated relation definitions (e.g. symmetrics) |
4312
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
160 |
if rdefs is not None: |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
161 |
set_perms(rdefs, permsdict) |
0 | 162 |
schema.infer_specialization_rules() |
163 |
session.commit() |
|
164 |
schema.reading_from_database = False |
|
165 |
||
166 |
||
167 |
def deserialize_ertype_permissions(session): |
|
168 |
"""return sect action:groups associations for the given |
|
169 |
entity or relation schema with its eid, according to schema's |
|
170 |
permissions stored in the database as [read|add|delete|update]_permission |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
171 |
relations between CWEType/CWRType and CWGroup entities |
0 | 172 |
""" |
173 |
res = {} |
|
174 |
for action in ('read', 'add', 'update', 'delete'): |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
175 |
rql = 'Any E,N WHERE G is CWGroup, G name N, E %s_permission G' % action |
0 | 176 |
for eid, gname in session.execute(rql, build_descr=False): |
177 |
res.setdefault(eid, {}).setdefault(action, []).append(gname) |
|
178 |
rql = ('Any E,X,EXPR,V WHERE X is RQLExpression, X expression EXPR, ' |
|
179 |
'E %s_permission X, X mainvars V' % action) |
|
180 |
for eid, expreid, expr, mainvars in session.execute(rql, build_descr=False): |
|
181 |
# we don't know yet if it's a rql expr for an entity or a relation, |
|
182 |
# so append a tuple to differentiate from groups and so we'll be |
|
183 |
# able to instantiate it later |
|
184 |
res.setdefault(eid, {}).setdefault(action, []).append( (expr, mainvars, expreid) ) |
|
185 |
return res |
|
186 |
||
187 |
def set_perms(erschema, permsdict): |
|
188 |
"""set permissions on the given erschema according to the permission |
|
189 |
definition dictionary as built by deserialize_ertype_permissions for a |
|
190 |
given erschema's eid |
|
191 |
""" |
|
4312
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
192 |
try: |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
193 |
thispermsdict = permsdict[erschema.eid] |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
194 |
except KeyError: |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
195 |
return |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
196 |
permissions = erschema.permissions |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
197 |
for action, somethings in thispermsdict.iteritems(): |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
198 |
permissions[action] = tuple( |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
199 |
isinstance(p, tuple) and erschema.rql_expression(*p) or p |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
200 |
for p in somethings) |
0 | 201 |
|
202 |
||
203 |
def deserialize_rdef_constraints(session): |
|
204 |
"""return the list of relation definition's constraints as instances""" |
|
205 |
res = {} |
|
206 |
for rdefeid, ceid, ct, val in session.execute( |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
207 |
'Any E, X,TN,V WHERE E constrained_by X, X is CWConstraint, ' |
0 | 208 |
'X cstrtype T, T name TN, X value V', build_descr=False): |
209 |
cstr = CONSTRAINTS[ct].deserialize(val) |
|
210 |
cstr.eid = ceid |
|
211 |
res.setdefault(rdefeid, []).append(cstr) |
|
212 |
return res |
|
1630
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
213 |
|
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
214 |
|
0 | 215 |
# schema / perms serialization ################################################ |
216 |
||
217 |
def serialize_schema(cursor, schema, verbose=False): |
|
218 |
"""synchronize schema and permissions in the database according to |
|
219 |
current schema |
|
220 |
""" |
|
3737 | 221 |
quiet = os.environ.get('APYCOT_ROOT') |
3723
a961c7cb2644
be quiet w/ apycot
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
222 |
if not quiet: |
a961c7cb2644
be quiet w/ apycot
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
223 |
_title = '-> storing the schema in the database ' |
a961c7cb2644
be quiet w/ apycot
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
224 |
print _title, |
4312
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
225 |
execute = cursor.execute |
0 | 226 |
eschemas = schema.entities() |
227 |
aller = eschemas + schema.relations() |
|
3723
a961c7cb2644
be quiet w/ apycot
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
228 |
if not verbose and not quiet: |
0 | 229 |
pb_size = len(aller) + len(CONSTRAINTS) + len([x for x in eschemas if x.specializes()]) |
2396
8bfb99d7bbcc
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2395
diff
changeset
|
230 |
pb = ProgressBar(pb_size, title=_title) |
3857
6676a839dc97
ensure pb is not None
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3854
diff
changeset
|
231 |
else: |
6676a839dc97
ensure pb is not None
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3854
diff
changeset
|
232 |
pb = None |
2597
d9c5a7e0563c
[C schema seria] cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
233 |
rql = 'INSERT CWConstraintType X: X name %(ct)s' |
0 | 234 |
for cstrtype in CONSTRAINTS: |
235 |
if verbose: |
|
236 |
print rql |
|
4312
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
237 |
execute(rql, {'ct': unicode(cstrtype)}, build_descr=False) |
3857
6676a839dc97
ensure pb is not None
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3854
diff
changeset
|
238 |
if pb is not None: |
0 | 239 |
pb.update() |
240 |
groupmap = group_mapping(cursor, interactive=False) |
|
241 |
for ertype in aller: |
|
242 |
# skip eid and has_text relations |
|
2596
d02eed70937f
[R repo, schema] use VIRTUAL_RTYPES const
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2458
diff
changeset
|
243 |
if ertype in VIRTUAL_RTYPES: |
3857
6676a839dc97
ensure pb is not None
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3854
diff
changeset
|
244 |
if pb is not None: |
6676a839dc97
ensure pb is not None
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3854
diff
changeset
|
245 |
pb.update() |
0 | 246 |
continue |
4011
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4010
diff
changeset
|
247 |
for rql, kwargs in erschema2rql(schema[ertype], groupmap): |
0 | 248 |
if verbose: |
249 |
print rql % kwargs |
|
4312
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
250 |
execute(rql, kwargs, build_descr=False) |
3857
6676a839dc97
ensure pb is not None
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3854
diff
changeset
|
251 |
if pb is not None: |
0 | 252 |
pb.update() |
253 |
for rql, kwargs in specialize2rql(schema): |
|
254 |
if verbose: |
|
255 |
print rql % kwargs |
|
4312
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
256 |
execute(rql, kwargs, build_descr=False) |
3857
6676a839dc97
ensure pb is not None
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3854
diff
changeset
|
257 |
if pb is not None: |
0 | 258 |
pb.update() |
3723
a961c7cb2644
be quiet w/ apycot
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
259 |
if not quiet: |
a961c7cb2644
be quiet w/ apycot
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
260 |
print |
0 | 261 |
|
262 |
||
263 |
def _ervalues(erschema): |
|
264 |
try: |
|
265 |
type_ = unicode(erschema.type) |
|
266 |
except UnicodeDecodeError, e: |
|
267 |
raise Exception("can't decode %s [was %s]" % (erschema.type, e)) |
|
268 |
try: |
|
269 |
desc = unicode(erschema.description) or u'' |
|
270 |
except UnicodeDecodeError, e: |
|
271 |
raise Exception("can't decode %s [was %s]" % (erschema.description, e)) |
|
272 |
return { |
|
273 |
'name': type_, |
|
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2955
diff
changeset
|
274 |
'final': erschema.final, |
0 | 275 |
'description': desc, |
276 |
} |
|
277 |
||
278 |
def eschema_relations_values(eschema): |
|
279 |
values = _ervalues(eschema) |
|
280 |
relations = ['X %s %%(%s)s' % (attr, attr) for attr in sorted(values)] |
|
281 |
return relations, values |
|
282 |
||
283 |
# XXX 2.47 migration |
|
284 |
HAS_FULLTEXT_CONTAINER = True |
|
285 |
||
286 |
def rschema_relations_values(rschema): |
|
287 |
values = _ervalues(rschema) |
|
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2955
diff
changeset
|
288 |
values['final'] = rschema.final |
4467
0e73d299730a
fix long-waiting symetric typo: should be spelled symmetric. Add auto database migration on schema deserialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4312
diff
changeset
|
289 |
values['symmetric'] = rschema.symmetric |
0 | 290 |
values['inlined'] = rschema.inlined |
291 |
if HAS_FULLTEXT_CONTAINER: |
|
292 |
if isinstance(rschema.fulltext_container, str): |
|
293 |
values['fulltext_container'] = unicode(rschema.fulltext_container) |
|
294 |
else: |
|
295 |
values['fulltext_container'] = rschema.fulltext_container |
|
296 |
relations = ['X %s %%(%s)s' % (attr, attr) for attr in sorted(values)] |
|
297 |
return relations, values |
|
298 |
||
299 |
def _rdef_values(rschema, objtype, props): |
|
300 |
amap = {'order': 'ordernum'} |
|
301 |
values = {} |
|
4024
6a14cff373c3
more api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4021
diff
changeset
|
302 |
for prop, default in schemamod.RelationDefinitionSchema.rproperty_defs(objtype).iteritems(): |
4003
b9436fe77c9e
fix bad merge
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3998
diff
changeset
|
303 |
if prop in ('eid', 'constraints', 'uid', 'infered', 'permissions'): |
0 | 304 |
continue |
305 |
value = props.get(prop, default) |
|
306 |
if prop in ('indexed', 'fulltextindexed', 'internationalizable'): |
|
307 |
value = bool(value) |
|
308 |
elif prop == 'ordernum': |
|
309 |
value = int(value) |
|
310 |
elif isinstance(value, str): |
|
311 |
value = unicode(value) |
|
312 |
values[amap.get(prop, prop)] = value |
|
313 |
return values |
|
1630
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
314 |
|
0 | 315 |
def nfrdef_relations_values(rschema, objtype, props): |
316 |
values = _rdef_values(rschema, objtype, props) |
|
317 |
relations = ['X %s %%(%s)s' % (attr, attr) for attr in sorted(values)] |
|
318 |
return relations, values |
|
1630
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
319 |
|
0 | 320 |
def frdef_relations_values(rschema, objtype, props): |
321 |
values = _rdef_values(rschema, objtype, props) |
|
322 |
default = values['default'] |
|
323 |
del values['default'] |
|
324 |
if default is not None: |
|
325 |
if default is False: |
|
326 |
default = u'' |
|
327 |
elif not isinstance(default, unicode): |
|
328 |
default = unicode(default) |
|
329 |
values['defaultval'] = default |
|
330 |
relations = ['X %s %%(%s)s' % (attr, attr) for attr in sorted(values)] |
|
331 |
return relations, values |
|
332 |
||
1630
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
333 |
|
4033
732eccbed54a
missing groupmap propagation to rschema2rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4024
diff
changeset
|
334 |
def __rdef2rql(genmap, rschema, subjtype=None, objtype=None, props=None, |
732eccbed54a
missing groupmap propagation to rschema2rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4024
diff
changeset
|
335 |
groupmap=None): |
0 | 336 |
if subjtype is None: |
337 |
assert objtype is None |
|
338 |
assert props is None |
|
4189
bd552b341334
make groupmap argument optional / sort for testing purpose
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4033
diff
changeset
|
339 |
targets = sorted(rschema.rdefs) |
0 | 340 |
else: |
341 |
assert not objtype is None |
|
342 |
targets = [(subjtype, objtype)] |
|
4011
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4010
diff
changeset
|
343 |
# relation schema |
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4010
diff
changeset
|
344 |
if rschema.final: |
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4010
diff
changeset
|
345 |
etype = 'CWAttribute' |
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4010
diff
changeset
|
346 |
else: |
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4010
diff
changeset
|
347 |
etype = 'CWRelation' |
0 | 348 |
for subjtype, objtype in targets: |
349 |
if props is None: |
|
4011
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4010
diff
changeset
|
350 |
_props = rschema.rdef(subjtype, objtype) |
0 | 351 |
else: |
352 |
_props = props |
|
353 |
# don't serialize infered relations |
|
354 |
if _props.get('infered'): |
|
355 |
continue |
|
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2955
diff
changeset
|
356 |
gen = genmap[rschema.final] |
0 | 357 |
for rql, values in gen(rschema, subjtype, objtype, _props): |
358 |
yield rql, values |
|
4011
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4010
diff
changeset
|
359 |
# no groupmap means "no security insertion" |
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4010
diff
changeset
|
360 |
if groupmap: |
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4010
diff
changeset
|
361 |
for rql, args in _erperms2rql(_props, groupmap): |
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4010
diff
changeset
|
362 |
args['st'] = str(subjtype) |
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4010
diff
changeset
|
363 |
args['rt'] = str(rschema) |
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4010
diff
changeset
|
364 |
args['ot'] = str(objtype) |
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4010
diff
changeset
|
365 |
yield rql + 'X is %s, X from_entity ST, X to_entity OT, '\ |
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4010
diff
changeset
|
366 |
'X relation_type RT, RT name %%(rt)s, ST name %%(st)s, '\ |
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4010
diff
changeset
|
367 |
'OT name %%(ot)s' % etype, args |
0 | 368 |
|
369 |
||
370 |
def schema2rql(schema, skip=None, allow=None): |
|
371 |
"""return a list of rql insert statements to enter the schema in the |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
372 |
database as CWRType and CWEType entities |
0 | 373 |
""" |
374 |
assert not (skip is not None and allow is not None), \ |
|
375 |
'can\'t use both skip and allow' |
|
376 |
all = schema.entities() + schema.relations() |
|
377 |
if skip is not None: |
|
378 |
return chain(*[erschema2rql(schema[t]) for t in all if not t in skip]) |
|
379 |
elif allow is not None: |
|
380 |
return chain(*[erschema2rql(schema[t]) for t in all if t in allow]) |
|
381 |
return chain(*[erschema2rql(schema[t]) for t in all]) |
|
1630
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
382 |
|
4011
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4010
diff
changeset
|
383 |
def erschema2rql(erschema, groupmap): |
0 | 384 |
if isinstance(erschema, schemamod.EntitySchema): |
4033
732eccbed54a
missing groupmap propagation to rschema2rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4024
diff
changeset
|
385 |
return eschema2rql(erschema, groupmap=groupmap) |
732eccbed54a
missing groupmap propagation to rschema2rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4024
diff
changeset
|
386 |
return rschema2rql(erschema, groupmap=groupmap) |
0 | 387 |
|
4189
bd552b341334
make groupmap argument optional / sort for testing purpose
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4033
diff
changeset
|
388 |
def eschema2rql(eschema, groupmap=None): |
0 | 389 |
"""return a list of rql insert statements to enter an entity schema |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
390 |
in the database as an CWEType entity |
0 | 391 |
""" |
392 |
relations, values = eschema_relations_values(eschema) |
|
393 |
# NOTE: 'specializes' relation can't be inserted here since there's no |
|
394 |
# way to make sure the parent type is inserted before the child type |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
395 |
yield 'INSERT CWEType X: %s' % ','.join(relations) , values |
4189
bd552b341334
make groupmap argument optional / sort for testing purpose
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4033
diff
changeset
|
396 |
# entity permissions |
bd552b341334
make groupmap argument optional / sort for testing purpose
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4033
diff
changeset
|
397 |
if groupmap is not None: |
bd552b341334
make groupmap argument optional / sort for testing purpose
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4033
diff
changeset
|
398 |
for rql, args in _erperms2rql(eschema, groupmap): |
bd552b341334
make groupmap argument optional / sort for testing purpose
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4033
diff
changeset
|
399 |
args['name'] = str(eschema) |
bd552b341334
make groupmap argument optional / sort for testing purpose
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4033
diff
changeset
|
400 |
yield rql + 'X is CWEType, X name %(name)s', args |
0 | 401 |
|
402 |
def specialize2rql(schema): |
|
403 |
for eschema in schema.entities(): |
|
404 |
for rql, kwargs in eschemaspecialize2rql(eschema): |
|
405 |
yield rql, kwargs |
|
406 |
||
407 |
def eschemaspecialize2rql(eschema): |
|
408 |
specialized_type = eschema.specializes() |
|
409 |
if specialized_type: |
|
410 |
values = {'x': eschema.type, 'et': specialized_type.type} |
|
411 |
yield 'SET X specializes ET WHERE X name %(x)s, ET name %(et)s', values |
|
412 |
||
4011
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4010
diff
changeset
|
413 |
def rschema2rql(rschema, addrdef=True, groupmap=None): |
0 | 414 |
"""return a list of rql insert statements to enter a relation schema |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
415 |
in the database as an CWRType entity |
0 | 416 |
""" |
417 |
if rschema.type == 'has_text': |
|
418 |
return |
|
419 |
relations, values = rschema_relations_values(rschema) |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
420 |
yield 'INSERT CWRType X: %s' % ','.join(relations), values |
0 | 421 |
if addrdef: |
4011
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4010
diff
changeset
|
422 |
for rql, values in rdef2rql(rschema, groupmap=groupmap): |
0 | 423 |
yield rql, values |
1630
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
424 |
|
4011
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4010
diff
changeset
|
425 |
def rdef2rql(rschema, subjtype=None, objtype=None, props=None, groupmap=None): |
0 | 426 |
genmap = {True: frdef2rql, False: nfrdef2rql} |
4011
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4010
diff
changeset
|
427 |
return __rdef2rql(genmap, rschema, subjtype, objtype, props, groupmap) |
0 | 428 |
|
429 |
||
430 |
_LOCATE_RDEF_RQL0 = 'X relation_type ER,X from_entity SE,X to_entity OE' |
|
431 |
_LOCATE_RDEF_RQL1 = 'SE name %(se)s,ER name %(rt)s,OE name %(oe)s' |
|
432 |
||
433 |
def frdef2rql(rschema, subjtype, objtype, props): |
|
434 |
relations, values = frdef_relations_values(rschema, objtype, props) |
|
435 |
relations.append(_LOCATE_RDEF_RQL0) |
|
436 |
values.update({'se': str(subjtype), 'rt': str(rschema), 'oe': str(objtype)}) |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
437 |
yield 'INSERT CWAttribute X: %s WHERE %s' % (','.join(relations), _LOCATE_RDEF_RQL1), values |
0 | 438 |
for rql, values in rdefrelations2rql(rschema, subjtype, objtype, props): |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
439 |
yield rql + ', EDEF is CWAttribute', values |
1630
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
440 |
|
0 | 441 |
def nfrdef2rql(rschema, subjtype, objtype, props): |
442 |
relations, values = nfrdef_relations_values(rschema, objtype, props) |
|
443 |
relations.append(_LOCATE_RDEF_RQL0) |
|
444 |
values.update({'se': str(subjtype), 'rt': str(rschema), 'oe': str(objtype)}) |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
445 |
yield 'INSERT CWRelation X: %s WHERE %s' % (','.join(relations), _LOCATE_RDEF_RQL1), values |
0 | 446 |
for rql, values in rdefrelations2rql(rschema, subjtype, objtype, props): |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
447 |
yield rql + ', EDEF is CWRelation', values |
1630
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
448 |
|
0 | 449 |
def rdefrelations2rql(rschema, subjtype, objtype, props): |
450 |
iterators = [] |
|
4021
280c910c8710
move i18n / migration modules from cw.common to cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4014
diff
changeset
|
451 |
for constraint in props.constraints: |
0 | 452 |
iterators.append(constraint2rql(rschema, subjtype, objtype, constraint)) |
453 |
return chain(*iterators) |
|
454 |
||
455 |
def constraint2rql(rschema, subjtype, objtype, constraint): |
|
456 |
values = {'ctname': unicode(constraint.type()), |
|
457 |
'value': unicode(constraint.serialize()), |
|
458 |
'rt': str(rschema), 'se': str(subjtype), 'oe': str(objtype)} |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
459 |
yield 'INSERT CWConstraint X: X value %(value)s, X cstrtype CT, EDEF constrained_by X WHERE \ |
0 | 460 |
CT name %(ctname)s, EDEF relation_type ER, EDEF from_entity SE, EDEF to_entity OE, \ |
461 |
ER name %(rt)s, SE name %(se)s, OE name %(oe)s', values |
|
462 |
||
463 |
||
4011
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4010
diff
changeset
|
464 |
def _erperms2rql(erschema, groupmap): |
0 | 465 |
"""return rql insert statements to enter the entity or relation |
466 |
schema's permissions in the database as |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
467 |
[read|add|delete|update]_permission relations between CWEType/CWRType |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
468 |
and CWGroup entities |
0 | 469 |
""" |
470 |
for action in erschema.ACTIONS: |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3876
diff
changeset
|
471 |
for group_or_rqlexpr in erschema.action_permissions(action): |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3876
diff
changeset
|
472 |
if isinstance(group_or_rqlexpr, basestring): |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3876
diff
changeset
|
473 |
# group |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3876
diff
changeset
|
474 |
try: |
4003
b9436fe77c9e
fix bad merge
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3998
diff
changeset
|
475 |
yield ('SET X %s_permission Y WHERE Y eid %%(g)s, ' % action, |
4011
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4010
diff
changeset
|
476 |
{'g': groupmap[group_or_rqlexpr]}) |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3876
diff
changeset
|
477 |
except KeyError: |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3876
diff
changeset
|
478 |
continue |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3876
diff
changeset
|
479 |
else: |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3876
diff
changeset
|
480 |
# rqlexpr |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3876
diff
changeset
|
481 |
rqlexpr = group_or_rqlexpr |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3876
diff
changeset
|
482 |
yield ('INSERT RQLExpression E: E expression %%(e)s, E exprtype %%(t)s, ' |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3876
diff
changeset
|
483 |
'E mainvars %%(v)s, X %s_permission E WHERE ' % action, |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3876
diff
changeset
|
484 |
{'e': unicode(rqlexpr.expression), |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3876
diff
changeset
|
485 |
'v': unicode(rqlexpr.mainvars), |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3876
diff
changeset
|
486 |
't': unicode(rqlexpr.__class__.__name__)}) |
0 | 487 |
|
488 |
||
489 |
def updateeschema2rql(eschema): |
|
490 |
relations, values = eschema_relations_values(eschema) |
|
491 |
values['et'] = eschema.type |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
492 |
yield 'SET %s WHERE X is CWEType, X name %%(et)s' % ','.join(relations), values |
0 | 493 |
|
494 |
def updaterschema2rql(rschema): |
|
495 |
relations, values = rschema_relations_values(rschema) |
|
496 |
values['rt'] = rschema.type |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
497 |
yield 'SET %s WHERE X is CWRType, X name %%(rt)s' % ','.join(relations), values |
1630
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
498 |
|
0 | 499 |
def updaterdef2rql(rschema, subjtype=None, objtype=None, props=None): |
500 |
genmap = {True: updatefrdef2rql, False: updatenfrdef2rql} |
|
501 |
return __rdef2rql(genmap, rschema, subjtype, objtype, props) |
|
502 |
||
503 |
def updatefrdef2rql(rschema, subjtype, objtype, props): |
|
504 |
relations, values = frdef_relations_values(rschema, objtype, props) |
|
505 |
values.update({'se': subjtype, 'rt': str(rschema), 'oe': objtype}) |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
506 |
yield 'SET %s WHERE %s, %s, X is CWAttribute' % (','.join(relations), |
2597
d9c5a7e0563c
[C schema seria] cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
507 |
_LOCATE_RDEF_RQL0, |
d9c5a7e0563c
[C schema seria] cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
508 |
_LOCATE_RDEF_RQL1), values |
1630
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
509 |
|
0 | 510 |
def updatenfrdef2rql(rschema, subjtype, objtype, props): |
511 |
relations, values = nfrdef_relations_values(rschema, objtype, props) |
|
512 |
values.update({'se': subjtype, 'rt': str(rschema), 'oe': objtype}) |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
513 |
yield 'SET %s WHERE %s, %s, X is CWRelation' % (','.join(relations), |
2597
d9c5a7e0563c
[C schema seria] cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
514 |
_LOCATE_RDEF_RQL0, |
d9c5a7e0563c
[C schema seria] cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
515 |
_LOCATE_RDEF_RQL1), values |