source: products/qPloneSkinDump/trunk/skin_template/setuphandlers.py @ 2111

Last change on this file since 2111 was 74, checked in by chervol, 15 years ago

initial plone2.0.5 branch import

  • Property svn:eol-style set to native
File size: 6.3 KB
Line 
1import sys
2from logging import getLogger
3
4from BTrees.OOBTree import OOBTree
5from persistent.list import PersistentList
6
7from zope.dottedname.resolve import resolve
8from zope.component import getUtility, getSiteManager, getMultiAdapter
9
10from plone.portlets.interfaces import IPortletAssignmentMapping, IPortletManager, IPlacelessPortletManager
11from plone.portlets.interfaces import IPortletContext, IPortletDataProvider
12from plone.portlets.interfaces import ILocalPortletAssignmentManager
13from plone.portlets.storage import PortletCategoryMapping, PortletAssignmentMapping
14from plone.app.portlets import portlets
15
16from plone.app.customerize.registration import *
17
18from Products.CMFCore.utils import getToolByName
19from Products.GenericSetup.utils import _getDottedName, _resolveDottedName
20
21from Products.%(product_name)s.config import *
22from Products.%(product_name)s.utils import *
23from Products.%(product_name)s.utilsPortlets import dumpPortlets
24
25logger = getLogger('%(product_name)s')
26
27def assignPortlet(mapping, info):
28    name = info['name']
29    try:
30        assignment = _resolveDottedName(info['class'])
31    except:
32        e,v,tr = sys.exc_info()
33        logger.info('Debug from setup portlets handler: %%s: %%s (%%s)' , info['class'], str(v), str(e))
34        return False
35    else:
36        portlet = assignment(**info['kwargs'])
37        if mapping.get(name, None) is not None:
38            del mapping[name]
39        mapping[name] = portlet
40        return portlet
41
42def setupSiteWidePortlets(site, data, managers):
43
44    for name, info in data:
45        manager = managers.get(name, None)
46        if manager is not None:
47            for category, keys in info.items():
48                catmapping = manager.get(category, None)
49                if catmapping is None:
50                    catmapping = manager[category] = PortletCategoryMapping()
51                for key, assignments in keys.items():
52                    mapping = catmapping.get(key, None)
53                    if mapping is None:
54                        mapping = catmapping[key] = PortletAssignmentMapping()
55                    for assignment in assignments.values():
56                        assignPortlet(mapping, assignment)
57
58def setupPortletsForContext(context, data, managers):
59
60    for name, info in data:
61        manager = managers.get(name, None)
62        if manager is not None:
63            # set portlet assignments
64            mapping = getMultiAdapter((context, manager), IPortletAssignmentMapping, context=context)
65
66            # purge mapping
67            mapping._data = OOBTree()
68            mapping._order = PersistentList()
69
70            if info['assignments']:
71                # set portlet assignments
72                for assignment in info['assignments']:
73                    assignPortlet(mapping, assignment)
74            if info['blacklists']:
75                # set blacklists
76                localassignmentmanager = getMultiAdapter((context, manager), ILocalPortletAssignmentManager, context=context)
77                for category, status in info['blacklists']:
78                    localassignmentmanager.setBlacklistStatus(category, status)
79
80
81def saveBeforeInstallPortlets(context):
82    # Save information about portlets before installation
83
84    beforeInstall = dumpPortlets(context=context, dump_policy='root', dump_portlets_selection=[] )
85
86    # update properties list
87    pp = getToolByName(context, 'portal_properties')
88    skin_props = pp._getOb('%(product_name_lowercase)s_properties')
89    skin_props._updateProperty('portlets_before_install', str(beforeInstall) )
90
91
92def importPortlets(context):
93
94    if context.readDataFile("%(afterinstall_profile_marker)s") is None:
95        return
96
97    site = context.getSite()
98
99    saveBeforeInstallPortlets(site)
100
101    components = getSiteManager(site)
102    ms = [r for r in components.registeredUtilities() if r.provided.isOrExtends(IPortletManager)]
103    context_managers = {}
104    for m in ms:
105        if not m.provided.isOrExtends(IPlacelessPortletManager):
106            context_managers[m.name] = getUtility(IPortletManager, name=m.name, context=site)
107
108    managers = {}
109    for m in ms:
110        managers[m.name] = getUtility(IPortletManager, name=m.name, context=site)
111
112    for path, info in SLOT_STRUCTURE:
113        if path == '__site-wide-portlets__':
114            setupSiteWidePortlets(site, info, managers)
115        else:
116            try:
117                obj = site.restrictedTraverse(path)
118            except:
119                e,v,tr = sys.exc_info()
120                logger.info('Debug from importPortlets handler: %%s: %%s (%%s)' , path, str(v), str(e))
121            else:
122                setupPortletsForContext(obj, info, context_managers)
123
124def importZexps(context):
125
126    if context.readDataFile("%(afterinstall_profile_marker)s") is None:
127        return
128
129    site = context.getSite()
130    if checkIfImport():
131        performImportToPortal(site)
132
133def importVarious(context):
134
135    if context.readDataFile("%(afterinstall_profile_marker)s") is None:
136        return
137
138    site = context.getSite()
139    out = StringIO()
140    if FINAL_CUSTOMIZATION_FUNCTIONS:
141        dummy = [func(site, out) for func in FINAL_CUSTOMIZATION_FUNCTIONS]
142
143def importCustomViews(context):
144
145    if context.readDataFile("%(afterinstall_profile_marker)s") is None:
146        return
147
148    site = context.getSite()
149
150    for view in CUSTOM_VIEWS:
151        for_name = view['for_name']
152        view_name = view['view_name']
153        type_name = view['type_name']
154        kwargs = view['kwargs']
155
156        reg = findTemplateViewRegistration(for_name, type_name, view_name)
157        viewzpt_id = str(generateIdFromRegistration(reg))
158
159        container = getUtility(IViewTemplateContainer)
160        if viewzpt_id in container.objectIds():
161            continue
162
163        attr, pt = findViewletTemplate(reg.factory)
164        if pt:
165            ptname = basename(pt.filename)
166        else:
167            ptname = None
168        viewzpt = TTWViewTemplate(
169            id = viewzpt_id,
170            text = kwargs['text'],
171            encoding = kwargs['encoding'],
172            content_type = kwargs['content_type'],
173            view = getViewClassFromRegistration(reg),
174            permission = getViewPermissionFromRegistration(reg),
175            name = ptname)
176        container.addTemplate(viewzpt_id, viewzpt)
177
178        sm = getSiteManager(site)
179        sm.registerAdapter(viewzpt, required = reg.required, provided = reg.provided, name = reg.name)
Note: See TracBrowser for help on using the repository browser.