1 | from sets import Set |
---|
2 | from DateTime import DateTime |
---|
3 | from Acquisition import aq_inner |
---|
4 | from zope.component import queryAdapter |
---|
5 | from plone.memoize import view |
---|
6 | from plone.app.controlpanel.form import ControlPanelView |
---|
7 | |
---|
8 | from Products.Five.browser import BrowserView |
---|
9 | from Products.CMFCore.utils import getToolByName |
---|
10 | from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile |
---|
11 | from Products.CMFPlone import PloneMessageFactory as pmf |
---|
12 | |
---|
13 | from quintagroup.seoptimizer import SeoptimizerMessageFactory as _ |
---|
14 | from quintagroup.seoptimizer import interfaces |
---|
15 | |
---|
16 | SEPERATOR = '|' |
---|
17 | SEO_PREFIX = 'seo_' |
---|
18 | PROP_PREFIX = 'qSEO_' |
---|
19 | SUFFIX = '_override' |
---|
20 | PROP_CUSTOM_PREFIX = 'qSEO_custom_' |
---|
21 | |
---|
22 | class SEOContext( BrowserView ): |
---|
23 | """ This class contains methods that allows to edit html header meta tags. |
---|
24 | """ |
---|
25 | |
---|
26 | def __init__(self, *args, **kwargs): |
---|
27 | super(SEOContext, self).__init__(*args, **kwargs) |
---|
28 | self._seotags = self._getSEOTags() |
---|
29 | |
---|
30 | def __getitem__(self, key): |
---|
31 | return self._seotags.get(key, '') |
---|
32 | |
---|
33 | @view.memoize |
---|
34 | def _getSEOTags(self): |
---|
35 | seotags = { |
---|
36 | "seo_title": self.getSEOProperty( 'qSEO_title', accessor='Title' ), |
---|
37 | "seo_robots": self.getSEOProperty( 'qSEO_robots', default='ALL'), |
---|
38 | "seo_description": self.getSEOProperty( 'qSEO_description', accessor='Description' ), |
---|
39 | "seo_distribution": self.getSEOProperty( 'qSEO_distribution', default="Global"), |
---|
40 | "seo_customMetaTags": self.seo_customMetaTags(), |
---|
41 | "seo_globalWithoutLocalCustomMetaTags": self.seo_globalWithoutLocalCustomMetaTags(), |
---|
42 | "seo_localCustomMetaTags": self.seo_localCustomMetaTags(), |
---|
43 | "seo_globalCustomMetaTags": self.seo_globalCustomMetaTags(), |
---|
44 | "seo_html_comment": self.getSEOProperty( 'qSEO_html_comment', default='' ), |
---|
45 | "meta_keywords": self.meta_keywords(), |
---|
46 | "seo_keywords": self.seo_keywords(), |
---|
47 | "seo_canonical": self.seo_canonical(), |
---|
48 | # Add test properties |
---|
49 | "has_seo_title": self.context.hasProperty('qSEO_title'), |
---|
50 | "has_seo_robots": self.context.hasProperty('qSEO_robots'), |
---|
51 | "has_seo_description": self.context.hasProperty( 'qSEO_description'), |
---|
52 | "has_seo_distribution": self.context.hasProperty( 'qSEO_distribution'), |
---|
53 | "has_html_comment": self.context.hasProperty('qSEO_html_comment'), |
---|
54 | "has_seo_keywords": self.context.hasProperty('qSEO_keywords'), |
---|
55 | "has_seo_canonical": self.context.hasProperty('qSEO_canonical'), |
---|
56 | } |
---|
57 | seotags["seo_nonEmptylocalMetaTags"] = bool(seotags["seo_localCustomMetaTags"]) |
---|
58 | return seotags |
---|
59 | |
---|
60 | |
---|
61 | def getSEOProperty( self, property_name, accessor='', default=None ): |
---|
62 | """ Get value from seo property by property name. |
---|
63 | """ |
---|
64 | context = aq_inner(self.context) |
---|
65 | |
---|
66 | if context.hasProperty(property_name): |
---|
67 | return context.getProperty(property_name, default) |
---|
68 | |
---|
69 | if accessor: |
---|
70 | method = getattr(context, accessor, default) |
---|
71 | if not callable(method): |
---|
72 | return default |
---|
73 | |
---|
74 | # Catch AttributeErrors raised by some AT applications |
---|
75 | try: |
---|
76 | value = method() |
---|
77 | except AttributeError: |
---|
78 | value = default |
---|
79 | |
---|
80 | return value |
---|
81 | |
---|
82 | |
---|
83 | def isSEOTabVisibile(self): |
---|
84 | context = aq_inner(self.context) |
---|
85 | portal_properties = getToolByName(context, 'portal_properties') |
---|
86 | seo_properties = getToolByName(portal_properties, 'seo_properties') |
---|
87 | content_types_with_seoproperties = seo_properties.getProperty('content_types_with_seoproperties', '') |
---|
88 | return bool(self.context.portal_type in content_types_with_seoproperties) |
---|
89 | |
---|
90 | |
---|
91 | def seo_customMetaTags( self ): |
---|
92 | """ Returned seo custom metatags from default_custom_metatags property in seo_properties |
---|
93 | (global seo custom metatags) with update from seo custom metatags properties in context (local seo custom metatags). |
---|
94 | """ |
---|
95 | tags = self.seo_globalCustomMetaTags() |
---|
96 | loc = self.seo_localCustomMetaTags() |
---|
97 | names = [i['meta_name'] for i in tags] |
---|
98 | add_tags = [] |
---|
99 | for i in loc: |
---|
100 | if i['meta_name'] in names: |
---|
101 | for t in tags: |
---|
102 | if t['meta_name'] == i['meta_name']: |
---|
103 | t['meta_content'] = i['meta_content'] |
---|
104 | else: |
---|
105 | add_tags.append(i) |
---|
106 | tags.extend(add_tags) |
---|
107 | return tags |
---|
108 | |
---|
109 | def seo_globalWithoutLocalCustomMetaTags( self ): |
---|
110 | """ Returned seo custom metatags from default_custom_metatags property in seo_properties |
---|
111 | (global seo custom metatags) without seo custom metatags from properties in context (local seo custom metatags). |
---|
112 | """ |
---|
113 | glob = self.seo_globalCustomMetaTags() |
---|
114 | loc = self.seo_localCustomMetaTags() |
---|
115 | names = [i['meta_name'] for i in loc] |
---|
116 | tags = [] |
---|
117 | for i in glob: |
---|
118 | if i['meta_name'] not in names: |
---|
119 | tags.append(i) |
---|
120 | return tags |
---|
121 | |
---|
122 | def seo_localCustomMetaTags( self ): |
---|
123 | """ Returned seo custom metatags from properties in context (local seo custom metatags). |
---|
124 | """ |
---|
125 | result = [] |
---|
126 | property_prefix = 'qSEO_custom_' |
---|
127 | context = aq_inner(self.context) |
---|
128 | for property, value in context.propertyItems(): |
---|
129 | if property.startswith(property_prefix) and property[len(property_prefix):]: |
---|
130 | result.append({'meta_name' : property[len(property_prefix):], |
---|
131 | 'meta_content' : value}) |
---|
132 | return result |
---|
133 | |
---|
134 | def seo_globalCustomMetaTags( self ): |
---|
135 | """ Returned seo custom metatags from default_custom_metatags property in seo_properties. |
---|
136 | """ |
---|
137 | result = [] |
---|
138 | context = aq_inner(self.context) |
---|
139 | site_properties = getToolByName(context, 'portal_properties') |
---|
140 | if hasattr(site_properties, 'seo_properties'): |
---|
141 | custom_meta_tags = getattr(site_properties.seo_properties, 'default_custom_metatags', []) |
---|
142 | for tag in custom_meta_tags: |
---|
143 | name_value = tag.split(SEPERATOR) |
---|
144 | if name_value[0]: |
---|
145 | result.append({'meta_name' : name_value[0], |
---|
146 | 'meta_content' : len(name_value) == 2 and name_value[1] or ''}) |
---|
147 | return result |
---|
148 | |
---|
149 | def seo_html_comment( self ): |
---|
150 | """ Generate HTML Comments from SEO properties. |
---|
151 | """ |
---|
152 | html_comment = self.getSEOProperty( 'qSEO_html_comment' ) |
---|
153 | return html_comment and html_comment or '' |
---|
154 | |
---|
155 | def meta_keywords( self ): |
---|
156 | """ Generate Meta Keywords from SEO properties (global and local) with Subject, |
---|
157 | depending on the options in configlet. |
---|
158 | """ |
---|
159 | prop_name = 'qSEO_keywords' |
---|
160 | accessor = 'Subject' |
---|
161 | context = aq_inner(self.context) |
---|
162 | keywords = Set([]) |
---|
163 | pprops = getToolByName(context, 'portal_properties') |
---|
164 | sheet = getattr(pprops, 'seo_properties', None) |
---|
165 | method = getattr(context, accessor, None) |
---|
166 | if not callable(method): |
---|
167 | return None |
---|
168 | |
---|
169 | # Catch AttributeErrors raised by some AT applications |
---|
170 | try: |
---|
171 | subject = Set(method()) |
---|
172 | except AttributeError: |
---|
173 | subject = keywords |
---|
174 | |
---|
175 | if sheet: |
---|
176 | settings_use_keywords_sg = sheet.getProperty('settings_use_keywords_sg') |
---|
177 | settings_use_keywords_lg = sheet.getProperty('settings_use_keywords_lg') |
---|
178 | global_keywords = Set(sheet.getProperty('additional_keywords', None)) |
---|
179 | local_keywords = Set(context.getProperty(prop_name, None)) |
---|
180 | # Subject overrides global seo keywords and global overrides local seo keywords |
---|
181 | if [settings_use_keywords_sg, settings_use_keywords_lg] == [1, 1]: |
---|
182 | keywords = subject |
---|
183 | # Subject overrides global seo keywords and merge global and local seo keywords |
---|
184 | elif [settings_use_keywords_sg, settings_use_keywords_lg] == [1, 2]: |
---|
185 | keywords = subject | local_keywords |
---|
186 | # Global seo keywords overrides Subject and global overrides local seo keywords |
---|
187 | elif [settings_use_keywords_sg, settings_use_keywords_lg] == [2, 1]: |
---|
188 | keywords = global_keywords |
---|
189 | # Global seo keywords overrides Subject and merge global and local seo keywords |
---|
190 | elif [settings_use_keywords_sg, settings_use_keywords_lg] == [2, 2]: |
---|
191 | keywords = global_keywords | local_keywords |
---|
192 | # Merge Subject and global seo keywords and global overrides local seo keywords |
---|
193 | elif [settings_use_keywords_sg, settings_use_keywords_lg] == [3, 1]: |
---|
194 | keywords = subject | global_keywords |
---|
195 | # Merge Subject and global seo keywords and merge global and local seo keywords |
---|
196 | elif [settings_use_keywords_sg, settings_use_keywords_lg] == [3, 2]: |
---|
197 | keywords = subject | global_keywords | local_keywords |
---|
198 | else: |
---|
199 | keywords = subject |
---|
200 | else: |
---|
201 | keywords = subject |
---|
202 | |
---|
203 | return tuple(keywords) |
---|
204 | |
---|
205 | def seo_keywords( self ): |
---|
206 | """ Generate SEO Keywords from SEO properties (global merde local). |
---|
207 | """ |
---|
208 | prop_name = 'qSEO_keywords' |
---|
209 | context = aq_inner(self.context) |
---|
210 | keywords = Set([]) |
---|
211 | pprops = getToolByName(context, 'portal_properties') |
---|
212 | sheet = getattr(pprops, 'seo_properties', None) |
---|
213 | |
---|
214 | if sheet: |
---|
215 | settings_use_keywords_sg = sheet.getProperty('settings_use_keywords_sg') |
---|
216 | settings_use_keywords_lg = sheet.getProperty('settings_use_keywords_lg') |
---|
217 | global_keywords = Set(sheet.getProperty('additional_keywords', None)) |
---|
218 | local_keywords = Set(context.getProperty(prop_name, None)) |
---|
219 | keywords = global_keywords | local_keywords |
---|
220 | else: |
---|
221 | keywords = '' |
---|
222 | return tuple(keywords) |
---|
223 | |
---|
224 | def seo_canonical( self ): |
---|
225 | """ Generate canonical URL from SEO properties. |
---|
226 | """ |
---|
227 | purl = getToolByName(self.context, 'portal_url')() |
---|
228 | canpath = queryAdapter(self.context, interfaces.ISEOCanonicalPath) |
---|
229 | return purl + canpath.canonical_path() |
---|
230 | |
---|
231 | |
---|
232 | class SEOContextPropertiesView( BrowserView ): |
---|
233 | """ This class contains methods that allows to manage seo properties. |
---|
234 | """ |
---|
235 | template = ViewPageTemplateFile('templates/seo_context_properties.pt') |
---|
236 | |
---|
237 | def test( self, condition, first, second ): |
---|
238 | """ |
---|
239 | """ |
---|
240 | return condition and first or second |
---|
241 | |
---|
242 | def getMainDomain(self, url): |
---|
243 | """ Get a main domain. |
---|
244 | """ |
---|
245 | url = url.split('//')[-1] |
---|
246 | dompath = url.split(':')[0] |
---|
247 | dom = dompath.split('/')[0] |
---|
248 | return '.'.join(dom.split('.')[-2:]) |
---|
249 | |
---|
250 | def validateSEOProperty(self, property, value): |
---|
251 | """ Validate a seo property. |
---|
252 | """ |
---|
253 | purl = getToolByName(self.context, 'portal_url')() |
---|
254 | state = '' |
---|
255 | if property == PROP_PREFIX+'canonical': |
---|
256 | # validate seo canonical url property |
---|
257 | pdomain = self.getMainDomain(purl) |
---|
258 | if not pdomain == self.getMainDomain(value): |
---|
259 | state = _('canonical_msg', default=u'Canonical URL mast be in ${pdomain} domain.', mapping={'pdomain': pdomain}) |
---|
260 | return state |
---|
261 | |
---|
262 | def setProperty(self, property, value, type='string'): |
---|
263 | """ Add a new property. |
---|
264 | |
---|
265 | Sets a new property with the given id, value and type or changes it. |
---|
266 | """ |
---|
267 | context = aq_inner(self.context) |
---|
268 | state = self.validateSEOProperty(property, value) |
---|
269 | if not state: |
---|
270 | if context.hasProperty(property): |
---|
271 | context.manage_changeProperties({property: value}) |
---|
272 | else: |
---|
273 | context.manage_addProperty(property, value, type) |
---|
274 | return state |
---|
275 | |
---|
276 | def manageSEOProps(self, **kw): |
---|
277 | """ Manage seo properties. |
---|
278 | """ |
---|
279 | context = aq_inner(self.context) |
---|
280 | state = '' |
---|
281 | delete_list, seo_overrides_keys, seo_keys = [], [], [] |
---|
282 | seo_items = dict([(k[len(SEO_PREFIX):],v) for k,v in kw.items() if k.startswith(SEO_PREFIX)]) |
---|
283 | for key in seo_items.keys(): |
---|
284 | if key.endswith(SUFFIX): |
---|
285 | seo_overrides_keys.append(key[:-len(SUFFIX)]) |
---|
286 | else: |
---|
287 | seo_keys.append(key) |
---|
288 | for seo_key in seo_keys: |
---|
289 | if seo_key == 'custommetatags': |
---|
290 | self.manageSEOCustomMetaTagsProperties(**kw) |
---|
291 | else: |
---|
292 | if seo_key in seo_overrides_keys and seo_items.get(seo_key+SUFFIX): |
---|
293 | seo_value = seo_items[seo_key] |
---|
294 | t_value = 'string' |
---|
295 | if type(seo_value)==type([]) or type(seo_value)==type(()): t_value = 'lines' |
---|
296 | state = self.setProperty(PROP_PREFIX+seo_key, seo_value, type=t_value) |
---|
297 | if state: |
---|
298 | return state |
---|
299 | elif context.hasProperty(PROP_PREFIX+seo_key): |
---|
300 | delete_list.append(PROP_PREFIX+seo_key) |
---|
301 | if delete_list: |
---|
302 | context.manage_delProperties(delete_list) |
---|
303 | return state |
---|
304 | |
---|
305 | def setSEOCustomMetaTags(self, custommetatags): |
---|
306 | """ Set seo custom metatags properties. |
---|
307 | """ |
---|
308 | context = aq_inner(self.context) |
---|
309 | for tag in custommetatags: |
---|
310 | self.setProperty('%s%s' % (PROP_CUSTOM_PREFIX, tag['meta_name']), tag['meta_content']) |
---|
311 | |
---|
312 | def delAllSEOCustomMetaTagsProperties(self): |
---|
313 | """ Delete all seo custom metatags properties. |
---|
314 | """ |
---|
315 | context = aq_inner(self.context) |
---|
316 | delete_list = [] |
---|
317 | for property, value in context.propertyItems(): |
---|
318 | if property.startswith(PROP_CUSTOM_PREFIX) and not property == PROP_CUSTOM_PREFIX: |
---|
319 | delete_list.append(property) |
---|
320 | if delete_list: |
---|
321 | context.manage_delProperties(delete_list) |
---|
322 | |
---|
323 | def updateSEOCustomMetaTagsProperties(self, custommetatags): |
---|
324 | """ Update seo custom metatags properties. |
---|
325 | """ |
---|
326 | context = aq_inner(self.context) |
---|
327 | site_properties = getToolByName(context, 'portal_properties') |
---|
328 | globalCustomMetaTags = [] |
---|
329 | if hasattr(site_properties, 'seo_properties'): |
---|
330 | custom_meta_tags = getattr(site_properties.seo_properties, 'default_custom_metatags', []) |
---|
331 | for tag in custom_meta_tags: |
---|
332 | name_value = tag.split(SEPERATOR) |
---|
333 | if name_value[0]: |
---|
334 | globalCustomMetaTags.append({'meta_name' : name_value[0], |
---|
335 | 'meta_content' : len(name_value) == 1 and '' or name_value[1]}) |
---|
336 | for tag in custommetatags: |
---|
337 | meta_name, meta_content = tag['meta_name'], tag['meta_content'] |
---|
338 | if meta_name: |
---|
339 | if not [gmt for gmt in globalCustomMetaTags if (gmt['meta_name']==meta_name and gmt['meta_content']==meta_content)]: |
---|
340 | self.setProperty('%s%s' % (PROP_CUSTOM_PREFIX, meta_name), meta_content) |
---|
341 | |
---|
342 | def manageSEOCustomMetaTagsProperties(self, **kw): |
---|
343 | """ Update seo custom metatags properties, if enabled checkbox override or delete properties. |
---|
344 | |
---|
345 | Change object properties by passing either a mapping object |
---|
346 | of name:value pairs {'foo':6} or passing name=value parameters. |
---|
347 | """ |
---|
348 | context = aq_inner(self.context) |
---|
349 | self.delAllSEOCustomMetaTagsProperties() |
---|
350 | if kw.get('seo_custommetatags_override'): |
---|
351 | custommetatags = kw.get('seo_custommetatags', {}) |
---|
352 | self.updateSEOCustomMetaTagsProperties(custommetatags) |
---|
353 | |
---|
354 | def __call__( self ): |
---|
355 | """ Perform the update seo properties and redirect if necessary, or render the page Call method. |
---|
356 | """ |
---|
357 | context = aq_inner(self.context) |
---|
358 | request = self.request |
---|
359 | form = self.request.form |
---|
360 | submitted = form.get('form.submitted', False) |
---|
361 | if submitted: |
---|
362 | state = self.manageSEOProps(**form) |
---|
363 | if not state: |
---|
364 | state = _('seoproperties_saved', default=u'Content SEO properties have been saved.') |
---|
365 | context.plone_utils.addPortalMessage(state) |
---|
366 | kwargs = {'modification_date' : DateTime()} |
---|
367 | context.plone_utils.contentEdit(context, **kwargs) |
---|
368 | return request.response.redirect(self.context.absolute_url()) |
---|
369 | context.plone_utils.addPortalMessage(state, 'error') |
---|
370 | return self.template() |
---|