cubicweb/misc/migration/postcreate.py
author Denis Laxalde <denis.laxalde@logilab.fr>
Fri, 05 Apr 2019 17:58:19 +0200
changeset 12567 26744ad37953
parent 11129 97095348b3ee
permissions -rw-r--r--
Drop python2 support This mostly consists in removing the dependency on "six" and updating the code to use only Python3 idioms. Notice that we previously used TemporaryDirectory from cubicweb.devtools.testlib for compatibility with Python2. We now directly import it from tempfile.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8544
3d049071957e massive copyright update to avoid clutering later patches
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7797
diff changeset
     1
# copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
     3
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
     4
# This file is part of CubicWeb.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
     5
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
     9
# any later version.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
    10
#
5424
8ecbcbff9777 replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5421
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
    14
# details.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
    15
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
6427
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
    18
"""cubicweb post creation script, set user's workflow"""
10817
7b154e0fa194 Use cubicweb._
Julien Cristau <julien.cristau@logilab.fr>
parents: 10803
diff changeset
    19
from cubicweb import _
7b154e0fa194 Use cubicweb._
Julien Cristau <julien.cristau@logilab.fr>
parents: 10803
diff changeset
    20
7b154e0fa194 Use cubicweb._
Julien Cristau <julien.cristau@logilab.fr>
parents: 10803
diff changeset
    21
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
    22
# 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
    23
create_entity('CWProperty', pkey=u'system.version.cubicweb',
12567
26744ad37953 Drop python2 support
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11129
diff changeset
    24
              value=str(config.cubicweb_version()))
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
    25
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
    26
    create_entity('CWProperty', pkey=u'system.version.%s' % cube.lower(),
12567
26744ad37953 Drop python2 support
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11129
diff changeset
    27
                  value=str(config.cube_version(cube)))
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
    28
11075
881b3d9ee2b3 cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10817
diff changeset
    29
# some entities have been added before schema entities, add their missing 'is' and
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
    30
# '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
    31
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
    32
    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
    33
        '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
    34
        'WHERE X.type=ET.cw_name AND NOT EXISTS('
10803
d261bd7c47cf [postcreate] properly set is_instance_of for bootstrap entities
Julien Cristau <julien.cristau@logilab.fr>
parents: 10684
diff changeset
    35
        '      SELECT 1 from %s_relation '
d261bd7c47cf [postcreate] properly set is_instance_of for bootstrap entities
Julien Cristau <julien.cristau@logilab.fr>
parents: 10684
diff changeset
    36
        '      WHERE eid_from=X.eid AND eid_to=ET.cw_eid)' % (rtype, rtype))
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
    37
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    38
# 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
    39
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
    40
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
    41
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
    42
userwf.add_transition(_('deactivate'), (activated,), deactivated,
10473
23a2fa8cb725 avoid sanitizing warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8544
diff changeset
    43
                      requiredgroups=(u'managers',))
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
    44
userwf.add_transition(_('activate'), (deactivated,), activated,
10473
23a2fa8cb725 avoid sanitizing warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8544
diff changeset
    45
                      requiredgroups=(u'managers',))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    46
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    47
# 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
    48
if hasattr(config, 'anonymous_user'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    49
    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
    50
    if anonlogin == session.user.login:
10589
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10473
diff changeset
    51
        print('you are using a manager account as anonymous user.')
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10473
diff changeset
    52
        print('Hopefully this is not a production instance...')
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
    53
    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
    54
        from cubicweb.server import create_user
12567
26744ad37953 Drop python2 support
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11129
diff changeset
    55
        create_user(session, anonlogin, anonpwd, u'guests')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    56
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2172
diff changeset
    57
# 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
    58
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
    59
    rql('SET X in_state S WHERE X eid %(x)s, S eid %(s)s',
5174
78438ad513ca #759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
    60
        {'x': user.eid, 's': activated.eid})
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    61
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
# 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
    63
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
    64
    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
    65
    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
    66
    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
    67
        for optname, optdict, value in options:
10473
23a2fa8cb725 avoid sanitizing warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8544
diff changeset
    68
            key = u'%s.%s' % (section, optname)
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
    69
            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
    70
            # 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
    71
            if value != default:
5174
78438ad513ca #759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
    72
                rql('INSERT CWProperty X: X pkey %(k)s, X value %(v)s',
78438ad513ca #759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
    73
                    {'k': key, 'v': value})