1 | import logging |
---|
2 | from zope.component import queryMultiAdapter |
---|
3 | |
---|
4 | from quintagroup.canonicalpath.interfaces import ICanonicalLink |
---|
5 | from quintagroup.canonicalpath.adapters import PROPERTY_LINK |
---|
6 | |
---|
7 | logger = logging.getLogger('quintagroup.seoptimizer') |
---|
8 | FIX_PTYPES_DOMAIN = ['Document', 'File', 'News Item'] |
---|
9 | REMOVE_SEOPROPERTIES = ['additional_keywords', |
---|
10 | 'settings_use_keywords_sg', |
---|
11 | 'settings_use_keywords_lg', |
---|
12 | 'filter_keywords_by_content', |
---|
13 | ] |
---|
14 | |
---|
15 | |
---|
16 | def changeDomain(plone_tools): |
---|
17 | """ Fix i18n_domain bug for some portal_types, |
---|
18 | which present in earlier versions of the package. |
---|
19 | """ |
---|
20 | types_tool = plone_tools.types() |
---|
21 | for ptype in [ptypes for ptypes in types_tool.objectValues() |
---|
22 | if ptypes.id in FIX_PTYPES_DOMAIN]: |
---|
23 | if ptype.i18n_domain == 'quintagroup.seoptimizer': |
---|
24 | ptype.i18n_domain = 'plone' |
---|
25 | logger.log(logging.INFO, "I18n Domain of the type \'%s\' " |
---|
26 | "changed to \'plone\'." % ptype.id) |
---|
27 | |
---|
28 | |
---|
29 | def changeMetatagsOrderList(plone_tools): |
---|
30 | """ Change format metatags order list |
---|
31 | from "metaname accessor" to "metaname". |
---|
32 | """ |
---|
33 | plone_tools.types() |
---|
34 | setup_tool = plone_tools.url().getPortalObject().portal_setup |
---|
35 | seoprops_tool = plone_tools.properties().seo_properties |
---|
36 | if seoprops_tool.hasProperty('metatags_order'): |
---|
37 | mto = seoprops_tool.getProperty('metatags_order', []) |
---|
38 | mto_new = [line.split(' ')[0].strip() for line in mto] |
---|
39 | if not list(mto) == mto_new: |
---|
40 | logger.log(logging.INFO, "Changed format metatags order list in " |
---|
41 | "configlet from \"metaname accessor\" to \"metaname\".") |
---|
42 | seoprops_tool.manage_changeProperties(metatags_order=mto_new) |
---|
43 | else: |
---|
44 | name_profile = 'profile-quintagroup.seoptimizer:default' |
---|
45 | setup_tool.runImportStepFromProfile(name_profile, 'propertiestool') |
---|
46 | |
---|
47 | |
---|
48 | def migrationActions(plone_tools): |
---|
49 | """ Migration actions from portal_types action to seoproperties tool |
---|
50 | (for seoaction in portal_actions). |
---|
51 | """ |
---|
52 | types_tool = plone_tools.types() |
---|
53 | seoprops_tool = plone_tools.properties().seo_properties |
---|
54 | property_name = 'content_types_with_seoproperties' |
---|
55 | ctws = list(seoprops_tool.getProperty(property_name, [])) |
---|
56 | flag = False |
---|
57 | for ptype in types_tool.objectValues(): |
---|
58 | idxs = [idx_act[0] for idx_act in enumerate(ptype.listActions()) |
---|
59 | if idx_act[1].id == 'seo_properties'] |
---|
60 | if idxs: |
---|
61 | if ptype.id not in ctws: |
---|
62 | ctws.append(ptype.id) |
---|
63 | flag = True |
---|
64 | ptype.deleteActions(idxs) |
---|
65 | logger.log(logging.INFO, "Moved \"SEO Properties\" action from %s " |
---|
66 | "type in portal actions." % ptype.id) |
---|
67 | if flag: |
---|
68 | seo_change_properties = seoprops_tool.manage_changeProperties |
---|
69 | seo_change_properties(content_types_with_seoproperties=ctws) |
---|
70 | |
---|
71 | |
---|
72 | def removeNonUseSeoProperties(plone_tools): |
---|
73 | """ Revome properties used in earlier versions of the package |
---|
74 | """ |
---|
75 | seoprops_tool = plone_tools.properties().seo_properties |
---|
76 | remove_properties = [] |
---|
77 | for pr in REMOVE_SEOPROPERTIES: |
---|
78 | if seoprops_tool.hasProperty(pr): |
---|
79 | remove_properties.append(pr) |
---|
80 | logger.log(logging.INFO, "Removed %s property in " |
---|
81 | "seoproperties tool." % pr) |
---|
82 | if not remove_properties: |
---|
83 | seoprops_tool.manage_delProperties(remove_properties) |
---|
84 | |
---|
85 | |
---|
86 | def removeSkin(plone_tools): |
---|
87 | """ Remove skin layers. |
---|
88 | """ |
---|
89 | layer = 'quintagroup.seoptimizer' |
---|
90 | skins_tool = plone_tools.url().getPortalObject().portal_skins |
---|
91 | for skinName in skins_tool.getSkinSelections(): |
---|
92 | skin_paths = skins_tool.getSkinPath(skinName).split(',') |
---|
93 | paths = [l.strip() for l in skin_paths \ |
---|
94 | if not (l == layer or l.startswith(layer + '/'))] |
---|
95 | logger.log(logging.INFO, "Removed layers from %s skin." % skinName) |
---|
96 | skins_tool.addSkinSelection(skinName, ','.join(paths)) |
---|
97 | |
---|
98 | |
---|
99 | def migrateCanonical(plone_tools): |
---|
100 | """ Rename qSEO_canonical property into PROPERTY_LINK |
---|
101 | for all portal objects, which use SEO |
---|
102 | """ |
---|
103 | types = plone_tools.types() |
---|
104 | portal = plone_tools.url().getPortalObject() |
---|
105 | allCTTypes = types.listContentTypes() |
---|
106 | obj_metatypes = [m.content_meta_type for m in types.objectValues() \ |
---|
107 | if m.getId() in allCTTypes] |
---|
108 | portal.ZopeFindAndApply( |
---|
109 | portal, |
---|
110 | obj_metatypes=','.join(obj_metatypes), |
---|
111 | apply_func=renameProperty |
---|
112 | ) |
---|
113 | |
---|
114 | |
---|
115 | def renameProperty(obj, path): |
---|
116 | """ Rename qSEO_canonical property into PROPERTY_LINK |
---|
117 | for obj, which use SEO |
---|
118 | """ |
---|
119 | if obj.hasProperty('qSEO_canonical'): |
---|
120 | value = obj.getProperty('qSEO_canonical') |
---|
121 | |
---|
122 | level, msg = logging.INFO, "For %(url)s object 'qSEO_canonical' "\ |
---|
123 | "property renamed to '%(name)s'." |
---|
124 | try: |
---|
125 | ICanonicalLink(obj).canonical_link = value |
---|
126 | except Exception, e: |
---|
127 | level, msg = logging.ERROR, "%s on renaming 'qSEO_canonical' " \ |
---|
128 | "property for %%(url)s object" % str(e) |
---|
129 | |
---|
130 | logger.log(level, msg % {'url': obj.absolute_url(), |
---|
131 | 'name': PROPERTY_LINK}) |
---|
132 | obj.manage_delProperties(['qSEO_canonical']) |
---|
133 | |
---|
134 | |
---|
135 | def upgrade_2_to_3(setuptool): |
---|
136 | """ Upgrade quintagroup.seoptimizer from version 2.x.x to 3.0.0. |
---|
137 | """ |
---|
138 | plone_tools = queryMultiAdapter((setuptool, setuptool.REQUEST), |
---|
139 | name="plone_tools") |
---|
140 | profile_name = 'profile-quintagroup.seoptimizer:upgrade_2_to_3' |
---|
141 | setuptool.runAllImportStepsFromProfile(profile_name) |
---|
142 | migrationActions(plone_tools) |
---|
143 | changeDomain(plone_tools) |
---|
144 | changeMetatagsOrderList(plone_tools) |
---|
145 | removeNonUseSeoProperties(plone_tools) |
---|
146 | removeSkin(plone_tools) |
---|
147 | migrateCanonical(plone_tools) |
---|