[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 |
---|
[1815] | 5 | from plone.memoize import view |
---|
[392] | 6 | from plone.app.controlpanel.form import ControlPanelView |
---|
| 7 | |
---|
[408] | 8 | from Products.Five.browser import BrowserView |
---|
[401] | 9 | from Products.CMFCore.utils import getToolByName |
---|
| 10 | from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile |
---|
[1480] | 11 | from Products.CMFPlone import PloneMessageFactory as pmf |
---|
[401] | 12 | |
---|
[1463] | 13 | from quintagroup.seoptimizer import SeoptimizerMessageFactory as _ |
---|
[1671] | 14 | from quintagroup.seoptimizer import interfaces |
---|
[1463] | 15 | |
---|
[1183] | 16 | SEPERATOR = '|' |
---|
[1460] | 17 | SEO_PREFIX = 'seo_' |
---|
| 18 | PROP_PREFIX = 'qSEO_' |
---|
| 19 | SUFFIX = '_override' |
---|
[1461] | 20 | PROP_CUSTOM_PREFIX = 'qSEO_custom_' |
---|
| 21 | |
---|
[408] | 22 | class SEOContext( BrowserView ): |
---|
[1506] | 23 | """ This class contains methods that allows to edit html header meta tags. |
---|
[408] | 24 | """ |
---|
[1782] | 25 | |
---|
[1815] | 26 | def __init__(self, *args, **kwargs): |
---|
| 27 | super(SEOContext, self).__init__(*args, **kwargs) |
---|
| 28 | self._seotags = self._getSEOTags() |
---|
[1782] | 29 | |
---|
[1815] | 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 ): |
---|
[1506] | 62 | """ Get value from seo property by property name. |
---|
[408] | 63 | """ |
---|
[413] | 64 | context = aq_inner(self.context) |
---|
| 65 | |
---|
| 66 | if context.hasProperty(property_name): |
---|
[1815] | 67 | return context.getProperty(property_name, default) |
---|
[1475] | 68 | |
---|
[413] | 69 | if accessor: |
---|
[1815] | 70 | method = getattr(context, accessor, default) |
---|
[413] | 71 | if not callable(method): |
---|
[1815] | 72 | return default |
---|
[408] | 73 | |
---|
[413] | 74 | # Catch AttributeErrors raised by some AT applications |
---|
| 75 | try: |
---|
| 76 | value = method() |
---|
| 77 | except AttributeError: |
---|
[1815] | 78 | value = default |
---|
[1475] | 79 | |
---|
[413] | 80 | return value |
---|
[408] | 81 | |
---|
| 82 | |
---|
[1815] | 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) |
---|
[408] | 89 | |
---|
[1475] | 90 | |
---|
[408] | 91 | def seo_customMetaTags( self ): |
---|
[1506] | 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). |
---|
[408] | 94 | """ |
---|
[1265] | 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 ): |
---|
[1506] | 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). |
---|
[1265] | 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 ): |
---|
[1506] | 123 | """ Returned seo custom metatags from properties in context (local seo custom metatags). |
---|
[1265] | 124 | """ |
---|
[408] | 125 | result = [] |
---|
| 126 | property_prefix = 'qSEO_custom_' |
---|
| 127 | context = aq_inner(self.context) |
---|
| 128 | for property, value in context.propertyItems(): |
---|
[1265] | 129 | if property.startswith(property_prefix) and property[len(property_prefix):]: |
---|
[408] | 130 | result.append({'meta_name' : property[len(property_prefix):], |
---|
| 131 | 'meta_content' : value}) |
---|
[1265] | 132 | return result |
---|
[408] | 133 | |
---|
[1265] | 134 | def seo_globalCustomMetaTags( self ): |
---|
[1506] | 135 | """ Returned seo custom metatags from default_custom_metatags property in seo_properties. |
---|
[1265] | 136 | """ |
---|
| 137 | result = [] |
---|
| 138 | context = aq_inner(self.context) |
---|
[408] | 139 | site_properties = getToolByName(context, 'portal_properties') |
---|
| 140 | if hasattr(site_properties, 'seo_properties'): |
---|
[1183] | 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) |
---|
[1265] | 144 | if name_value[0]: |
---|
| 145 | result.append({'meta_name' : name_value[0], |
---|
[1309] | 146 | 'meta_content' : len(name_value) == 2 and name_value[1] or ''}) |
---|
[408] | 147 | return result |
---|
[1265] | 148 | |
---|
[408] | 149 | def seo_html_comment( self ): |
---|
[1506] | 150 | """ Generate HTML Comments from SEO properties. |
---|
[408] | 151 | """ |
---|
[413] | 152 | html_comment = self.getSEOProperty( 'qSEO_html_comment' ) |
---|
| 153 | return html_comment and html_comment or '' |
---|
[1475] | 154 | |
---|
[1624] | 155 | def meta_keywords( self ): |
---|
| 156 | """ Generate Meta Keywords from SEO properties (global and local) with Subject, |
---|
| 157 | depending on the options in configlet. |
---|
[408] | 158 | """ |
---|
| 159 | prop_name = 'qSEO_keywords' |
---|
| 160 | accessor = 'Subject' |
---|
| 161 | context = aq_inner(self.context) |
---|
[1468] | 162 | keywords = Set([]) |
---|
[408] | 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: |
---|
[1600] | 171 | subject = Set(method()) |
---|
[408] | 172 | except AttributeError: |
---|
[1600] | 173 | subject = keywords |
---|
[408] | 174 | |
---|
[1600] | 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 |
---|
[1475] | 202 | |
---|
[1638] | 203 | return tuple(keywords) |
---|
[1600] | 204 | |
---|
[1624] | 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 = '' |
---|
[1638] | 222 | return tuple(keywords) |
---|
[1624] | 223 | |
---|
[896] | 224 | def seo_canonical( self ): |
---|
[1506] | 225 | """ Generate canonical URL from SEO properties. |
---|
[896] | 226 | """ |
---|
[1671] | 227 | purl = getToolByName(self.context, 'portal_url')() |
---|
[1675] | 228 | canpath = queryAdapter(self.context, interfaces.ISEOCanonicalPath) |
---|
[1671] | 229 | return purl + canpath.canonical_path() |
---|
[896] | 230 | |
---|
| 231 | |
---|
[1460] | 232 | class SEOContextPropertiesView( BrowserView ): |
---|
[1506] | 233 | """ This class contains methods that allows to manage seo properties. |
---|
[1460] | 234 | """ |
---|
| 235 | template = ViewPageTemplateFile('templates/seo_context_properties.pt') |
---|
| 236 | |
---|
[1461] | 237 | def test( self, condition, first, second ): |
---|
| 238 | """ |
---|
| 239 | """ |
---|
| 240 | return condition and first or second |
---|
[1466] | 241 | |
---|
| 242 | def getMainDomain(self, url): |
---|
[1506] | 243 | """ Get a main domain. |
---|
| 244 | """ |
---|
[1466] | 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): |
---|
[1506] | 251 | """ Validate a seo property. |
---|
| 252 | """ |
---|
[1466] | 253 | purl = getToolByName(self.context, 'portal_url')() |
---|
| 254 | state = '' |
---|
| 255 | if property == PROP_PREFIX+'canonical': |
---|
[1506] | 256 | # validate seo canonical url property |
---|
[1466] | 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 | |
---|
[1462] | 262 | def setProperty(self, property, value, type='string'): |
---|
[1506] | 263 | """ Add a new property. |
---|
| 264 | |
---|
| 265 | Sets a new property with the given id, value and type or changes it. |
---|
| 266 | """ |
---|
[1462] | 267 | context = aq_inner(self.context) |
---|
[1466] | 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 |
---|
[1461] | 275 | |
---|
| 276 | def manageSEOProps(self, **kw): |
---|
[1506] | 277 | """ Manage seo properties. |
---|
| 278 | """ |
---|
[1461] | 279 | context = aq_inner(self.context) |
---|
[1466] | 280 | state = '' |
---|
[1468] | 281 | delete_list, seo_overrides_keys, seo_keys = [], [], [] |
---|
[1460] | 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): |
---|
[1468] | 285 | seo_overrides_keys.append(key[:-len(SUFFIX)]) |
---|
[1460] | 286 | else: |
---|
[1468] | 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) |
---|
[1466] | 301 | if delete_list: |
---|
| 302 | context.manage_delProperties(delete_list) |
---|
| 303 | return state |
---|
[1461] | 304 | |
---|
[1462] | 305 | def setSEOCustomMetaTags(self, custommetatags): |
---|
[1506] | 306 | """ Set seo custom metatags properties. |
---|
| 307 | """ |
---|
[1462] | 308 | context = aq_inner(self.context) |
---|
[1463] | 309 | for tag in custommetatags: |
---|
| 310 | self.setProperty('%s%s' % (PROP_CUSTOM_PREFIX, tag['meta_name']), tag['meta_content']) |
---|
[1462] | 311 | |
---|
[1466] | 312 | def delAllSEOCustomMetaTagsProperties(self): |
---|
[1506] | 313 | """ Delete all seo custom metatags properties. |
---|
| 314 | """ |
---|
[1466] | 315 | context = aq_inner(self.context) |
---|
[1461] | 316 | delete_list = [] |
---|
| 317 | for property, value in context.propertyItems(): |
---|
[1466] | 318 | if property.startswith(PROP_CUSTOM_PREFIX) and not property == PROP_CUSTOM_PREFIX: |
---|
[1461] | 319 | delete_list.append(property) |
---|
[1466] | 320 | if delete_list: |
---|
| 321 | context.manage_delProperties(delete_list) |
---|
[1461] | 322 | |
---|
[1466] | 323 | def updateSEOCustomMetaTagsProperties(self, custommetatags): |
---|
[1506] | 324 | """ Update seo custom metatags properties. |
---|
| 325 | """ |
---|
[1461] | 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)]: |
---|
[1466] | 340 | self.setProperty('%s%s' % (PROP_CUSTOM_PREFIX, meta_name), meta_content) |
---|
| 341 | |
---|
| 342 | def manageSEOCustomMetaTagsProperties(self, **kw): |
---|
[1506] | 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 | """ |
---|
[1461] | 348 | context = aq_inner(self.context) |
---|
[1466] | 349 | self.delAllSEOCustomMetaTagsProperties() |
---|
| 350 | if kw.get('seo_custommetatags_override'): |
---|
| 351 | custommetatags = kw.get('seo_custommetatags', {}) |
---|
| 352 | self.updateSEOCustomMetaTagsProperties(custommetatags) |
---|
[1460] | 353 | |
---|
| 354 | def __call__( self ): |
---|
[1506] | 355 | """ Perform the update seo properties and redirect if necessary, or render the page Call method. |
---|
[1460] | 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: |
---|
[1466] | 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) |
---|
[1475] | 366 | kwargs = {'modification_date' : DateTime()} |
---|
[1675] | 367 | context.plone_utils.contentEdit(context, **kwargs) |
---|
[1466] | 368 | return request.response.redirect(self.context.absolute_url()) |
---|
| 369 | context.plone_utils.addPortalMessage(state, 'error') |
---|
| 370 | return self.template() |
---|