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 | |
---|
12 | try: |
---|
13 | from quintagroup.canonicalpath.interfaces import ICanonicalPath |
---|
14 | except ImportError: |
---|
15 | HAS_CANONICAL_PATH = False |
---|
16 | |
---|
17 | class SEOContext( BrowserView ): |
---|
18 | """ |
---|
19 | """ |
---|
20 | def getSEOProperty( self, property_name, accessor='' ): |
---|
21 | """ |
---|
22 | """ |
---|
23 | context = aq_inner(self.context) |
---|
24 | |
---|
25 | if context.hasProperty(property_name): |
---|
26 | return context.getProperty(property_name) |
---|
27 | |
---|
28 | if accessor: |
---|
29 | method = getattr(context, accessor, None) |
---|
30 | if not callable(method): |
---|
31 | return None |
---|
32 | |
---|
33 | # Catch AttributeErrors raised by some AT applications |
---|
34 | try: |
---|
35 | value = method() |
---|
36 | except AttributeError: |
---|
37 | value = None |
---|
38 | |
---|
39 | return value |
---|
40 | |
---|
41 | def seo_title( self ): |
---|
42 | """ |
---|
43 | """ |
---|
44 | return self.getSEOProperty( 'qSEO_title', accessor='Title' ) |
---|
45 | |
---|
46 | def seo_robots( self ): |
---|
47 | """ |
---|
48 | """ |
---|
49 | robots = self.getSEOProperty( 'qSEO_robots' ) |
---|
50 | return robots and robots or 'ALL' |
---|
51 | |
---|
52 | def seo_description( self ): |
---|
53 | """ |
---|
54 | Generate Description from SEO properties |
---|
55 | """ |
---|
56 | |
---|
57 | return self.getSEOProperty( 'qSEO_description', accessor = 'Description') |
---|
58 | |
---|
59 | def seo_distribution( self ): |
---|
60 | """ |
---|
61 | Generate Description from SEO properties |
---|
62 | """ |
---|
63 | dist = self.getSEOProperty( 'qSEO_distribution' ) |
---|
64 | |
---|
65 | return dist and dist or 'Global' |
---|
66 | |
---|
67 | def seo_customMetaTags( self ): |
---|
68 | """ |
---|
69 | """ |
---|
70 | tags = self.seo_globalCustomMetaTags() |
---|
71 | loc = self.seo_localCustomMetaTags() |
---|
72 | |
---|
73 | names = [i['meta_name'] for i in tags] |
---|
74 | add_tags = [] |
---|
75 | for i in loc: |
---|
76 | if i['meta_name'] in names: |
---|
77 | for t in tags: |
---|
78 | if t['meta_name'] == i['meta_name']: |
---|
79 | t['meta_content'] = i['meta_content'] |
---|
80 | else: |
---|
81 | add_tags.append(i) |
---|
82 | tags.extend(add_tags) |
---|
83 | return tags |
---|
84 | |
---|
85 | def seo_globalWithoutLocalCustomMetaTags( self ): |
---|
86 | """ |
---|
87 | """ |
---|
88 | glob = self.seo_globalCustomMetaTags() |
---|
89 | loc = self.seo_localCustomMetaTags() |
---|
90 | names = [i['meta_name'] for i in loc] |
---|
91 | tags = [] |
---|
92 | for i in glob: |
---|
93 | if i['meta_name'] not in names: |
---|
94 | tags.append(i) |
---|
95 | return tags |
---|
96 | |
---|
97 | def seo_localCustomMetaTags( self ): |
---|
98 | """ |
---|
99 | """ |
---|
100 | result = [] |
---|
101 | property_prefix = 'qSEO_custom_' |
---|
102 | context = aq_inner(self.context) |
---|
103 | for property, value in context.propertyItems(): |
---|
104 | if property.startswith(property_prefix) and property[len(property_prefix):]: |
---|
105 | result.append({'meta_name' : property[len(property_prefix):], |
---|
106 | 'meta_content' : value}) |
---|
107 | return result |
---|
108 | |
---|
109 | def seo_globalCustomMetaTags( self ): |
---|
110 | """ |
---|
111 | """ |
---|
112 | result = [] |
---|
113 | context = aq_inner(self.context) |
---|
114 | site_properties = getToolByName(context, 'portal_properties') |
---|
115 | if hasattr(site_properties, 'seo_properties'): |
---|
116 | custom_meta_tags = getattr(site_properties.seo_properties, 'default_custom_metatags', []) |
---|
117 | for tag in custom_meta_tags: |
---|
118 | name_value = tag.split(SEPERATOR) |
---|
119 | if name_value[0]: |
---|
120 | result.append({'meta_name' : name_value[0], |
---|
121 | 'meta_content' : len(name_value) == 2 and name_value[1] or ''}) |
---|
122 | return result |
---|
123 | |
---|
124 | def seo_nonEmptylocalMetaTags( self ): |
---|
125 | """ |
---|
126 | """ |
---|
127 | return bool(self.seo_localCustomMetaTags()) |
---|
128 | |
---|
129 | def seo_html_comment( self ): |
---|
130 | """ |
---|
131 | """ |
---|
132 | html_comment = self.getSEOProperty( 'qSEO_html_comment' ) |
---|
133 | return html_comment and html_comment or '' |
---|
134 | |
---|
135 | def seo_keywords( self ): |
---|
136 | """ |
---|
137 | Generate Keywords from SEO properties |
---|
138 | """ |
---|
139 | |
---|
140 | prop_name = 'qSEO_keywords' |
---|
141 | add_keywords = 'additional_keywords' |
---|
142 | accessor = 'Subject' |
---|
143 | context = aq_inner(self.context) |
---|
144 | |
---|
145 | keywords = [] |
---|
146 | if context.hasProperty(prop_name): |
---|
147 | keywords = context.getProperty(prop_name) |
---|
148 | |
---|
149 | pprops = getToolByName(context, 'portal_properties') |
---|
150 | sheet = getattr(pprops, 'seo_properties', None) |
---|
151 | if sheet and sheet.hasProperty(add_keywords): |
---|
152 | keywords += sheet.getProperty(add_keywords) |
---|
153 | |
---|
154 | if keywords: |
---|
155 | return keywords |
---|
156 | |
---|
157 | method = getattr(context, accessor, None) |
---|
158 | if not callable(method): |
---|
159 | return None |
---|
160 | |
---|
161 | # Catch AttributeErrors raised by some AT applications |
---|
162 | try: |
---|
163 | value = method() |
---|
164 | except AttributeError: |
---|
165 | value = None |
---|
166 | |
---|
167 | return value |
---|
168 | |
---|
169 | def seo_canonical( self ): |
---|
170 | """ |
---|
171 | Get canonical URL |
---|
172 | """ |
---|
173 | canonical = self.getSEOProperty( 'qSEO_canonical' ) |
---|
174 | |
---|
175 | if not canonical and HAS_CANONICAL_PATH: |
---|
176 | canpath = queryAdapter(self.context, ICanonicalPath) |
---|
177 | if canpath: |
---|
178 | purl = getToolByName(self.context, 'portal_url')() |
---|
179 | cpath = canpath.canonical_path() |
---|
180 | canonical = purl + cpath |
---|
181 | |
---|
182 | return canonical and canonical or self.context.absolute_url() |
---|
183 | |
---|
184 | |
---|
185 | class SEOControlPanel( ControlPanelView ): |
---|
186 | """ |
---|
187 | """ |
---|
188 | template = ViewPageTemplateFile('templates/seo_controlpanel.pt') |
---|
189 | |
---|
190 | @property |
---|
191 | def portal_properties( self ): |
---|
192 | """ |
---|
193 | """ |
---|
194 | context = aq_inner(self.context) |
---|
195 | return getToolByName(context, 'portal_properties') |
---|
196 | |
---|
197 | @property |
---|
198 | def portal_types( self ): |
---|
199 | """ |
---|
200 | """ |
---|
201 | context = aq_inner(self.context) |
---|
202 | return getToolByName(context, 'portal_types') |
---|
203 | |
---|
204 | def hasSEOAction( self, type_info ): |
---|
205 | """ |
---|
206 | """ |
---|
207 | return filter(lambda x:x.id == 'seo_properties', type_info.listActions()) |
---|
208 | |
---|
209 | def test( self, condition, first, second ): |
---|
210 | """ |
---|
211 | """ |
---|
212 | return condition and first or second |
---|
213 | |
---|
214 | def getExposeDCMetaTags( self ): |
---|
215 | """ |
---|
216 | """ |
---|
217 | sp = self.portal_properties.site_properties |
---|
218 | return sp.getProperty('exposeDCMetaTags') |
---|
219 | |
---|
220 | def getDefaultCustomMetatags( self ): |
---|
221 | """ |
---|
222 | """ |
---|
223 | seo = self.portal_properties.seo_properties |
---|
224 | return seo.getProperty('default_custom_metatags') |
---|
225 | |
---|
226 | def getMetaTagsOrder( self ): |
---|
227 | """ |
---|
228 | """ |
---|
229 | seo = self.portal_properties.seo_properties |
---|
230 | return seo.getProperty('metatags_order') |
---|
231 | |
---|
232 | def getAdditionalKeywords( self ): |
---|
233 | """ |
---|
234 | """ |
---|
235 | seo = self.portal_properties.seo_properties |
---|
236 | return seo.getProperty('additional_keywords') |
---|
237 | |
---|
238 | def createMultiColumnList( self ): |
---|
239 | """ |
---|
240 | """ |
---|
241 | context = aq_inner(self.context) |
---|
242 | allTypes = self.portal_types.listContentTypes() |
---|
243 | try: |
---|
244 | return context.createMultiColumnList(allTypes, sort_on='title_or_id') |
---|
245 | except AttributeError: |
---|
246 | return [slist] |
---|
247 | |
---|
248 | def __call__( self ): |
---|
249 | """ |
---|
250 | """ |
---|
251 | context = aq_inner(self.context) |
---|
252 | request = self.request |
---|
253 | |
---|
254 | portalTypes=request.get( 'portalTypes', [] ) |
---|
255 | exposeDCMetaTags=request.get( 'exposeDCMetaTags', None ) |
---|
256 | additionalKeywords=request.get('additionalKeywords', []) |
---|
257 | default_custom_metatags=request.get('default_custom_metatags', []) |
---|
258 | metatags_order=request.get('metatags_order', []) |
---|
259 | |
---|
260 | site_props = getToolByName(self.portal_properties, 'site_properties') |
---|
261 | seo_props = getToolByName(self.portal_properties, 'seo_properties') |
---|
262 | |
---|
263 | form = self.request.form |
---|
264 | submitted = form.get('form.submitted', False) |
---|
265 | |
---|
266 | if submitted: |
---|
267 | site_props.manage_changeProperties(exposeDCMetaTags=exposeDCMetaTags) |
---|
268 | seo_props.manage_changeProperties(additional_keywords=additionalKeywords) |
---|
269 | seo_props.manage_changeProperties(default_custom_metatags=default_custom_metatags) |
---|
270 | seo_props.manage_changeProperties(metatags_order=metatags_order) |
---|
271 | |
---|
272 | for ptype in self.portal_types.objectValues(): |
---|
273 | acts = filter(lambda x: x.id == 'seo_properties', ptype.listActions()) |
---|
274 | action = acts and acts[0] or None |
---|
275 | if ptype.getId() in portalTypes: |
---|
276 | if action is None: |
---|
277 | ptype.addAction('seo_properties', |
---|
278 | 'SEO Properties', |
---|
279 | 'string:${object_url}/qseo_properties_edit_form', |
---|
280 | '', |
---|
281 | 'Modify portal content', |
---|
282 | 'object', |
---|
283 | visible=1) |
---|
284 | else: |
---|
285 | if action !=None: |
---|
286 | actions = list(ptype.listActions()) |
---|
287 | ptype.deleteActions([actions.index(a) for a in actions if a.getId()=='seo_properties']) |
---|
288 | return request.response.redirect('%s/%s'%(self.context.absolute_url(), '@@seo-controlpanel')) |
---|
289 | else: |
---|
290 | return self.template(portalTypes=portalTypes, exposeDCMetaTags=exposeDCMetaTags) |
---|
291 | |
---|
292 | def typeInfo( self, type_name ): |
---|
293 | """ |
---|
294 | """ |
---|
295 | return self.portal_types.getTypeInfo( type_name ) |
---|