source: products/quintagroup.plonegooglesitemaps/trunk/quintagroup/plonegooglesitemaps/setuphandlers.py @ 3152

Last change on this file since 3152 was 3152, checked in by zidane, 9 years ago

fixes pep8

  • Property svn:eol-style set to native
File size: 5.2 KB
Line 
1import sys
2import logging
3from zope.component import getSiteManager
4from zope.component import getGlobalSiteManager
5
6from Acquisition import aq_parent
7from Products.CMFCore.utils import getToolByName
8
9from config import SUPPORT_BLAYER
10from quintagroup.plonegooglesitemaps.content.newsextender import NewsExtender
11
12logger = logging.getLogger('quintagroup.plonegooglesitemaps')
13
14
15def unregisterSchemaExtenderAdapters(site):
16    """ Unregister news schema extender adapters
17        from local component registry.
18    """
19    lsm = getSiteManager(site)
20    gsm = getGlobalSiteManager()
21    if lsm == gsm:
22        logger.warning("Not found local component registry")
23        return
24
25    unregistered = []
26    registrations = tuple(lsm.registeredAdapters())
27    for registration in registrations:
28        factory = registration.factory
29        if factory == NewsExtender:
30            required = registration.required
31            provided = registration.provided
32            name = registration.name
33            lsm.unregisterAdapter(factory=factory,
34                required=required, provided=provided, name=name)
35            unregistered.append(str(required))
36    logger.info("Unregistered news schema extender adapters for: %s" \
37                % unregistered)
38
39
40def removeConfiglet(site):
41    """ Remove configlet.
42    """
43    conf_id = 'GoogleSitemaps'
44    controlpanel_tool = getToolByName(site, 'portal_controlpanel')
45    if controlpanel_tool:
46        controlpanel_tool.unregisterConfiglet(conf_id)
47        logger.log(logging.INFO, "Unregistered \"%s\" configlet." % conf_id)
48
49
50def removeBrowserLayer(site):
51    """ Remove browser layer.
52    """
53    if not SUPPORT_BLAYER:
54        return
55
56    from plone.browserlayer.utils import unregister_layer
57    from plone.browserlayer.interfaces import ILocalBrowserLayerType
58
59    name = "quintagroup.plonegooglesitemaps"
60    site = getSiteManager(site)
61    registeredLayers = [r.name for r in site.registeredUtilities()
62                        if r.provided == ILocalBrowserLayerType]
63    if name in registeredLayers:
64        unregister_layer(name, site_manager=site)
65        logger.log(logging.INFO, "Unregistered \"%s\" browser layer." % name)
66
67
68def uninstall(context):
69    """ Do customized uninstallation.
70    """
71    if context.readDataFile('gsm_uninstall.txt') is None:
72        return
73    site = context.getSite()
74    unregisterSchemaExtenderAdapters(site)
75    removeConfiglet(site)
76    removeBrowserLayer(site)
77
78
79def cleanup(site):
80    """Clean-up qPloneGoogleSitemaps artefacts."""
81    old_product = "qPloneGoogleSitemaps"
82    # Get plone tools
83    pp = getToolByName(site, 'portal_properties')
84    skins = getToolByName(site, 'portal_skins')
85    controlpanel = getToolByName(site, 'portal_controlpanel')
86    # Remove old configlet from controlpanel
87    configlet_ids = [ai['id'] for ai in controlpanel.listActionInfos()]
88    if old_product in configlet_ids:
89        controlpanel.unregisterConfiglet(old_product)
90        logger.info("Unregistered '%s' configlet from "\
91                    "portal_controlpanel tool." % old_product)
92    # Remove qPloneGoogleSitemaps skin layer
93    for skinName in skins.getSkinSelections():
94        skin_paths = skins.getSkinPath(skinName).split(',')
95        paths = [l.strip() for l in skin_paths if not l == old_product]
96        if len(paths) < len(skin_paths):
97            logger.info("Removed '%s' from '%s' skin." \
98                        % (old_product, skinName))
99        skins.addSkinSelection(skinName, ','.join(paths))
100
101
102def recriateSitemaps(smaps):
103    msg = "Recriation Sitemaps: "
104    if smaps:
105        logger.info(msg + "Process %s sitemaps." % (
106                [sm.getPath() for sm in smaps]))
107        fields = ['id', 'sitemapType', 'portalTypes', 'states',
108                  'blackout_list', 'reg_exp', 'urls', 'pingTransitions']
109        for smb in smaps:
110            # get sitemap properties
111            sm_path = smb.getPath()
112            sm = smb.getObject()
113            container = aq_parent(sm)
114            data = {}
115            for fn in fields:
116                data[fn] = sm.getField(fn).getAccessor(sm)()
117            # Replace old GoogleSitemap by new one with
118            # previous properties
119            container.manage_delObjects(data['id'])
120            container.invokeFactory("Sitemap", id=data['id'])
121            new_sm = getattr(container, data['id'])
122            new_sm.update(**data)
123            new_sm.at_post_create_script()
124            logger.info("Successfully replaced '%s' Sitemap" % sm_path)
125
126
127def getOldGSitemaps(site):
128    catalog = getToolByName(site, 'portal_catalog')
129    smaps = catalog(portal_type="Sitemap")
130    old_smb = [smb for smb in smaps \
131               if 'qPloneGoogleSitemaps' in str(smb.getObject().__class__)]
132    return old_smb
133
134
135def migrate_qPGSM(context):
136    """ Clean-up qPloneGoogleSitemaps product artefacts and
137        recriate sitemaps from quintagroup.plonegooglesitemaps
138        package version 1.0.
139    """
140    if context.readDataFile('gsm_migration.txt') is None:
141        return
142
143    site = context.getSite()
144    cleanup(site)
145    old_gsmaps = getOldGSitemaps(site)
146    if old_gsmaps:
147        recriateSitemaps(old_gsmaps)
148        logger.info("Successfully migrated old GoogleSitemaps.")
149    else:
150        logger.info("No old GoogleSitemaps found.")
Note: See TracBrowser for help on using the repository browser.