author | sylvain.thenault@logilab.fr |
Fri, 17 Apr 2009 16:55:37 +0200 | |
branch | tls-sprint |
changeset 1398 | 5fe84a5f7035 |
parent 0 | b97547f5f1fa |
child 1802 | d628defebc17 |
permissions | -rw-r--r-- |
0 | 1 |
"""some utility functions for datastore initialization. |
2 |
||
3 |
:organization: Logilab |
|
4 |
:copyright: 2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
|
5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
|
6 |
""" |
|
7 |
__docformat__ = "restructuredtext en" |
|
8 |
||
9 |
from google.appengine.api.datastore import Key, Entity, Put, Get, Query |
|
10 |
from google.appengine.api import datastore_errors |
|
11 |
||
12 |
_GROUP_CACHE = {} # XXX use memcache |
|
13 |
||
14 |
def _get_group(groupname): |
|
15 |
try: |
|
16 |
return _GROUP_CACHE[groupname] |
|
17 |
except KeyError: |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
18 |
key = Key.from_path('CWGroup', 'key_' + groupname, parent=None) |
0 | 19 |
try: |
20 |
group = Get(key) |
|
21 |
except datastore_errors.EntityNotFoundError: |
|
22 |
raise Exception('can\'t find required group %s, is your application ' |
|
23 |
'correctly initialized (eg did you run the ' |
|
24 |
'initialization script) ?' % groupname) |
|
25 |
_GROUP_CACHE[groupname] = group |
|
26 |
return group |
|
27 |
||
28 |
||
29 |
def create_user(login, password, groups): |
|
30 |
"""create a cubicweb user""" |
|
31 |
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
|
32 |
user = Entity('CWUser', name=login) |
0 | 33 |
user['s_login'] = unicode(login) |
34 |
user['s_upassword'] = crypt_password(password) |
|
35 |
set_user_groups(user, groups) |
|
36 |
Put(user) |
|
37 |
return user |
|
38 |
||
39 |
def create_groups(): |
|
40 |
"""create initial cubicweb groups""" |
|
41 |
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
|
42 |
group = Entity('CWGroup', name='key_' + groupname) |
0 | 43 |
group['s_name'] = unicode(groupname) |
44 |
Put(group) |
|
45 |
_GROUP_CACHE[groupname] = group |
|
46 |
||
47 |
def set_user_groups(user, groups): |
|
48 |
"""set user in the given groups (as string). The given user entity |
|
49 |
(datastore.Entity) is not putted back to the repository, this is the caller |
|
50 |
responsability. |
|
51 |
""" |
|
52 |
groups = [_get_group(g) for g in groups] |
|
53 |
user['s_in_group'] = [g.key() for g in groups] or None |
|
54 |
for group in groups: |
|
55 |
try: |
|
56 |
group['o_in_group'].append(user.key()) |
|
57 |
except (KeyError, AttributeError): |
|
58 |
group['o_in_group'] = [user.key()] |
|
59 |
Put(group) |
|
60 |
||
61 |
def init_relations(gaeentity, eschema): |
|
62 |
"""set None for every subject relations which is not yet defined""" |
|
63 |
for rschema in eschema.subject_relations(): |
|
64 |
if rschema in ('identity', 'has_text'): |
|
65 |
continue |
|
66 |
dsrelation = 's_' + rschema.type |
|
67 |
if not dsrelation in gaeentity: |
|
68 |
gaeentity[dsrelation] = None |
|
69 |
for rschema in eschema.object_relations(): |
|
70 |
if rschema == 'identity': |
|
71 |
continue |
|
72 |
dsrelation = 'o_' + rschema.type |
|
73 |
if not dsrelation in gaeentity: |
|
74 |
gaeentity[dsrelation] = None |
|
75 |
||
76 |
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
|
77 |
for etype in ('CWUser', 'CWGroup'): |
0 | 78 |
eschema = schema.eschema(etype) |
79 |
for gaeentity in Query(etype).Run(): |
|
80 |
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
|
81 |
# 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
|
82 |
gaeentity['s_is'] = Key.from_path('CWEType', 'key_' + etype, parent=None) |
0 | 83 |
Put(gaeentity) |
84 |
||
85 |
def init_persistent_schema(ssession, schema): |
|
86 |
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
|
87 |
rql = ('INSERT CWEType X: X name %(name)s, X description %(descr)s,' |
0 | 88 |
'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
|
89 |
eschema = schema.eschema('CWEType') |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
90 |
execute(rql, {'name': u'CWEType', 'descr': unicode(eschema.description), |
0 | 91 |
'meta': eschema.meta}) |
92 |
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
|
93 |
if eschema.is_final() or eschema == 'CWEType': |
0 | 94 |
continue |
95 |
execute(rql, {'name': unicode(eschema), 'meta': eschema.meta, |
|
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))}) |
|
108 |