1 | from Acquisition import aq_inner |
---|
2 | from zope.component import queryAdapter |
---|
3 | from plone.app.controlpanel.form import ControlPanelView |
---|
4 | |
---|
5 | from Products.Five.browser import BrowserView |
---|
6 | from Products.CMFCore.utils import getToolByName |
---|
7 | from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile |
---|
8 | |
---|
9 | SEPERATOR = '|' |
---|
10 | HAS_CANONICAL_PATH = True |
---|
11 | SEO_PREFIX = 'seo_' |
---|
12 | PROP_PREFIX = 'qSEO_' |
---|
13 | SUFFIX = '_override' |
---|
14 | try: |
---|
15 | from quintagroup.canonicalpath.interfaces import ICanonicalPath |
---|
16 | except ImportError: |
---|
17 | HAS_CANONICAL_PATH = False |
---|
18 | |
---|
19 | class SEOContext( BrowserView ): |
---|
20 | """ |
---|
21 | """ |
---|
22 | def getSEOProperty( self, property_name, accessor='' ): |
---|
23 | """ |
---|
24 | """ |
---|
25 | context = aq_inner(self.context) |
---|
26 | |
---|
27 | if context.hasProperty(property_name): |
---|
28 | return context.getProperty(property_name) |
---|
29 | |
---|
30 | if accessor: |
---|
31 | method = getattr(context, accessor, None) |
---|
32 | if not callable(method): |
---|
33 | return None |
---|
34 | |
---|
35 | # Catch AttributeErrors raised by some AT applications |
---|
36 | try: |
---|
37 | value = method() |
---|
38 | except AttributeError: |
---|
39 | value = None |
---|
40 | |
---|
41 | return value |
---|
42 | |
---|
43 | def seo_title( self ): |
---|
44 | """ |
---|
45 | """ |
---|
46 | return self.getSEOProperty( 'qSEO_title', accessor='Title' ) |
---|
47 | |
---|
48 | def seo_robots( self ): |
---|
49 | """ |
---|
50 | """ |
---|
51 | robots = self.getSEOProperty( 'qSEO_robots' ) |
---|
52 | return robots and robots or 'ALL' |
---|
53 | |
---|
54 | def seo_description( self ): |
---|
55 | """ |
---|
56 | Generate Description from SEO properties |
---|
57 | """ |
---|
58 | |
---|
59 | return self.getSEOProperty( 'qSEO_description', accessor = 'Description') |
---|
60 | |
---|
61 | def seo_distribution( self ): |
---|
62 | """ |
---|
63 | Generate Description from SEO properties |
---|
64 | """ |
---|
65 | dist = self.getSEOProperty( 'qSEO_distribution' ) |
---|
66 | |
---|
67 | return dist and dist or 'Global' |
---|
68 | |
---|
69 | def seo_customMetaTags( self ): |
---|
70 | """ |
---|
71 | """ |
---|
72 | tags = self.seo_globalCustomMetaTags() |
---|
73 | loc = self.seo_localCustomMetaTags() |
---|
74 | |
---|
75 | names = [i['meta_name'] for i in tags] |
---|
76 | add_tags = [] |
---|
77 | for i in loc: |
---|
78 | if i['meta_name'] in names: |
---|
79 | for t in tags: |
---|
80 | if t['meta_name'] == i['meta_name']: |
---|
81 | t['meta_content'] = i['meta_content'] |
---|
82 | else: |
---|
83 | add_tags.append(i) |
---|
84 | tags.extend(add_tags) |
---|
85 | return tags |
---|
86 | |
---|
87 | def seo_globalWithoutLocalCustomMetaTags( self ): |
---|
88 | """ |
---|
89 | """ |
---|
90 | glob = self.seo_globalCustomMetaTags() |
---|
91 | loc = self.seo_localCustomMetaTags() |
---|
92 | names = [i['meta_name'] for i in loc] |
---|
93 | tags = [] |
---|
94 | for i in glob: |
---|
95 | if i['meta_name'] not in names: |
---|
96 | tags.append(i) |
---|
97 | return tags |
---|
98 | |
---|
99 | def seo_localCustomMetaTags( self ): |
---|
100 | """ |
---|
101 | """ |
---|
102 | result = [] |
---|
103 | property_prefix = 'qSEO_custom_' |
---|
104 | context = aq_inner(self.context) |
---|
105 | for property, value in context.propertyItems(): |
---|
106 | if property.startswith(property_prefix) and property[len(property_prefix):]: |
---|
107 | result.append({'meta_name' : property[len(property_prefix):], |
---|
108 | 'meta_content' : value}) |
---|
109 | return result |
---|
110 | |
---|
111 | def seo_globalCustomMetaTags( self ): |
---|
112 | """ |
---|
113 | """ |
---|
114 | result = [] |
---|
115 | context = aq_inner(self.context) |
---|
116 | site_properties = getToolByName(context, 'portal_properties') |
---|
117 | if hasattr(site_properties, 'seo_properties'): |
---|
118 | custom_meta_tags = getattr(site_properties.seo_properties, 'default_custom_metatags', []) |
---|
119 | for tag in custom_meta_tags: |
---|
120 | name_value = tag.split(SEPERATOR) |
---|
121 | if name_value[0]: |
---|
122 | result.append({'meta_name' : name_value[0], |
---|
123 | 'meta_content' : len(name_value) == 2 and name_value[1] or ''}) |
---|
124 | return result |
---|
125 | |
---|
126 | def seo_nonEmptylocalMetaTags( self ): |
---|
127 | """ |
---|
128 | """ |
---|
129 | return bool(self.seo_localCustomMetaTags()) |
---|
130 | |
---|
131 | def seo_html_comment( self ): |
---|
132 | """ |
---|
133 | """ |
---|
134 | html_comment = self.getSEOProperty( 'qSEO_html_comment' ) |
---|
135 | return html_comment and html_comment or '' |
---|
136 | |
---|
137 | def seo_keywords( self ): |
---|
138 | """ |
---|
139 | Generate Keywords from SEO properties |
---|
140 | """ |
---|
141 | |
---|
142 | prop_name = 'qSEO_keywords' |
---|
143 | add_keywords = 'additional_keywords' |
---|
144 | accessor = 'Subject' |
---|
145 | context = aq_inner(self.context) |
---|
146 | |
---|
147 | keywords = [] |
---|
148 | if context.hasProperty(prop_name): |
---|
149 | keywords = context.getProperty(prop_name) |
---|
150 | |
---|
151 | pprops = getToolByName(context, 'portal_properties') |
---|
152 | sheet = getattr(pprops, 'seo_properties', None) |
---|
153 | if sheet and sheet.hasProperty(add_keywords): |
---|
154 | keywords += sheet.getProperty(add_keywords) |
---|
155 | |
---|
156 | if keywords: |
---|
157 | return keywords |
---|
158 | |
---|
159 | method = getattr(context, accessor, None) |
---|
160 | if not callable(method): |
---|
161 | return None |
---|
162 | |
---|
163 | # Catch AttributeErrors raised by some AT applications |
---|
164 | try: |
---|
165 | value = method() |
---|
166 | except AttributeError: |
---|
167 | value = None |
---|
168 | |
---|
169 | return value |
---|
170 | |
---|
171 | def seo_canonical( self ): |
---|
172 | """ |
---|
173 | Get canonical URL |
---|
174 | """ |
---|
175 | canonical = self.getSEOProperty( 'qSEO_canonical' ) |
---|
176 | |
---|
177 | if not canonical and HAS_CANONICAL_PATH: |
---|
178 | canpath = queryAdapter(self.context, ICanonicalPath) |
---|
179 | if canpath: |
---|
180 | purl = getToolByName(self.context, 'portal_url')() |
---|
181 | cpath = canpath.canonical_path() |
---|
182 | canonical = purl + cpath |
---|
183 | |
---|
184 | return canonical and canonical or self.context.absolute_url() |
---|
185 | |
---|
186 | |
---|
187 | class SEOControlPanel( ControlPanelView ): |
---|
188 | """ |
---|
189 | """ |
---|
190 | template = ViewPageTemplateFile('templates/seo_controlpanel.pt') |
---|
191 | |
---|
192 | @property |
---|
193 | def portal_properties( self ): |
---|
194 | """ |
---|
195 | """ |
---|
196 | context = aq_inner(self.context) |
---|
197 | return getToolByName(context, 'portal_properties') |
---|
198 | |
---|
199 | @property |
---|
200 | def portal_types( self ): |
---|
201 | """ |
---|
202 | """ |
---|
203 | context = aq_inner(self.context) |
---|
204 | return getToolByName(context, 'portal_types') |
---|
205 | |
---|
206 | def hasSEOAction( self, type_info ): |
---|
207 | """ |
---|
208 | """ |
---|
209 | return filter(lambda x:x.id == 'seo_properties', type_info.listActions()) |
---|
210 | |
---|
211 | def test( self, condition, first, second ): |
---|
212 | """ |
---|
213 | """ |
---|
214 | return condition and first or second |
---|
215 | |
---|
216 | def getExposeDCMetaTags( self ): |
---|
217 | """ |
---|
218 | """ |
---|
219 | sp = self.portal_properties.site_properties |
---|
220 | return sp.getProperty('exposeDCMetaTags') |
---|
221 | |
---|
222 | def getDefaultCustomMetatags( self ): |
---|
223 | """ |
---|
224 | """ |
---|
225 | seo = self.portal_properties.seo_properties |
---|
226 | return seo.getProperty('default_custom_metatags') |
---|
227 | |
---|
228 | def getMetaTagsOrder( self ): |
---|
229 | """ |
---|
230 | """ |
---|
231 | seo = self.portal_properties.seo_properties |
---|
232 | return seo.getProperty('metatags_order') |
---|
233 | |
---|
234 | def getAdditionalKeywords( self ): |
---|
235 | """ |
---|
236 | """ |
---|
237 | seo = self.portal_properties.seo_properties |
---|
238 | return seo.getProperty('additional_keywords') |
---|
239 | |
---|
240 | def createMultiColumnList( self ): |
---|
241 | """ |
---|
242 | """ |
---|
243 | context = aq_inner(self.context) |
---|
244 | allTypes = self.portal_types.listContentTypes() |
---|
245 | try: |
---|
246 | return context.createMultiColumnList(allTypes, sort_on='title_or_id') |
---|
247 | except AttributeError: |
---|
248 | return [slist] |
---|
249 | |
---|
250 | def __call__( self ): |
---|
251 | """ |
---|
252 | """ |
---|
253 | context = aq_inner(self.context) |
---|
254 | request = self.request |
---|
255 | |
---|
256 | portalTypes=request.get( 'portalTypes', [] ) |
---|
257 | exposeDCMetaTags=request.get( 'exposeDCMetaTags', None ) |
---|
258 | additionalKeywords=request.get('additionalKeywords', []) |
---|
259 | default_custom_metatags=request.get('default_custom_metatags', []) |
---|
260 | metatags_order=request.get('metatags_order', []) |
---|
261 | |
---|
262 | site_props = getToolByName(self.portal_properties, 'site_properties') |
---|
263 | seo_props = getToolByName(self.portal_properties, 'seo_properties') |
---|
264 | |
---|
265 | form = self.request.form |
---|
266 | submitted = form.get('form.submitted', False) |
---|
267 | |
---|
268 | if submitted: |
---|
269 | site_props.manage_changeProperties(exposeDCMetaTags=exposeDCMetaTags) |
---|
270 | seo_props.manage_changeProperties(additional_keywords=additionalKeywords) |
---|
271 | seo_props.manage_changeProperties(default_custom_metatags=default_custom_metatags) |
---|
272 | seo_props.manage_changeProperties(metatags_order=metatags_order) |
---|
273 | |
---|
274 | for ptype in self.portal_types.objectValues(): |
---|
275 | acts = filter(lambda x: x.id == 'seo_properties', ptype.listActions()) |
---|
276 | action = acts and acts[0] or None |
---|
277 | if ptype.getId() in portalTypes: |
---|
278 | if action is None: |
---|
279 | ptype.addAction('seo_properties', |
---|
280 | 'SEO Properties', |
---|
281 | 'string:${object_url}/qseo_properties_edit_form', |
---|
282 | '', |
---|
283 | 'Modify portal content', |
---|
284 | 'object', |
---|
285 | visible=1) |
---|
286 | else: |
---|
287 | if action !=None: |
---|
288 | actions = list(ptype.listActions()) |
---|
289 | ptype.deleteActions([actions.index(a) for a in actions if a.getId()=='seo_properties']) |
---|
290 | return request.response.redirect('%s/%s'%(self.context.absolute_url(), '@@seo-controlpanel')) |
---|
291 | else: |
---|
292 | return self.template(portalTypes=portalTypes, exposeDCMetaTags=exposeDCMetaTags) |
---|
293 | |
---|
294 | def typeInfo( self, type_name ): |
---|
295 | """ |
---|
296 | """ |
---|
297 | return self.portal_types.getTypeInfo( type_name ) |
---|
298 | |
---|
299 | |
---|
300 | class SEOContextPropertiesView( BrowserView ): |
---|
301 | """ |
---|
302 | """ |
---|
303 | template = ViewPageTemplateFile('templates/seo_context_properties.pt') |
---|
304 | |
---|
305 | def updateSEOProps(self, **kw): |
---|
306 | delete_list, overrides, values = [], [], [] |
---|
307 | seo_items = dict([(k[len(SEO_PREFIX):],v) for k,v in kw.items() if k.startswith(SEO_PREFIX)]) |
---|
308 | for key in seo_items.keys(): |
---|
309 | if key.endswith(SUFFIX): |
---|
310 | overrides.append(key[:-len(SUFFIX)]) |
---|
311 | else: |
---|
312 | values.append(key) |
---|
313 | |
---|
314 | |
---|
315 | for val in values: |
---|
316 | if val in overrides and seo_items[val+SUFFIX]: |
---|
317 | setProperty(context, PROP_PREFIX+val, seo_items[val]) |
---|
318 | elif context.hasProperty(PROP_PREFIX+val): |
---|
319 | delete_list.append(PROP_PREFIX+val) |
---|
320 | if delete_list: context.manage_delProperties(delete_list) |
---|
321 | |
---|
322 | def setProperty(self, property, value, type='string'): |
---|
323 | context = self.context |
---|
324 | if context.hasProperty(property): |
---|
325 | context.manage_changeProperties({property: value}) |
---|
326 | else: |
---|
327 | context.manage_addProperty(property, value, type) |
---|
328 | |
---|
329 | |
---|
330 | def __call__( self ): |
---|
331 | """ |
---|
332 | """ |
---|
333 | context = aq_inner(self.context) |
---|
334 | request = self.request |
---|
335 | form = self.request.form |
---|
336 | submitted = form.get('form.submitted', False) |
---|
337 | if submitted: |
---|
338 | self.updateSEOProps(**form) |
---|
339 | |
---|
340 | |
---|
341 | #delete_list = [] |
---|
342 | |
---|
343 | ## update custom meta tags |
---|
344 | #property_prefix = 'qSEO_custom_' |
---|
345 | #custom_existing = [] |
---|
346 | #for property, value in context.propertyItems(): |
---|
347 | #if property.find(property_prefix) == 0 and len(property) > len(property_prefix): |
---|
348 | #custom_existing.append(property) |
---|
349 | |
---|
350 | #globalCustomMetaTags = [] |
---|
351 | #site_properties = getToolByName(context, 'portal_properties') |
---|
352 | #if hasattr(site_properties, 'seo_properties'): |
---|
353 | #custom_meta_tags = getattr(site_properties.seo_properties, 'default_custom_metatags', []) |
---|
354 | #for tag in custom_meta_tags: |
---|
355 | #name_value = tag.split(SEPERATOR) |
---|
356 | #if name_value[0]: |
---|
357 | #globalCustomMetaTags.append({'meta_name' : name_value[0], |
---|
358 | #'meta_content' : len(name_value) == 1 and '' or name_value[1]}) |
---|
359 | |
---|
360 | #custom_updated = [] |
---|
361 | #for tag in custommetatags: |
---|
362 | #meta_name, meta_content = tag['meta_name'], tag['meta_content'] |
---|
363 | #if meta_name: |
---|
364 | #if not [gmt for gmt in globalCustomMetaTags if (gmt['meta_name']==meta_name and gmt['meta_content']==meta_content)]: |
---|
365 | #setProperty(context, '%s%s' % (property_prefix, meta_name), meta_content) |
---|
366 | #custom_updated.append('%s%s' % (property_prefix, meta_name)) |
---|
367 | |
---|
368 | ##add not updated custom metatags to delete list |
---|
369 | #for tag in custom_existing: |
---|
370 | #if tag not in custom_updated: |
---|
371 | #delete_list.append(tag) |
---|
372 | |
---|
373 | #msg ='Content SEO properties have been saved.' |
---|
374 | #try: |
---|
375 | #context.plone_utils.addPortalMessage(msg) |
---|
376 | #return state |
---|
377 | #except AttributeError: |
---|
378 | #return state.set(context=context, portal_status_message=msg) |
---|
379 | return request.response.redirect(self.context.absolute_url()) |
---|
380 | else: |
---|
381 | return self.template() |
---|