|
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: |
|
18 key = Key.from_path('EGroup', 'key_' + groupname, parent=None) |
|
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 |
|
32 user = Entity('EUser', name=login) |
|
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'): |
|
42 group = Entity('EGroup', name='key_' + groupname) |
|
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): |
|
77 for etype in ('EUser', 'EGroup'): |
|
78 eschema = schema.eschema(etype) |
|
79 for gaeentity in Query(etype).Run(): |
|
80 init_relations(gaeentity, eschema) |
|
81 # XXX o_is on EEType entity |
|
82 gaeentity['s_is'] = Key.from_path('EEType', 'key_' + etype, parent=None) |
|
83 Put(gaeentity) |
|
84 |
|
85 def init_persistent_schema(ssession, schema): |
|
86 execute = ssession.unsafe_execute |
|
87 rql = ('INSERT EEType X: X name %(name)s, X description %(descr)s,' |
|
88 'X final FALSE, X meta %(meta)s') |
|
89 eschema = schema.eschema('EEType') |
|
90 execute(rql, {'name': u'EEType', 'descr': unicode(eschema.description), |
|
91 'meta': eschema.meta}) |
|
92 for eschema in schema.entities(): |
|
93 if eschema.is_final() or eschema == 'EEType': |
|
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 |
|
101 execute('INSERT EProperty X: X pkey %(pk)s, X value%(v)s', |
|
102 {'pk': u'system.version.cubicweb', |
|
103 'v': unicode(config.cubicweb_version())}) |
|
104 for cube in config.cubes(): |
|
105 execute('INSERT EProperty X: X pkey %(pk)s, X value%(v)s', |
|
106 {'pk': u'system.version.%s' % cube, |
|
107 'v': unicode(config.cube_version(cube))}) |
|
108 |