misc/migration/postcreate.py
branchstable
changeset 3315 59220b704562
parent 3005 a50d03e7014f
child 3822 b61c7b065a66
equal deleted inserted replaced
3298:caef98aa4a98 3315:59220b704562
     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,
       
    16                requiredgroups=('managers',))
       
    17 
    15 
    18 # need this since we already have at least one user in the database (the default admin)
    16 # some entities have been added before schema entities, fix the 'is' and
    19 rql('SET X in_state S WHERE X is CWUser, S eid %s' % activatedeid)
    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',))
    20 
    33 
    21 # 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
    22 if hasattr(config, 'anonymous_user'):
    35 if hasattr(config, 'anonymous_user'):
    23     anonlogin, anonpwd = config.anonymous_user()
    36     anonlogin, anonpwd = config.anonymous_user()
    24     if anonlogin:
    37     if anonlogin:
    25         rql('INSERT CWUser X: X login %(login)s, X upassword %(pwd)s,'
    38         rql('INSERT CWUser X: X login %(login)s, X upassword %(pwd)s,'
    26             'X in_state S, X in_group G WHERE G name "guests", S name "activated"',
    39             'X in_group G WHERE G name "guests"',
    27             {'login': unicode(anonlogin), 'pwd': anonpwd})
    40             {'login': unicode(anonlogin), 'pwd': anonpwd})
    28 
    41 
    29 cfg = config.persistent_options_configuration()
    42 # need this since we already have at least one user in the database (the default admin)
       
    43 for user in rql('Any X WHERE X is CWUser').entities():
       
    44     session.unsafe_execute('SET X in_state S WHERE X eid %(x)s, S eid %(s)s',
       
    45                            {'x': user.eid, 's': activated.eid}, 'x')
       
    46 
       
    47 # on interactive mode, ask for level 0 persistent options
    30 if interactive_mode:
    48 if interactive_mode:
       
    49     cfg = config.persistent_options_configuration()
    31     cfg.input_config(inputlevel=0)
    50     cfg.input_config(inputlevel=0)
    32 
    51     for section, options in cfg.options_by_section():
    33 for section, options in cfg.options_by_section():
    52         for optname, optdict, value in options:
    34     for optname, optdict, value in options:
    53             key = '%s.%s' % (section, optname)
    35         key = '%s.%s' % (section, optname)
    54             default = cfg.option_default(optname, optdict)
    36         default = cfg.option_default(optname, optdict)
    55             # only record values differing from default
    37         # only record values differing from default
    56             if value != default:
    38         if value != default:
    57                 rql('INSERT CWProperty X: X pkey %(k)s, X value %(v)s', {'k': key, 'v': value})
    39             rql('INSERT CWProperty X: X pkey %(k)s, X value %(v)s', {'k': key, 'v': value})
       
    40 
    58 
    41 # add PERM_USE_TEMPLATE_FORMAT permission
    59 # add PERM_USE_TEMPLATE_FORMAT permission
    42 from cubicweb.schema import PERM_USE_TEMPLATE_FORMAT
    60 from cubicweb.schema import PERM_USE_TEMPLATE_FORMAT
    43 eid = add_entity('CWPermission', name=PERM_USE_TEMPLATE_FORMAT,
    61 usetmplperm = create_entity('CWPermission', name=PERM_USE_TEMPLATE_FORMAT,
    44                  label=_('use template languages'))
    62                             label=_('use template languages'))
    45 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',
    46     {'x': eid}, 'x')
    64     {'x': usetmplperm.eid}, 'x')