author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Mon, 21 Dec 2009 18:45:03 +0100 | |
changeset 4153 | 1e0b30474454 |
parent 3689 | deb13e88e037 |
child 4212 | ab6573088b4a |
permissions | -rw-r--r-- |
0 | 1 |
"""some utility functions for datastore initialization. |
2 |
||
3 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
4 |
:copyright: 2008-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 7 |
""" |
8 |
__docformat__ = "restructuredtext en" |
|
9 |
||
10 |
from google.appengine.api.datastore import Key, Entity, Put, Get, Query |
|
11 |
from google.appengine.api import datastore_errors |
|
12 |
||
13 |
_GROUP_CACHE = {} # XXX use memcache |
|
14 |
||
15 |
def _get_group(groupname): |
|
16 |
try: |
|
17 |
return _GROUP_CACHE[groupname] |
|
18 |
except KeyError: |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
19 |
key = Key.from_path('CWGroup', 'key_' + groupname, parent=None) |
0 | 20 |
try: |
21 |
group = Get(key) |
|
22 |
except datastore_errors.EntityNotFoundError: |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2126
diff
changeset
|
23 |
raise Exception('can\'t find required group %s, is your instance ' |
0 | 24 |
'correctly initialized (eg did you run the ' |
25 |
'initialization script) ?' % groupname) |
|
26 |
_GROUP_CACHE[groupname] = group |
|
27 |
return group |
|
28 |
||
29 |
||
30 |
def create_user(login, password, groups): |
|
31 |
"""create a cubicweb user""" |
|
32 |
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
|
33 |
user = Entity('CWUser', name=login) |
0 | 34 |
user['s_login'] = unicode(login) |
35 |
user['s_upassword'] = crypt_password(password) |
|
36 |
set_user_groups(user, groups) |
|
37 |
Put(user) |
|
38 |
return user |
|
39 |
||
40 |
def create_groups(): |
|
41 |
"""create initial cubicweb groups""" |
|
42 |
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
|
43 |
group = Entity('CWGroup', name='key_' + groupname) |
0 | 44 |
group['s_name'] = unicode(groupname) |
45 |
Put(group) |
|
46 |
_GROUP_CACHE[groupname] = group |
|
47 |
||
48 |
def set_user_groups(user, groups): |
|
49 |
"""set user in the given groups (as string). The given user entity |
|
50 |
(datastore.Entity) is not putted back to the repository, this is the caller |
|
51 |
responsability. |
|
52 |
""" |
|
53 |
groups = [_get_group(g) for g in groups] |
|
54 |
user['s_in_group'] = [g.key() for g in groups] or None |
|
55 |
for group in groups: |
|
56 |
try: |
|
57 |
group['o_in_group'].append(user.key()) |
|
58 |
except (KeyError, AttributeError): |
|
59 |
group['o_in_group'] = [user.key()] |
|
60 |
Put(group) |
|
61 |
||
62 |
def init_relations(gaeentity, eschema): |
|
63 |
"""set None for every subject relations which is not yet defined""" |
|
64 |
for rschema in eschema.subject_relations(): |
|
65 |
if rschema in ('identity', 'has_text'): |
|
66 |
continue |
|
67 |
dsrelation = 's_' + rschema.type |
|
68 |
if not dsrelation in gaeentity: |
|
69 |
gaeentity[dsrelation] = None |
|
70 |
for rschema in eschema.object_relations(): |
|
71 |
if rschema == 'identity': |
|
72 |
continue |
|
73 |
dsrelation = 'o_' + rschema.type |
|
74 |
if not dsrelation in gaeentity: |
|
75 |
gaeentity[dsrelation] = None |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
76 |
|
0 | 77 |
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
|
78 |
for etype in ('CWUser', 'CWGroup'): |
0 | 79 |
eschema = schema.eschema(etype) |
80 |
for gaeentity in Query(etype).Run(): |
|
81 |
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
|
82 |
# 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
|
83 |
gaeentity['s_is'] = Key.from_path('CWEType', 'key_' + etype, parent=None) |
0 | 84 |
Put(gaeentity) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
85 |
|
0 | 86 |
def init_persistent_schema(ssession, schema): |
87 |
execute = ssession.unsafe_execute |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
88 |
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
|
89 |
'X final FALSE') |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
90 |
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
|
91 |
execute(rql, {'name': u'CWEType', 'descr': unicode(eschema.description)}) |
0 | 92 |
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
|
93 |
if eschema.final or eschema == 'CWEType': |
0 | 94 |
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
|
95 |
execute(rql, {'name': unicode(eschema), |
0 | 96 |
'descr': unicode(eschema.description)}) |
97 |
||
98 |
def insert_versions(ssession, config): |
|
99 |
execute = ssession.unsafe_execute |
|
100 |
# insert versions |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
101 |
execute('INSERT CWProperty X: X pkey %(pk)s, X value%(v)s', |
0 | 102 |
{'pk': u'system.version.cubicweb', |
103 |
'v': unicode(config.cubicweb_version())}) |
|
104 |
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
|
105 |
execute('INSERT CWProperty X: X pkey %(pk)s, X value%(v)s', |
0 | 106 |
{'pk': u'system.version.%s' % cube, |
107 |
'v': unicode(config.cube_version(cube))}) |