goa/dbinit.py
author Julien Jehannet <julien.jehannet@logilab.fr>
Fri, 05 Feb 2010 17:13:53 +0100
changeset 4527 67ab70e98488
parent 4212 ab6573088b4a
child 4835 13b0b96d7982
permissions -rw-r--r--
[R] devtools: improve default data import mechanism Validation chain is now possible with checkers Before that the expected values needed to be coherent. Now, we can use ObjectStore to validate the input data * add new input transformers: - uppercase - lowercase * add new input checkers (raise AssertionError on error): - decimal: take care of possible comma character as number separator - integer: cast to int() - yesno: to validate boolean value - isalpha - required: input value *must* not be empty * new control checker: - optional: block possible exception we delete field in the returned dict instead of raising AssertionError (exclusive with required) Helper methods to manipulate indexes: * build_rqlindex() is used to build index based on already created entities * fetch() replace get_one()/get_many() methods by factorizing code Minor changes in reporting: * use tell() for all printing * let new value for askerrors to display automatically the report (used in crontab)

"""some utility functions for datastore initialization.

:organization: Logilab
:copyright: 2008-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2.
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
"""
__docformat__ = "restructuredtext en"

from google.appengine.api.datastore import Key, Entity, Put, Get, Query
from google.appengine.api import datastore_errors

_GROUP_CACHE = {} # XXX use memcache

def _get_group(groupname):
    try:
        return _GROUP_CACHE[groupname]
    except KeyError:
        key = Key.from_path('CWGroup', 'key_' + groupname, parent=None)
        try:
            group = Get(key)
        except datastore_errors.EntityNotFoundError:
            raise Exception('can\'t find required group %s, is your instance '
                            'correctly initialized (eg did you run the '
                            'initialization script) ?' % groupname)
        _GROUP_CACHE[groupname] = group
        return group


def create_user(login, password, groups):
    """create a cubicweb user"""
    from cubicweb.server.utils import crypt_password
    user = Entity('CWUser', name=login)
    user['s_login'] = unicode(login)
    user['s_upassword'] = crypt_password(password)
    set_user_groups(user, groups)
    Put(user)
    return user

def create_groups():
    """create initial cubicweb groups"""
    for groupname in ('managers', 'users', 'guests'):
        group = Entity('CWGroup', name='key_' + groupname)
        group['s_name'] = unicode(groupname)
        Put(group)
        _GROUP_CACHE[groupname] = group

def set_user_groups(user, groups):
    """set user in the given groups (as string). The given user entity
    (datastore.Entity) is not putted back to the repository, this is the caller
    responsability.
    """
    groups = [_get_group(g) for g in groups]
    user['s_in_group'] = [g.key() for g in groups] or None
    for group in groups:
        try:
            group['o_in_group'].append(user.key())
        except (KeyError, AttributeError):
            group['o_in_group'] = [user.key()]
        Put(group)

def init_relations(gaeentity, eschema):
    """set None for every subject relations which is not yet defined"""
    for rschema in eschema.subject_relations():
        if rschema in ('identity', 'has_text'):
            continue
        dsrelation = 's_' + rschema.type
        if not dsrelation in gaeentity:
            gaeentity[dsrelation] = None
    for rschema in eschema.object_relations():
        if rschema == 'identity':
            continue
        dsrelation = 'o_' + rschema.type
        if not dsrelation in gaeentity:
            gaeentity[dsrelation] = None

def fix_entities(schema):
    for etype in ('CWUser', 'CWGroup'):
        eschema = schema.eschema(etype)
        for gaeentity in Query(etype).Run():
            init_relations(gaeentity, eschema)
            # XXX o_is on CWEType entity
            gaeentity['s_is'] = Key.from_path('CWEType', 'key_' + etype, parent=None)
            Put(gaeentity)

def init_persistent_schema(ssession, schema):
    execute = ssession.unsafe_execute
    rql = ('INSERT CWEType X: X name %(name)s, X description %(descr)s,'
           'X final FALSE')
    eschema = schema.eschema('CWEType')
    execute(rql, {'name': u'CWEType', 'descr': unicode(eschema.description)})
    for eschema in schema.entities():
        if eschema.final or eschema == 'CWEType':
            continue
        execute(rql, {'name': unicode(eschema),
                      'descr': unicode(eschema.description)})

def insert_versions(ssession, config):
    execute = ssession.unsafe_execute
    # insert versions
    execute('INSERT CWProperty X: X pkey %(pk)s, X value%(v)s',
            {'pk': u'system.version.cubicweb',
             'v': unicode(config.cubicweb_version())})
    for cube in config.cubes():
        execute('INSERT CWProperty X: X pkey %(pk)s, X value%(v)s',
                {'pk': u'system.version.%s' % cube,
                 'v': unicode(config.cube_version(cube))})