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