products/CPSUid

changeset 19:b619c4219090

add upgrade step for uid counter changes
author atchertchian
date Fri, 11 Aug 2006 10:41:30 +0000
parents cc4e1748df28
children 469391ff4449
files CHANGES configure.zcml upgrade.py
diffstat 3 files changed, 70 insertions(+), 2 deletions(-) [+]
line diff
     1.1 --- a/CHANGES
     1.2 +++ b/CHANGES
     1.3 @@ -1,6 +1,6 @@
     1.4  Requires:
     1.5  ~~~~~~~~~
     1.6 -- Deleting existing counters.
     1.7 +- Run uid counters upgrade step.
     1.8  New features:
     1.9  ~~~~~~~~~~~~~
    1.10  -
     2.1 --- a/configure.zcml
     2.2 +++ b/configure.zcml
     2.3 @@ -1,6 +1,7 @@
     2.4  <configure
     2.5      xmlns="http://namespaces.zope.org/zope"
     2.6 -    xmlns:five="http://namespaces.zope.org/five">
     2.7 +    xmlns:five="http://namespaces.zope.org/five"
     2.8 +    xmlns:cps="http://namespaces.nuxeo.org/cps">
     2.9  
    2.10    <adapter
    2.11        factory=".exportimport.UidToolXMLAdapter"
    2.12 @@ -16,4 +17,10 @@
    2.13             Products.GenericSetup.interfaces.ISetupEnviron"
    2.14        />
    2.15  
    2.16 +  <cps:upgradeStep
    2.17 +      title="Upgrade CPSUid counters"
    2.18 +      handler=".upgrade.upgradeUidCounters"
    2.19 +      checker=".upgrade.checkUidCounters"
    2.20 +      />
    2.21 +
    2.22  </configure>
     3.1 new file mode 100644
     3.2 --- /dev/null
     3.3 +++ b/upgrade.py
     3.4 @@ -0,0 +1,61 @@
     3.5 +# (C) Copyright 2006 Nuxeo SAS <http://nuxeo.com>
     3.6 +# Authors:
     3.7 +# - Anahide Tchertchian <at@nuxeo.com>
     3.8 +#
     3.9 +# This program is free software; you can redistribute it and/or modify
    3.10 +# it under the terms of the GNU General Public License version 2 as published
    3.11 +# by the Free Software Foundation.
    3.12 +#
    3.13 +# This program is distributed in the hope that it will be useful,
    3.14 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
    3.15 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    3.16 +# GNU General Public License for more details.
    3.17 +#
    3.18 +# You should have received a copy of the GNU General Public License
    3.19 +# along with this program; if not, write to the Free Software
    3.20 +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
    3.21 +# 02111-1307, USA.
    3.22 +#
    3.23 +"""Upgrades
    3.24 +
    3.25 +$Id$
    3.26 +"""
    3.27 +
    3.28 +from logging import getLogger
    3.29 +from Products.CMFCore.utils import getToolByName
    3.30 +
    3.31 +def checkUidCounters(self):
    3.32 +    upgrade = 0
    3.33 +    uidtool = getToolByName(self, 'portal_uid', None)
    3.34 +    if uidtool is not None:
    3.35 +        for generator in uidtool.objectValues('Uid Generator'):
    3.36 +            for counter in generator.objectValues('Uid Counter'):
    3.37 +                if isinstance(counter.criteria, dict):
    3.38 +                    upgrade = 1
    3.39 +                    break
    3.40 +    return upgrade
    3.41 +
    3.42 +
    3.43 +def upgradeUidCounters(self):
    3.44 +    """Upgrade uid counters, turning their criteria property into lines
    3.45 +    """
    3.46 +    logger = getLogger('upgradeUidCounters')
    3.47 +    nchanged = 0
    3.48 +    uidtool = getToolByName(self, 'portal_uid', None)
    3.49 +    if uidtool is not None:
    3.50 +        for generator in uidtool.objectValues('Uid Generator'):
    3.51 +            for counter in generator.objectValues('Uid Counter'):
    3.52 +                criteria = counter.criteria
    3.53 +                if isinstance(criteria, dict):
    3.54 +                    logger.debug("upgrading %s" % counter.absolute_url())
    3.55 +                    criteria_lines = list(['%s %s'%(key, value)
    3.56 +                                           for key, value in criteria.items()])
    3.57 +                    counter._setPropValue('criteria', criteria_lines)
    3.58 +                    nchanged += 1
    3.59 +    logger.debug("%s objects upgraded" % nchanged)
    3.60 +    return '%s objects upgraded' % nchanged
    3.61 +
    3.62 +
    3.63 +def upgrade(self):
    3.64 +    if checkRootWorkflows(self):
    3.65 +        upgradeRootWorkflows(self)