source: products/quintagroup.plonegooglesitemaps/branches/sitemap_date/quintagroup/plonegooglesitemaps/setuphandlers.py @ 3565

Last change on this file since 3565 was 3501, checked in by potar, 7 years ago

fixed pep8

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