File: migrate.py

package info (click to toggle)
zope-cmfdynamicviewfti 2.1-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 172 kB
  • ctags: 136
  • sloc: python: 719; xml: 57; makefile: 31; sh: 2
file content (99 lines) | stat: -rw-r--r-- 3,547 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
##############################################################################
#
# CMFDynamicViewFTI
# Copyright (c) 2005 Plone Foundation. All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
# Authors:  Martin Aspeli
#           Christian Heimes
#
##############################################################################
"""Migration functions
"""
from Acquisition import aq_base

from Products.CMFCore.TypesTool import FactoryTypeInformation
from Products.CMFCore.utils import getToolByName
from Products.CMFDynamicViewFTI.interfaces import IDynamicViewTypeInformation
from Products.CMFDynamicViewFTI.fti import DynamicViewTypeInformation
fti_meta_type = DynamicViewTypeInformation.meta_type

def migrateFTI(portal, id, ti_name, fti_meta_type):
    """Migrates a single FTI to DynamicViewFTI

    portal - context (portal root)
    id - id of the type information
    ti_name - name of the type information
    fti_meta_type - name of the fti type
    """
    ttool = getToolByName(portal, 'portal_types')
    ti = ttool[id]

    # copy all data like actions and properties
    actions = []
    for action in getattr(ti, '_actions', ()):
        actions.append(action._getCopy(action))
    actions = tuple(actions)
    properties = dict(ti.propertyItems())

    # delete the old fti and create a new one
    ttool._delObject(id)

    ttool.manage_addTypeInformation(fti_meta_type,
                                    id=id,
                                    typeinfo_name=ti_name)

    # assign actions and properties from the old fti
    # NOTE: aliases are ignored and security settings are not copied
    new_ti = ttool[id]
    new_ti._actions = actions
    new_ti.manage_changeProperties(**properties)


def migrateFTIs(portal, product=None, fti_meta_type=fti_meta_type):
    """Migrates all FTIs in portal types

    migrateFTIs checks all FTIs if they have to be migrated. The product argument
    can be used to restrict migration to a single product
    """
    ttool = getToolByName(portal, 'portal_types')
    migrated = []

    # create a list of type informations that might need migration
    # (product, meta_type) -> type info name
    ftis = {}
    for name, ti in ttool.listDefaultTypeInformation():
        ti_product = ti.get('product', None)
        ti_mt = ti.get('meta_type', None)
        if product and ti_product != product:
            continue
        if ti.get('fti_meta_type') != fti_meta_type:
            continue
        ftis[(ti_product, ti_mt)] = name

    # check all FTIs in portal_types
    for obj in ttool.objectValues():
        if obj.meta_type == fti_meta_type:
            continue # already migrated

        ti_product = getattr(aq_base(obj), 'product', None)
        ti_mt = getattr(aq_base(obj), 'content_meta_type', None)
        if not ti_product or not ti_mt:
            continue # strange/broken ti object or no FTI

        ti_name = ftis.get((ti_product, ti_mt), None)
        if ti_name is None:
            continue # not in list of FTIs to be migrated

        # match - migrated FTI
        id = obj.getId()
        migrateFTI(portal, id, ti_name, fti_meta_type)
        migrated.append(id)

    return migrated