misc/migration/postcreate.py
branch3.5
changeset 3005 a50d03e7014f
parent 2920 64322aa83a1d
child 3822 b61c7b065a66
equal deleted inserted replaced
3003:2944ee420dca 3005:a50d03e7014f
     4 :copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2.
     4 :copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2.
     5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
     5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
     6 :license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
     6 :license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
     7 """
     7 """
     8 
     8 
     9 activatedeid = add_state(_('activated'), 'CWUser', initial=True)
     9 # insert versions
    10 deactivatedeid = add_state(_('deactivated'), 'CWUser')
    10 create_entity('CWProperty', pkey=u'system.version.cubicweb',
    11 add_transition(_('deactivate'), 'CWUser',
    11               value=unicode(config.cubicweb_version()))
    12                (activatedeid,), deactivatedeid,
    12 for cube in config.cubes():
    13                requiredgroups=('managers',))
    13     create_entity('CWProperty', pkey=u'system.version.%s' % cube.lower(),
    14 add_transition(_('activate'), 'CWUser',
    14                   value=unicode(config.cube_version(cube)))
    15                (deactivatedeid,), activatedeid,
    15 
    16                requiredgroups=('managers',))
    16 # some entities have been added before schema entities, fix the 'is' and
       
    17 # 'is_instance_of' relations
       
    18 for rtype in ('is', 'is_instance_of'):
       
    19     sql('INSERT INTO %s_relation '
       
    20         'SELECT X.eid, ET.cw_eid FROM entities as X, cw_CWEType as ET '
       
    21         'WHERE X.type=ET.cw_name AND NOT EXISTS('
       
    22         '      SELECT 1 from is_relation '
       
    23         '      WHERE eid_from=X.eid AND eid_to=ET.cw_eid)' % rtype)
       
    24 
       
    25 # user workflow
       
    26 userwf = add_workflow(_('default user workflow'), 'CWUser')
       
    27 activated = userwf.add_state(_('activated'), initial=True)
       
    28 deactivated = userwf.add_state(_('deactivated'))
       
    29 userwf.add_transition(_('deactivate'), (activated,), deactivated,
       
    30                       requiredgroups=('managers',))
       
    31 userwf.add_transition(_('activate'), (deactivated,), activated,
       
    32                       requiredgroups=('managers',))
    17 
    33 
    18 # create anonymous user if all-in-one config and anonymous user has been specified
    34 # create anonymous user if all-in-one config and anonymous user has been specified
    19 if hasattr(config, 'anonymous_user'):
    35 if hasattr(config, 'anonymous_user'):
    20     anonlogin, anonpwd = config.anonymous_user()
    36     anonlogin, anonpwd = config.anonymous_user()
    21     if anonlogin:
    37     if anonlogin:
    24             {'login': unicode(anonlogin), 'pwd': anonpwd})
    40             {'login': unicode(anonlogin), 'pwd': anonpwd})
    25 
    41 
    26 # need this since we already have at least one user in the database (the default admin)
    42 # need this since we already have at least one user in the database (the default admin)
    27 for user in rql('Any X WHERE X is CWUser').entities():
    43 for user in rql('Any X WHERE X is CWUser').entities():
    28     session.unsafe_execute('SET X in_state S WHERE X eid %(x)s, S eid %(s)s',
    44     session.unsafe_execute('SET X in_state S WHERE X eid %(x)s, S eid %(s)s',
    29                            {'x': user.eid, 's': activatedeid}, 'x')
    45                            {'x': user.eid, 's': activated.eid}, 'x')
    30 
    46 
    31 cfg = config.persistent_options_configuration()
    47 # on interactive mode, ask for level 0 persistent options
    32 if interactive_mode:
    48 if interactive_mode:
       
    49     cfg = config.persistent_options_configuration()
    33     cfg.input_config(inputlevel=0)
    50     cfg.input_config(inputlevel=0)
    34 
    51     for section, options in cfg.options_by_section():
    35 for section, options in cfg.options_by_section():
    52         for optname, optdict, value in options:
    36     for optname, optdict, value in options:
    53             key = '%s.%s' % (section, optname)
    37         key = '%s.%s' % (section, optname)
    54             default = cfg.option_default(optname, optdict)
    38         default = cfg.option_default(optname, optdict)
    55             # only record values differing from default
    39         # only record values differing from default
    56             if value != default:
    40         if value != default:
    57                 rql('INSERT CWProperty X: X pkey %(k)s, X value %(v)s', {'k': key, 'v': value})
    41             rql('INSERT CWProperty X: X pkey %(k)s, X value %(v)s', {'k': key, 'v': value})
       
    42 
    58 
    43 # add PERM_USE_TEMPLATE_FORMAT permission
    59 # add PERM_USE_TEMPLATE_FORMAT permission
    44 from cubicweb.schema import PERM_USE_TEMPLATE_FORMAT
    60 from cubicweb.schema import PERM_USE_TEMPLATE_FORMAT
    45 eid = add_entity('CWPermission', name=PERM_USE_TEMPLATE_FORMAT,
    61 usetmplperm = create_entity('CWPermission', name=PERM_USE_TEMPLATE_FORMAT,
    46                  label=_('use template languages'))
    62                             label=_('use template languages'))
    47 rql('SET X require_group G WHERE G name "managers", X eid %(x)s',
    63 rql('SET X require_group G WHERE G name "managers", X eid %(x)s',
    48     {'x': eid}, 'x')
    64     {'x': usetmplperm.eid}, 'x')