author | Aurelien Campeas <aurelien.campeas@logilab.fr> |
Wed, 15 Jul 2009 17:48:24 +0200 | |
branch | stable |
changeset 2377 | fc1fea0e87c5 |
parent 1977 | 606923dff11b |
child 2126 | a25859917ccc |
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: |
|
23 |
raise Exception('can\'t find required group %s, is your application ' |
|
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,' |
0 | 89 |
'X final FALSE, X meta %(meta)s') |
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') |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
91 |
execute(rql, {'name': u'CWEType', 'descr': unicode(eschema.description), |
0 | 92 |
'meta': eschema.meta}) |
93 |
for eschema in schema.entities(): |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
94 |
if eschema.is_final() or eschema == 'CWEType': |
0 | 95 |
continue |
96 |
execute(rql, {'name': unicode(eschema), 'meta': eschema.meta, |
|
97 |
'descr': unicode(eschema.description)}) |
|
98 |
||
99 |
def insert_versions(ssession, config): |
|
100 |
execute = ssession.unsafe_execute |
|
101 |
# insert versions |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
102 |
execute('INSERT CWProperty X: X pkey %(pk)s, X value%(v)s', |
0 | 103 |
{'pk': u'system.version.cubicweb', |
104 |
'v': unicode(config.cubicweb_version())}) |
|
105 |
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
|
106 |
execute('INSERT CWProperty X: X pkey %(pk)s, X value%(v)s', |
0 | 107 |
{'pk': u'system.version.%s' % cube, |
108 |
'v': unicode(config.cube_version(cube))}) |