misc/migration/postcreate.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 20 Aug 2009 17:44:27 +0200
branch3.5
changeset 2920 64322aa83a1d
parent 2172 cf8f9180e63e
child 3005 a50d03e7014f
permissions -rw-r--r--
start a new workflow engine

"""cubicweb post creation script, set user's workflow

:organization: Logilab
:copyright: 2001-2009 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
"""

activatedeid = add_state(_('activated'), 'CWUser', initial=True)
deactivatedeid = add_state(_('deactivated'), 'CWUser')
add_transition(_('deactivate'), 'CWUser',
               (activatedeid,), deactivatedeid,
               requiredgroups=('managers',))
add_transition(_('activate'), 'CWUser',
               (deactivatedeid,), activatedeid,
               requiredgroups=('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:
        rql('INSERT CWUser X: X login %(login)s, X upassword %(pwd)s,'
            'X in_group G WHERE G name "guests"',
            {'login': unicode(anonlogin), 'pwd': anonpwd})

# 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():
    session.unsafe_execute('SET X in_state S WHERE X eid %(x)s, S eid %(s)s',
                           {'x': user.eid, 's': activatedeid}, 'x')

cfg = config.persistent_options_configuration()
if interactive_mode:
    cfg.input_config(inputlevel=0)

for section, options in cfg.options_by_section():
    for optname, optdict, value in options:
        key = '%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})

# add PERM_USE_TEMPLATE_FORMAT permission
from cubicweb.schema import PERM_USE_TEMPLATE_FORMAT
eid = add_entity('CWPermission', name=PERM_USE_TEMPLATE_FORMAT,
                 label=_('use template languages'))
rql('SET X require_group G WHERE G name "managers", X eid %(x)s',
    {'x': eid}, 'x')