author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Thu, 12 Aug 2010 10:11:49 +0200 | |
branch | stable |
changeset 6101 | 1aff7367a20c |
parent 5424 | 8ecbcbff9777 |
permissions | -rw-r--r-- |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
1 |
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
2 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
3 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
4 |
# This file is part of CubicWeb. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
5 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
6 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
7 |
# terms of the GNU Lesser General Public License as published by the Free |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
8 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
9 |
# any later version. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
10 |
# |
5424
8ecbcbff9777
replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5421
diff
changeset
|
11 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
13 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
14 |
# details. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
15 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
16 |
# You should have received a copy of the GNU Lesser General Public License along |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
0 | 18 |
"""some utility functions for datastore initialization. |
19 |
||
20 |
""" |
|
21 |
__docformat__ = "restructuredtext en" |
|
22 |
||
23 |
from google.appengine.api.datastore import Key, Entity, Put, Get, Query |
|
24 |
from google.appengine.api import datastore_errors |
|
25 |
||
26 |
_GROUP_CACHE = {} # XXX use memcache |
|
27 |
||
28 |
def _get_group(groupname): |
|
29 |
try: |
|
30 |
return _GROUP_CACHE[groupname] |
|
31 |
except KeyError: |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
32 |
key = Key.from_path('CWGroup', 'key_' + groupname, parent=None) |
0 | 33 |
try: |
34 |
group = Get(key) |
|
35 |
except datastore_errors.EntityNotFoundError: |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2126
diff
changeset
|
36 |
raise Exception('can\'t find required group %s, is your instance ' |
0 | 37 |
'correctly initialized (eg did you run the ' |
38 |
'initialization script) ?' % groupname) |
|
39 |
_GROUP_CACHE[groupname] = group |
|
40 |
return group |
|
41 |
||
42 |
||
43 |
def create_user(login, password, groups): |
|
44 |
"""create a cubicweb user""" |
|
45 |
from cubicweb.server.utils import crypt_password |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
46 |
user = Entity('CWUser', name=login) |
0 | 47 |
user['s_login'] = unicode(login) |
48 |
user['s_upassword'] = crypt_password(password) |
|
49 |
set_user_groups(user, groups) |
|
50 |
Put(user) |
|
51 |
return user |
|
52 |
||
53 |
def create_groups(): |
|
54 |
"""create initial cubicweb groups""" |
|
55 |
for groupname in ('managers', 'users', 'guests'): |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
56 |
group = Entity('CWGroup', name='key_' + groupname) |
0 | 57 |
group['s_name'] = unicode(groupname) |
58 |
Put(group) |
|
59 |
_GROUP_CACHE[groupname] = group |
|
60 |
||
61 |
def set_user_groups(user, groups): |
|
62 |
"""set user in the given groups (as string). The given user entity |
|
63 |
(datastore.Entity) is not putted back to the repository, this is the caller |
|
64 |
responsability. |
|
65 |
""" |
|
66 |
groups = [_get_group(g) for g in groups] |
|
67 |
user['s_in_group'] = [g.key() for g in groups] or None |
|
68 |
for group in groups: |
|
69 |
try: |
|
70 |
group['o_in_group'].append(user.key()) |
|
71 |
except (KeyError, AttributeError): |
|
72 |
group['o_in_group'] = [user.key()] |
|
73 |
Put(group) |
|
74 |
||
75 |
def init_relations(gaeentity, eschema): |
|
76 |
"""set None for every subject relations which is not yet defined""" |
|
77 |
for rschema in eschema.subject_relations(): |
|
78 |
if rschema in ('identity', 'has_text'): |
|
79 |
continue |
|
80 |
dsrelation = 's_' + rschema.type |
|
81 |
if not dsrelation in gaeentity: |
|
82 |
gaeentity[dsrelation] = None |
|
83 |
for rschema in eschema.object_relations(): |
|
84 |
if rschema == 'identity': |
|
85 |
continue |
|
86 |
dsrelation = 'o_' + rschema.type |
|
87 |
if not dsrelation in gaeentity: |
|
88 |
gaeentity[dsrelation] = None |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
89 |
|
0 | 90 |
def fix_entities(schema): |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
91 |
for etype in ('CWUser', 'CWGroup'): |
0 | 92 |
eschema = schema.eschema(etype) |
93 |
for gaeentity in Query(etype).Run(): |
|
94 |
init_relations(gaeentity, eschema) |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
95 |
# XXX o_is on CWEType entity |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
96 |
gaeentity['s_is'] = Key.from_path('CWEType', 'key_' + etype, parent=None) |
0 | 97 |
Put(gaeentity) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
98 |
|
0 | 99 |
def init_persistent_schema(ssession, schema): |
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
100 |
execute = ssession.execute |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
101 |
rql = ('INSERT CWEType X: X name %(name)s, X description %(descr)s,' |
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
|
102 |
'X final FALSE') |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
103 |
eschema = schema.eschema('CWEType') |
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
|
104 |
execute(rql, {'name': u'CWEType', 'descr': unicode(eschema.description)}) |
0 | 105 |
for eschema in schema.entities(): |
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
106 |
if eschema.final or eschema == 'CWEType': |
0 | 107 |
continue |
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
|
108 |
execute(rql, {'name': unicode(eschema), |
0 | 109 |
'descr': unicode(eschema.description)}) |
110 |
||
111 |
def insert_versions(ssession, config): |
|
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
112 |
execute = ssession.execute |
0 | 113 |
# insert versions |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
114 |
execute('INSERT CWProperty X: X pkey %(pk)s, X value%(v)s', |
0 | 115 |
{'pk': u'system.version.cubicweb', |
116 |
'v': unicode(config.cubicweb_version())}) |
|
117 |
for cube in config.cubes(): |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
118 |
execute('INSERT CWProperty X: X pkey %(pk)s, X value%(v)s', |
0 | 119 |
{'pk': u'system.version.%s' % cube, |
120 |
'v': unicode(config.cube_version(cube))}) |