cubicweb/misc/migration/postcreate.py
author Denis Laxalde <denis.laxalde@logilab.fr>
Tue, 22 Dec 2015 09:23:00 +0100
changeset 11144 fd8bf29ed00e
parent 11129 97095348b3ee
child 12567 26744ad37953
permissions -rw-r--r--
[tox] Generate test environments for Python 2.7 and 3.4 Test commands for each environment are written down explicitly since I could not find a way to extract the "package" name (e.g. "hooks") from the environment name (e.g. "py34-hooks"). For Python 3.4 interpreter, only environments (subpackages) that do not depend on cubes for their tests are listed since those test dependency cubes are not yet installable with Python 3.x. etwist is also not included since the Python 3 port is not complete at the moment. From local testing, py34 tests pass for the following subpackages: - dataimport - entities - ext - hooks - wsgi

# copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
#
# This file is part of CubicWeb.
#
# CubicWeb is free software: you can redistribute it and/or modify it under the
# terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 2.1 of the License, or (at your option)
# any later version.
#
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
"""cubicweb post creation script, set user's workflow"""
from __future__ import print_function

from six import text_type

from cubicweb import _


# insert versions
create_entity('CWProperty', pkey=u'system.version.cubicweb',
              value=text_type(config.cubicweb_version()))
for cube in config.cubes():
    create_entity('CWProperty', pkey=u'system.version.%s' % cube.lower(),
                  value=text_type(config.cube_version(cube)))

# some entities have been added before schema entities, add their missing 'is' and
# 'is_instance_of' relations
for rtype in ('is', 'is_instance_of'):
    sql('INSERT INTO %s_relation '
        'SELECT X.eid, ET.cw_eid FROM entities as X, cw_CWEType as ET '
        'WHERE X.type=ET.cw_name AND NOT EXISTS('
        '      SELECT 1 from %s_relation '
        '      WHERE eid_from=X.eid AND eid_to=ET.cw_eid)' % (rtype, rtype))

# user workflow
userwf = add_workflow(_('default user workflow'), 'CWUser')
activated = userwf.add_state(_('activated'), initial=True)
deactivated = userwf.add_state(_('deactivated'))
userwf.add_transition(_('deactivate'), (activated,), deactivated,
                      requiredgroups=(u'managers',))
userwf.add_transition(_('activate'), (deactivated,), activated,
                      requiredgroups=(u'managers',))

# create anonymous user if all-in-one config and anonymous user has been specified
if hasattr(config, 'anonymous_user'):
    anonlogin, anonpwd = config.anonymous_user()
    if anonlogin == session.user.login:
        print('you are using a manager account as anonymous user.')
        print('Hopefully this is not a production instance...')
    elif anonlogin:
        from cubicweb.server import create_user
        create_user(session, text_type(anonlogin), anonpwd, u'guests')

# need this since we already have at least one user in the database (the default admin)
for user in rql('Any X WHERE X is CWUser').entities():
    rql('SET X in_state S WHERE X eid %(x)s, S eid %(s)s',
        {'x': user.eid, 's': activated.eid})

# on interactive mode, ask for level 0 persistent options
if interactive_mode:
    cfg = config.persistent_options_configuration()
    cfg.input_config(inputlevel=0)
    for section, options in cfg.options_by_section():
        for optname, optdict, value in options:
            key = u'%s.%s' % (section, optname)
            default = cfg.option_default(optname, optdict)
            # only record values differing from default
            if value != default:
                rql('INSERT CWProperty X: X pkey %(k)s, X value %(v)s',
                    {'k': key, 'v': value})