cubicweb/misc/migration/postcreate.py
author Denis Laxalde <denis.laxalde@logilab.fr>
Fri, 22 Mar 2019 17:29:15 +0100
changeset 12539 10159a3d1d72
parent 11129 97095348b3ee
child 12567 26744ad37953
permissions -rw-r--r--
Drop deprecated CWCache entity type
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"""
10589
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10473
diff changeset
    19
from __future__ import print_function
1977
606923dff11b big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
    20
10684
7e9c9a32f24f [py3k] unicode → six.text_type
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10589
diff changeset
    21
from six import text_type
7e9c9a32f24f [py3k] unicode → six.text_type
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10589
diff changeset
    22
10817
7b154e0fa194 Use cubicweb._
Julien Cristau <julien.cristau@logilab.fr>
parents: 10803
diff changeset
    23
from cubicweb import _
7b154e0fa194 Use cubicweb._
Julien Cristau <julien.cristau@logilab.fr>
parents: 10803
diff changeset
    24
7b154e0fa194 Use cubicweb._
Julien Cristau <julien.cristau@logilab.fr>
parents: 10803
diff changeset
    25
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
    26
# 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
    27
create_entity('CWProperty', pkey=u'system.version.cubicweb',
10684
7e9c9a32f24f [py3k] unicode → six.text_type
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10589
diff changeset
    28
              value=text_type(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
    29
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
    30
    create_entity('CWProperty', pkey=u'system.version.%s' % cube.lower(),
10684
7e9c9a32f24f [py3k] unicode → six.text_type
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10589
diff changeset
    31
                  value=text_type(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
    32
11075
881b3d9ee2b3 cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10817
diff changeset
    33
# 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
    34
# '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
    35
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
    36
    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
    37
        '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
    38
        '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
    39
        '      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
    40
        '      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
    41
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
# 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
    43
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
    44
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
    45
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
    46
userwf.add_transition(_('deactivate'), (activated,), deactivated,
10473
23a2fa8cb725 avoid sanitizing warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8544
diff changeset
    47
                      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
    48
userwf.add_transition(_('activate'), (deactivated,), activated,
10473
23a2fa8cb725 avoid sanitizing warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8544
diff changeset
    49
                      requiredgroups=(u'managers',))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    50
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    51
# 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
    52
if hasattr(config, 'anonymous_user'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    53
    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
    54
    if anonlogin == session.user.login:
10589
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10473
diff changeset
    55
        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
    56
        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
    57
    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
    58
        from cubicweb.server import create_user
10684
7e9c9a32f24f [py3k] unicode → six.text_type
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10589
diff changeset
    59
        create_user(session, text_type(anonlogin), anonpwd, u'guests')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    60
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2172
diff changeset
    61
# 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
    62
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
    63
    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
    64
        {'x': user.eid, 's': activated.eid})
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    65
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
    66
# 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
    67
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
    68
    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
    69
    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
    70
    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
    71
        for optname, optdict, value in options:
10473
23a2fa8cb725 avoid sanitizing warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8544
diff changeset
    72
            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
    73
            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
    74
            # 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
    75
            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
    76
                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
    77
                    {'k': key, 'v': value})