1 | import logging |
---|
2 | from Products.CMFCore.utils import getToolByName |
---|
3 | from Products.CMFCore.Expression import Expression |
---|
4 | |
---|
5 | logger = logging.getLogger('quintagroup.seoptimizer') |
---|
6 | FIX_PTYPES_DOMAIN = ['Document', 'File', 'News Item'] |
---|
7 | |
---|
8 | def removeSkin(site, layer): |
---|
9 | """ Remove layers. |
---|
10 | """ |
---|
11 | skins_tool = getToolByName(site, 'portal_skins') |
---|
12 | for skinName in skins_tool.getSkinSelections(): |
---|
13 | skin_paths = skins_tool.getSkinPath(skinName).split(',') |
---|
14 | paths = [l.strip() for l in skin_paths if not (l == layer or l.startswith(layer+'/'))] |
---|
15 | logger.log(logging.INFO, "Removed layers from %s skin." % skinName) |
---|
16 | skins_tool.addSkinSelection(skinName, ','.join(paths)) |
---|
17 | |
---|
18 | def removeActions(site): |
---|
19 | """ Remove actions. |
---|
20 | """ |
---|
21 | types_tool = getToolByName(site, 'portal_types') |
---|
22 | for ptype in types_tool.objectValues(): |
---|
23 | idxs = [idx_act[0] for idx_act in enumerate(ptype.listActions()) if idx_act[1].id == 'seo_properties'] |
---|
24 | if idxs: |
---|
25 | ptype.deleteActions(idxs) |
---|
26 | logger.log(logging.INFO, "Deleted \"SEO Properties\" action for %s type." % ptype.id) |
---|
27 | |
---|
28 | def removeConfiglet(site, conf_id): |
---|
29 | """ Remove configlet. |
---|
30 | """ |
---|
31 | controlpanel_tool = getToolByName(site, 'portal_controlpanel') |
---|
32 | if controlpanel_tool: |
---|
33 | controlpanel_tool.unregisterConfiglet(conf_id) |
---|
34 | logger.log(logging.INFO, "Unregistered \"%s\" configlet." % conf_id) |
---|
35 | |
---|
36 | def changeDomain(site): |
---|
37 | """ Fixes old versions bug: Change of content type's domain to 'plone'. |
---|
38 | """ |
---|
39 | types_tool = getToolByName(site, 'portal_types') |
---|
40 | for ptype in [ptypes for ptypes in types_tool.objectValues() if ptypes.id in FIX_PTYPES_DOMAIN]: |
---|
41 | if ptype.i18n_domain == 'quintagroup.seoptimizer': |
---|
42 | ptype.i18n_domain = 'plone' |
---|
43 | logger.log(logging.INFO, "I18n Domain of the type \'%s\' changed to \'plone\'." % ptype.id) |
---|
44 | |
---|
45 | def changeMetatagsOrderList(site): |
---|
46 | """ Change format metatags order list from "metaname accessor" to "metaname". |
---|
47 | """ |
---|
48 | types_tool = getToolByName(site, 'portal_types') |
---|
49 | pprops_tool = getToolByName(site, 'portal_properties') |
---|
50 | seoprops_tool = getToolByName(pprops_tool, 'seo_properties') |
---|
51 | mto = seoprops_tool.getProperty('metatags_order', []) |
---|
52 | mto_new = [line.split(' ')[0].strip() for line in mto] |
---|
53 | if not list(mto) == mto_new: |
---|
54 | logger.log(logging.INFO, "Changed format metatags order list in configlet from \"metaname accessor\" to \"metaname\".") |
---|
55 | seoprops_tool.manage_changeProperties(metatags_order=mto_new) |
---|
56 | |
---|
57 | def migrationActions(site): |
---|
58 | """ Migration actions from portal_types action to portal_actions. |
---|
59 | """ |
---|
60 | types_tool = getToolByName(site, 'portal_types') |
---|
61 | pprops_tool = getToolByName(site, 'portal_properties') |
---|
62 | seoprops_tool = getToolByName(pprops_tool, 'seo_properties') |
---|
63 | ctws = list(seoprops_tool.getProperty('content_types_with_seoproperties', [])) |
---|
64 | |
---|
65 | for ptype in types_tool.objectValues(): |
---|
66 | idxs = [idx_act[0] for idx_act in enumerate(ptype.listActions()) if idx_act[1].id == 'seo_properties'] |
---|
67 | if idxs: |
---|
68 | if ptype.id not in ctws: |
---|
69 | ctws.append(ptype.id) |
---|
70 | ptype.deleteActions(idxs) |
---|
71 | logger.log(logging.INFO, "Moved \"SEO Properties\" action from %s type in portal actions." % ptype.id) |
---|
72 | seoprops_tool.manage_changeProperties(content_types_with_seoproperties=ctws) |
---|
73 | |
---|
74 | def importVarious(context): |
---|
75 | """ Do customized installation. |
---|
76 | """ |
---|
77 | if context.readDataFile('seo_install.txt') is None: |
---|
78 | return |
---|
79 | |
---|
80 | def reinstall(context): |
---|
81 | """ Do customized reinstallation. |
---|
82 | """ |
---|
83 | if context.readDataFile('seo_reinstall.txt') is None: |
---|
84 | return |
---|
85 | site = context.getSite() |
---|
86 | migrationActions(site) |
---|
87 | changeDomain(site) |
---|
88 | changeMetatagsOrderList(site) |
---|
89 | |
---|
90 | def uninstall(context): |
---|
91 | """ Do customized uninstallation. |
---|
92 | """ |
---|
93 | if context.readDataFile('seo_uninstall.txt') is None: |
---|
94 | return |
---|
95 | site = context.getSite() |
---|
96 | removeSkin(site, 'quintagroup.seoptimizer' ) |
---|
97 | removeActions(site) |
---|
98 | removeConfiglet(site, 'quintagroup.seoptimizer') |
---|