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

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

fixes pyflakes and pylint

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