misc/migration/postcreate.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 02 Apr 2010 09:54:03 +0200
branchstable
changeset 5133 aa9c01571805
parent 4835 13b0b96d7982
child 5174 78438ad513ca
child 5421 8167de96c523
permissions -rw-r--r--
remove ref to no more existing file
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1977
606923dff11b big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
     1
"""cubicweb post creation script, set user's workflow
606923dff11b big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
     2
606923dff11b big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
     3
:organization: Logilab
4212
ab6573088b4a update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3822
diff changeset
     4
:copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2.
1977
606923dff11b big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
     5
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
606923dff11b big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
     6
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
606923dff11b big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
     7
"""
3005
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
     8
# insert versions
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
     9
create_entity('CWProperty', pkey=u'system.version.cubicweb',
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    10
              value=unicode(config.cubicweb_version()))
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    11
for cube in config.cubes():
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    12
    create_entity('CWProperty', pkey=u'system.version.%s' % cube.lower(),
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    13
                  value=unicode(config.cube_version(cube)))
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    14
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    15
# some entities have been added before schema entities, fix the 'is' and
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    16
# 'is_instance_of' relations
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    17
for rtype in ('is', 'is_instance_of'):
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    18
    sql('INSERT INTO %s_relation '
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    19
        'SELECT X.eid, ET.cw_eid FROM entities as X, cw_CWEType as ET '
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    20
        'WHERE X.type=ET.cw_name AND NOT EXISTS('
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    21
        '      SELECT 1 from is_relation '
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    22
        '      WHERE eid_from=X.eid AND eid_to=ET.cw_eid)' % rtype)
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    23
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    24
# user workflow
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    25
userwf = add_workflow(_('default user workflow'), 'CWUser')
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    26
activated = userwf.add_state(_('activated'), initial=True)
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    27
deactivated = userwf.add_state(_('deactivated'))
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    28
userwf.add_transition(_('deactivate'), (activated,), deactivated,
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    29
                      requiredgroups=('managers',))
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    30
userwf.add_transition(_('activate'), (deactivated,), activated,
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    31
                      requiredgroups=('managers',))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    32
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    33
# create anonymous user if all-in-one config and anonymous user has been specified
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    34
if hasattr(config, 'anonymous_user'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    35
    anonlogin, anonpwd = config.anonymous_user()
3822
b61c7b065a66 do not fail if anon user == manager, simply emit warning that it should not be done in production.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3005
diff changeset
    36
    if anonlogin == session.user.login:
b61c7b065a66 do not fail if anon user == manager, simply emit warning that it should not be done in production.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3005
diff changeset
    37
        print 'you are using a manager account as anonymous user.'
b61c7b065a66 do not fail if anon user == manager, simply emit warning that it should not be done in production.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3005
diff changeset
    38
        print 'Hopefully this is not a production instance...'
b61c7b065a66 do not fail if anon user == manager, simply emit warning that it should not be done in production.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3005
diff changeset
    39
    elif anonlogin:
4612
d6ae30c5d055 added a function to create admin/anon user during db initialization process so one get a chance to monkey patch it
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4212
diff changeset
    40
        from cubicweb.server import create_user
d6ae30c5d055 added a function to create admin/anon user during db initialization process so one get a chance to monkey patch it
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4212
diff changeset
    41
        create_user(session, unicode(anonlogin), anonpwd, 'guests')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    42
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2172
diff changeset
    43
# need this since we already have at least one user in the database (the default admin)
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2172
diff changeset
    44
for user in rql('Any X WHERE X is CWUser').entities():
4835
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4612
diff changeset
    45
    rql('SET X in_state S WHERE X eid %(x)s, S eid %(s)s',
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4612
diff changeset
    46
        {'x': user.eid, 's': activated.eid}, 'x')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    47
3005
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    48
# on interactive mode, ask for level 0 persistent options
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    49
if interactive_mode:
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    50
    cfg = config.persistent_options_configuration()
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    51
    cfg.input_config(inputlevel=0)
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    52
    for section, options in cfg.options_by_section():
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    53
        for optname, optdict, value in options:
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    54
            key = '%s.%s' % (section, optname)
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    55
            default = cfg.option_default(optname, optdict)
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    56
            # only record values differing from default
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    57
            if value != default:
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    58
                rql('INSERT CWProperty X: X pkey %(k)s, X value %(v)s', {'k': key, 'v': value})
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    59
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    60
# add PERM_USE_TEMPLATE_FORMAT permission
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    61
from cubicweb.schema import PERM_USE_TEMPLATE_FORMAT
3005
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    62
usetmplperm = create_entity('CWPermission', name=PERM_USE_TEMPLATE_FORMAT,
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    63
                            label=_('use template languages'))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    64
rql('SET X require_group G WHERE G name "managers", X eid %(x)s',
3005
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    65
    {'x': usetmplperm.eid}, 'x')