goa/dbinit.py
brancholdstable
changeset 6665 90f2f20367bc
parent 6018 f4d1d5d9ccbb
parent 6661 1719137de7da
child 6701 fd4267ecbba6
child 6710 a89dc08e5970
equal deleted inserted replaced
6018:f4d1d5d9ccbb 6665:90f2f20367bc
     1 # copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
       
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     3 #
       
     4 # This file is part of CubicWeb.
       
     5 #
       
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
       
     7 # terms of the GNU Lesser General Public License as published by the Free
       
     8 # Software Foundation, either version 2.1 of the License, or (at your option)
       
     9 # any later version.
       
    10 #
       
    11 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT
       
    12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
       
    13 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
       
    14 # details.
       
    15 #
       
    16 # You should have received a copy of the GNU Lesser General Public License along
       
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
       
    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:
       
    32         key = Key.from_path('CWGroup', 'key_' + groupname, parent=None)
       
    33         try:
       
    34             group = Get(key)
       
    35         except datastore_errors.EntityNotFoundError:
       
    36             raise Exception('can\'t find required group %s, is your instance '
       
    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
       
    46     user = Entity('CWUser', name=login)
       
    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'):
       
    56         group = Entity('CWGroup', name='key_' + groupname)
       
    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
       
    89 
       
    90 def fix_entities(schema):
       
    91     for etype in ('CWUser', 'CWGroup'):
       
    92         eschema = schema.eschema(etype)
       
    93         for gaeentity in Query(etype).Run():
       
    94             init_relations(gaeentity, eschema)
       
    95             # XXX o_is on CWEType entity
       
    96             gaeentity['s_is'] = Key.from_path('CWEType', 'key_' + etype, parent=None)
       
    97             Put(gaeentity)
       
    98 
       
    99 def init_persistent_schema(ssession, schema):
       
   100     execute = ssession.execute
       
   101     rql = ('INSERT CWEType X: X name %(name)s, X description %(descr)s,'
       
   102            'X final FALSE')
       
   103     eschema = schema.eschema('CWEType')
       
   104     execute(rql, {'name': u'CWEType', 'descr': unicode(eschema.description)})
       
   105     for eschema in schema.entities():
       
   106         if eschema.final or eschema == 'CWEType':
       
   107             continue
       
   108         execute(rql, {'name': unicode(eschema),
       
   109                       'descr': unicode(eschema.description)})
       
   110 
       
   111 def insert_versions(ssession, config):
       
   112     execute = ssession.execute
       
   113     # insert versions
       
   114     execute('INSERT CWProperty X: X pkey %(pk)s, X value%(v)s',
       
   115             {'pk': u'system.version.cubicweb',
       
   116              'v': unicode(config.cubicweb_version())})
       
   117     for cube in config.cubes():
       
   118         execute('INSERT CWProperty X: X pkey %(pk)s, X value%(v)s',
       
   119                 {'pk': u'system.version.%s' % cube,
       
   120                  'v': unicode(config.cube_version(cube))})