[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 ): |
---|
[1506] | 27 | """ This class contains methods that allows to edit html header meta tags. |
---|
[408] | 28 | """ |
---|
[413] | 29 | def getSEOProperty( self, property_name, accessor='' ): |
---|
[1506] | 30 | """ Get value from seo property by property name. |
---|
[408] | 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 ): |
---|
[1506] | 51 | """ Generate SEO Title from SEO properties. |
---|
[413] | 52 | """ |
---|
| 53 | return self.getSEOProperty( 'qSEO_title', accessor='Title' ) |
---|
[408] | 54 | |
---|
[413] | 55 | def seo_robots( self ): |
---|
[1506] | 56 | """ Generate SEO Robots from SEO properties. |
---|
[413] | 57 | """ |
---|
| 58 | robots = self.getSEOProperty( 'qSEO_robots' ) |
---|
| 59 | return robots and robots or 'ALL' |
---|
[408] | 60 | |
---|
[413] | 61 | def seo_description( self ): |
---|
[1506] | 62 | """ Generate Description from SEO properties. |
---|
[413] | 63 | """ |
---|
[1475] | 64 | |
---|
[413] | 65 | return self.getSEOProperty( 'qSEO_description', accessor = 'Description') |
---|
| 66 | |
---|
[408] | 67 | def seo_distribution( self ): |
---|
[1506] | 68 | """ Generate Distribution from SEO properties. |
---|
[408] | 69 | """ |
---|
[413] | 70 | dist = self.getSEOProperty( 'qSEO_distribution' ) |
---|
[408] | 71 | |
---|
[413] | 72 | return dist and dist or 'Global' |
---|
[408] | 73 | |
---|
| 74 | def seo_customMetaTags( self ): |
---|
[1506] | 75 | """ Returned seo custom metatags from default_custom_metatags property in seo_properties |
---|
| 76 | (global seo custom metatags) with update from seo custom metatags properties in context (local seo custom metatags). |
---|
[408] | 77 | """ |
---|
[1265] | 78 | tags = self.seo_globalCustomMetaTags() |
---|
| 79 | loc = self.seo_localCustomMetaTags() |
---|
| 80 | names = [i['meta_name'] for i in tags] |
---|
| 81 | add_tags = [] |
---|
| 82 | for i in loc: |
---|
| 83 | if i['meta_name'] in names: |
---|
| 84 | for t in tags: |
---|
| 85 | if t['meta_name'] == i['meta_name']: |
---|
| 86 | t['meta_content'] = i['meta_content'] |
---|
| 87 | else: |
---|
| 88 | add_tags.append(i) |
---|
| 89 | tags.extend(add_tags) |
---|
| 90 | return tags |
---|
| 91 | |
---|
| 92 | def seo_globalWithoutLocalCustomMetaTags( self ): |
---|
[1506] | 93 | """ Returned seo custom metatags from default_custom_metatags property in seo_properties |
---|
| 94 | (global seo custom metatags) without seo custom metatags from properties in context (local seo custom metatags). |
---|
[1265] | 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 ): |
---|
[1506] | 106 | """ Returned seo custom metatags from properties in context (local seo custom metatags). |
---|
[1265] | 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 ): |
---|
[1506] | 118 | """ Returned seo custom metatags from default_custom_metatags property in seo_properties. |
---|
[1265] | 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 ): |
---|
[1506] | 138 | """ Generate HTML Comments from SEO properties. |
---|
[408] | 139 | """ |
---|
[413] | 140 | html_comment = self.getSEOProperty( 'qSEO_html_comment' ) |
---|
| 141 | return html_comment and html_comment or '' |
---|
[1475] | 142 | |
---|
[1624] | 143 | def meta_keywords( self ): |
---|
| 144 | """ Generate Meta Keywords from SEO properties (global and local) with Subject, |
---|
| 145 | depending on the options in configlet. |
---|
[408] | 146 | """ |
---|
| 147 | prop_name = 'qSEO_keywords' |
---|
| 148 | accessor = 'Subject' |
---|
| 149 | context = aq_inner(self.context) |
---|
[1468] | 150 | keywords = Set([]) |
---|
[408] | 151 | pprops = getToolByName(context, 'portal_properties') |
---|
| 152 | sheet = getattr(pprops, 'seo_properties', None) |
---|
| 153 | method = getattr(context, accessor, None) |
---|
| 154 | if not callable(method): |
---|
| 155 | return None |
---|
| 156 | |
---|
| 157 | # Catch AttributeErrors raised by some AT applications |
---|
| 158 | try: |
---|
[1600] | 159 | subject = Set(method()) |
---|
[408] | 160 | except AttributeError: |
---|
[1600] | 161 | subject = keywords |
---|
[408] | 162 | |
---|
[1600] | 163 | if sheet: |
---|
| 164 | settings_use_keywords_sg = sheet.getProperty('settings_use_keywords_sg') |
---|
| 165 | settings_use_keywords_lg = sheet.getProperty('settings_use_keywords_lg') |
---|
| 166 | global_keywords = Set(sheet.getProperty('additional_keywords', None)) |
---|
| 167 | local_keywords = Set(context.getProperty(prop_name, None)) |
---|
| 168 | # Subject overrides global seo keywords and global overrides local seo keywords |
---|
| 169 | if [settings_use_keywords_sg, settings_use_keywords_lg] == [1, 1]: |
---|
| 170 | keywords = subject |
---|
| 171 | # Subject overrides global seo keywords and merge global and local seo keywords |
---|
| 172 | elif [settings_use_keywords_sg, settings_use_keywords_lg] == [1, 2]: |
---|
| 173 | keywords = subject | local_keywords |
---|
| 174 | # Global seo keywords overrides Subject and global overrides local seo keywords |
---|
| 175 | elif [settings_use_keywords_sg, settings_use_keywords_lg] == [2, 1]: |
---|
| 176 | keywords = global_keywords |
---|
| 177 | # Global seo keywords overrides Subject and merge global and local seo keywords |
---|
| 178 | elif [settings_use_keywords_sg, settings_use_keywords_lg] == [2, 2]: |
---|
| 179 | keywords = global_keywords | local_keywords |
---|
| 180 | # Merge Subject and global seo keywords and global overrides local seo keywords |
---|
| 181 | elif [settings_use_keywords_sg, settings_use_keywords_lg] == [3, 1]: |
---|
| 182 | keywords = subject | global_keywords |
---|
| 183 | # Merge Subject and global seo keywords and merge global and local seo keywords |
---|
| 184 | elif [settings_use_keywords_sg, settings_use_keywords_lg] == [3, 2]: |
---|
| 185 | keywords = subject | global_keywords | local_keywords |
---|
| 186 | else: |
---|
| 187 | keywords = subject |
---|
| 188 | else: |
---|
| 189 | keywords = subject |
---|
[1475] | 190 | |
---|
[1638] | 191 | return tuple(keywords) |
---|
[1600] | 192 | |
---|
[1624] | 193 | def seo_keywords( self ): |
---|
| 194 | """ Generate SEO Keywords from SEO properties (global merde local). |
---|
| 195 | """ |
---|
| 196 | prop_name = 'qSEO_keywords' |
---|
| 197 | context = aq_inner(self.context) |
---|
| 198 | keywords = Set([]) |
---|
| 199 | pprops = getToolByName(context, 'portal_properties') |
---|
| 200 | sheet = getattr(pprops, 'seo_properties', None) |
---|
| 201 | |
---|
| 202 | if sheet: |
---|
| 203 | settings_use_keywords_sg = sheet.getProperty('settings_use_keywords_sg') |
---|
| 204 | settings_use_keywords_lg = sheet.getProperty('settings_use_keywords_lg') |
---|
| 205 | global_keywords = Set(sheet.getProperty('additional_keywords', None)) |
---|
| 206 | local_keywords = Set(context.getProperty(prop_name, None)) |
---|
| 207 | keywords = global_keywords | local_keywords |
---|
| 208 | else: |
---|
| 209 | keywords = '' |
---|
[1638] | 210 | return tuple(keywords) |
---|
[1624] | 211 | |
---|
[896] | 212 | def seo_canonical( self ): |
---|
[1506] | 213 | """ Generate canonical URL from SEO properties. |
---|
[896] | 214 | """ |
---|
| 215 | canonical = self.getSEOProperty( 'qSEO_canonical' ) |
---|
| 216 | |
---|
| 217 | if not canonical and HAS_CANONICAL_PATH: |
---|
| 218 | canpath = queryAdapter(self.context, ICanonicalPath) |
---|
| 219 | if canpath: |
---|
| 220 | purl = getToolByName(self.context, 'portal_url')() |
---|
| 221 | cpath = canpath.canonical_path() |
---|
| 222 | canonical = purl + cpath |
---|
| 223 | |
---|
| 224 | return canonical and canonical or self.context.absolute_url() |
---|
| 225 | |
---|
| 226 | |
---|
[392] | 227 | class SEOControlPanel( ControlPanelView ): |
---|
[1506] | 228 | """ The class with methods configuration Search Engine Optimizer in configlet. |
---|
[392] | 229 | """ |
---|
[401] | 230 | template = ViewPageTemplateFile('templates/seo_controlpanel.pt') |
---|
[392] | 231 | |
---|
[401] | 232 | @property |
---|
| 233 | def portal_properties( self ): |
---|
| 234 | """ |
---|
| 235 | """ |
---|
| 236 | context = aq_inner(self.context) |
---|
| 237 | return getToolByName(context, 'portal_properties') |
---|
[392] | 238 | |
---|
[401] | 239 | @property |
---|
| 240 | def portal_types( self ): |
---|
[1506] | 241 | """ Returned a list of portal types. |
---|
[401] | 242 | """ |
---|
| 243 | context = aq_inner(self.context) |
---|
| 244 | return getToolByName(context, 'portal_types') |
---|
| 245 | |
---|
| 246 | def hasSEOAction( self, type_info ): |
---|
| 247 | """ |
---|
| 248 | """ |
---|
| 249 | return filter(lambda x:x.id == 'seo_properties', type_info.listActions()) |
---|
| 250 | |
---|
| 251 | def test( self, condition, first, second ): |
---|
| 252 | """ |
---|
| 253 | """ |
---|
| 254 | return condition and first or second |
---|
| 255 | |
---|
| 256 | def getExposeDCMetaTags( self ): |
---|
[1506] | 257 | """ Get value from exposeDCMetaTags property in seo_properties. |
---|
[401] | 258 | """ |
---|
| 259 | sp = self.portal_properties.site_properties |
---|
| 260 | return sp.getProperty('exposeDCMetaTags') |
---|
[1313] | 261 | |
---|
[401] | 262 | def getDefaultCustomMetatags( self ): |
---|
[1506] | 263 | """ Get values from default_custom_metatags property in seo_properties. |
---|
[401] | 264 | """ |
---|
| 265 | seo = self.portal_properties.seo_properties |
---|
| 266 | return seo.getProperty('default_custom_metatags') |
---|
| 267 | |
---|
[1313] | 268 | def getMetaTagsOrder( self ): |
---|
[1506] | 269 | """ Get values from metatags_order property in seo_properties. |
---|
[1313] | 270 | """ |
---|
| 271 | seo = self.portal_properties.seo_properties |
---|
| 272 | return seo.getProperty('metatags_order') |
---|
| 273 | |
---|
[401] | 274 | def getAdditionalKeywords( self ): |
---|
[1506] | 275 | """ Get values from additional_keywords property in seo_properties. |
---|
[401] | 276 | """ |
---|
| 277 | seo = self.portal_properties.seo_properties |
---|
| 278 | return seo.getProperty('additional_keywords') |
---|
| 279 | |
---|
[1645] | 280 | def getUsageKeywordsInMetatags( self ): |
---|
| 281 | """ Get values from usage_keywords_in_metatags property in seo_properties. |
---|
| 282 | """ |
---|
| 283 | seo = self.portal_properties.seo_properties |
---|
| 284 | return seo.getProperty('usage_keywords_in_metatags') |
---|
| 285 | |
---|
[401] | 286 | def createMultiColumnList( self ): |
---|
| 287 | """ |
---|
| 288 | """ |
---|
| 289 | context = aq_inner(self.context) |
---|
| 290 | allTypes = self.portal_types.listContentTypes() |
---|
| 291 | try: |
---|
| 292 | return context.createMultiColumnList(allTypes, sort_on='title_or_id') |
---|
| 293 | except AttributeError: |
---|
| 294 | return [slist] |
---|
| 295 | |
---|
| 296 | def __call__( self ): |
---|
[1506] | 297 | """ Perform the update and redirect if necessary, or render the page. |
---|
[401] | 298 | """ |
---|
| 299 | context = aq_inner(self.context) |
---|
| 300 | request = self.request |
---|
| 301 | |
---|
[1518] | 302 | content_types_seoprops_enabled = request.get( 'contentTypes', [] ) |
---|
| 303 | exposeDCMetaTags = request.get( 'exposeDCMetaTags', None ) |
---|
| 304 | additionalKeywords = request.get('additionalKeywords', []) |
---|
| 305 | default_custom_metatags = request.get('default_custom_metatags', []) |
---|
| 306 | metatags_order = request.get('metatags_order', []) |
---|
[1645] | 307 | usageKeywordsInMetatags = request.get('usageKeywordsInMetatags', None) |
---|
[1580] | 308 | settingsUseKeywordsSG = int(request.get('settingsUseKeywordsSG', 1)) |
---|
| 309 | settingsUseKeywordsLG = int(request.get('settingsUseKeywordsLG', 1)) |
---|
[401] | 310 | |
---|
| 311 | site_props = getToolByName(self.portal_properties, 'site_properties') |
---|
| 312 | seo_props = getToolByName(self.portal_properties, 'seo_properties') |
---|
| 313 | |
---|
| 314 | form = self.request.form |
---|
| 315 | submitted = form.get('form.submitted', False) |
---|
| 316 | |
---|
| 317 | if submitted: |
---|
| 318 | site_props.manage_changeProperties(exposeDCMetaTags=exposeDCMetaTags) |
---|
| 319 | seo_props.manage_changeProperties(additional_keywords=additionalKeywords) |
---|
| 320 | seo_props.manage_changeProperties(default_custom_metatags=default_custom_metatags) |
---|
[1313] | 321 | seo_props.manage_changeProperties(metatags_order=metatags_order) |
---|
[1518] | 322 | seo_props.manage_changeProperties(content_types_seoprops_enabled=content_types_seoprops_enabled) |
---|
[1645] | 323 | seo_props.manage_changeProperties(usage_keywords_in_metatags=usageKeywordsInMetatags) |
---|
[1580] | 324 | seo_props.manage_changeProperties(settings_use_keywords_sg=settingsUseKeywordsSG) |
---|
| 325 | seo_props.manage_changeProperties(settings_use_keywords_lg=settingsUseKeywordsLG) |
---|
[401] | 326 | |
---|
| 327 | for ptype in self.portal_types.objectValues(): |
---|
| 328 | acts = filter(lambda x: x.id == 'seo_properties', ptype.listActions()) |
---|
| 329 | action = acts and acts[0] or None |
---|
[1518] | 330 | if ptype.getId() in content_types_seoprops_enabled: |
---|
[401] | 331 | if action is None: |
---|
| 332 | ptype.addAction('seo_properties', |
---|
| 333 | 'SEO Properties', |
---|
[1463] | 334 | 'string:${object_url}/@@seo-context-properties', |
---|
[1518] | 335 | "python:exists('portal/@@seo-context-properties')", |
---|
[401] | 336 | 'Modify portal content', |
---|
| 337 | 'object', |
---|
| 338 | visible=1) |
---|
| 339 | else: |
---|
| 340 | if action !=None: |
---|
| 341 | actions = list(ptype.listActions()) |
---|
| 342 | ptype.deleteActions([actions.index(a) for a in actions if a.getId()=='seo_properties']) |
---|
[1480] | 343 | context.plone_utils.addPortalMessage(pmf(u'Changes saved.')) |
---|
| 344 | return request.response.redirect('%s/%s'%(self.context.absolute_url(), 'plone_control_panel')) |
---|
[401] | 345 | else: |
---|
[1518] | 346 | return self.template(contentTypes=content_types_seoprops_enabled, exposeDCMetaTags=exposeDCMetaTags) |
---|
[401] | 347 | |
---|
| 348 | def typeInfo( self, type_name ): |
---|
[1506] | 349 | """ Get info type by type name. |
---|
[401] | 350 | """ |
---|
| 351 | return self.portal_types.getTypeInfo( type_name ) |
---|
[1460] | 352 | |
---|
[1580] | 353 | def select_settings_use_keywords_sg(self): |
---|
| 354 | context = aq_inner(self.context) |
---|
| 355 | site_properties = getToolByName(context, 'portal_properties') |
---|
| 356 | if hasattr(site_properties, 'seo_properties'): |
---|
| 357 | settings_use_keywords_sg = getattr(site_properties.seo_properties, 'settings_use_keywords_sg', 0) |
---|
| 358 | return settings_use_keywords_sg |
---|
[1460] | 359 | |
---|
[1580] | 360 | def select_settings_use_keywords_lg(self): |
---|
| 361 | context = aq_inner(self.context) |
---|
| 362 | site_properties = getToolByName(context, 'portal_properties') |
---|
| 363 | if hasattr(site_properties, 'seo_properties'): |
---|
| 364 | settings_use_keywords_lg = getattr(site_properties.seo_properties, 'settings_use_keywords_lg', 0) |
---|
| 365 | return settings_use_keywords_lg |
---|
| 366 | |
---|
| 367 | |
---|
[1460] | 368 | class SEOContextPropertiesView( BrowserView ): |
---|
[1506] | 369 | """ This class contains methods that allows to manage seo properties. |
---|
[1460] | 370 | """ |
---|
| 371 | template = ViewPageTemplateFile('templates/seo_context_properties.pt') |
---|
| 372 | |
---|
[1461] | 373 | def test( self, condition, first, second ): |
---|
| 374 | """ |
---|
| 375 | """ |
---|
| 376 | return condition and first or second |
---|
[1466] | 377 | |
---|
| 378 | def getMainDomain(self, url): |
---|
[1506] | 379 | """ Get a main domain. |
---|
| 380 | """ |
---|
[1466] | 381 | url = url.split('//')[-1] |
---|
| 382 | dompath = url.split(':')[0] |
---|
| 383 | dom = dompath.split('/')[0] |
---|
| 384 | return '.'.join(dom.split('.')[-2:]) |
---|
| 385 | |
---|
| 386 | def validateSEOProperty(self, property, value): |
---|
[1506] | 387 | """ Validate a seo property. |
---|
| 388 | """ |
---|
[1466] | 389 | purl = getToolByName(self.context, 'portal_url')() |
---|
| 390 | state = '' |
---|
| 391 | if property == PROP_PREFIX+'canonical': |
---|
[1506] | 392 | # validate seo canonical url property |
---|
[1466] | 393 | pdomain = self.getMainDomain(purl) |
---|
| 394 | if not pdomain == self.getMainDomain(value): |
---|
| 395 | state = _('canonical_msg', default=u'Canonical URL mast be in ${pdomain} domain.', mapping={'pdomain': pdomain}) |
---|
| 396 | return state |
---|
| 397 | |
---|
[1462] | 398 | def setProperty(self, property, value, type='string'): |
---|
[1506] | 399 | """ Add a new property. |
---|
| 400 | |
---|
| 401 | Sets a new property with the given id, value and type or changes it. |
---|
| 402 | """ |
---|
[1462] | 403 | context = aq_inner(self.context) |
---|
[1466] | 404 | state = self.validateSEOProperty(property, value) |
---|
| 405 | if not state: |
---|
| 406 | if context.hasProperty(property): |
---|
| 407 | context.manage_changeProperties({property: value}) |
---|
| 408 | else: |
---|
| 409 | context.manage_addProperty(property, value, type) |
---|
| 410 | return state |
---|
[1461] | 411 | |
---|
| 412 | def manageSEOProps(self, **kw): |
---|
[1506] | 413 | """ Manage seo properties. |
---|
| 414 | """ |
---|
[1461] | 415 | context = aq_inner(self.context) |
---|
[1466] | 416 | state = '' |
---|
[1468] | 417 | delete_list, seo_overrides_keys, seo_keys = [], [], [] |
---|
[1460] | 418 | seo_items = dict([(k[len(SEO_PREFIX):],v) for k,v in kw.items() if k.startswith(SEO_PREFIX)]) |
---|
| 419 | for key in seo_items.keys(): |
---|
| 420 | if key.endswith(SUFFIX): |
---|
[1468] | 421 | seo_overrides_keys.append(key[:-len(SUFFIX)]) |
---|
[1460] | 422 | else: |
---|
[1468] | 423 | seo_keys.append(key) |
---|
| 424 | for seo_key in seo_keys: |
---|
| 425 | if seo_key == 'custommetatags': |
---|
| 426 | self.manageSEOCustomMetaTagsProperties(**kw) |
---|
| 427 | else: |
---|
| 428 | if seo_key in seo_overrides_keys and seo_items.get(seo_key+SUFFIX): |
---|
| 429 | seo_value = seo_items[seo_key] |
---|
| 430 | t_value = 'string' |
---|
| 431 | if type(seo_value)==type([]) or type(seo_value)==type(()): t_value = 'lines' |
---|
| 432 | state = self.setProperty(PROP_PREFIX+seo_key, seo_value, type=t_value) |
---|
| 433 | if state: |
---|
| 434 | return state |
---|
| 435 | elif context.hasProperty(PROP_PREFIX+seo_key): |
---|
| 436 | delete_list.append(PROP_PREFIX+seo_key) |
---|
[1466] | 437 | if delete_list: |
---|
| 438 | context.manage_delProperties(delete_list) |
---|
| 439 | return state |
---|
[1461] | 440 | |
---|
[1462] | 441 | def setSEOCustomMetaTags(self, custommetatags): |
---|
[1506] | 442 | """ Set seo custom metatags properties. |
---|
| 443 | """ |
---|
[1462] | 444 | context = aq_inner(self.context) |
---|
[1463] | 445 | for tag in custommetatags: |
---|
| 446 | self.setProperty('%s%s' % (PROP_CUSTOM_PREFIX, tag['meta_name']), tag['meta_content']) |
---|
[1462] | 447 | |
---|
[1466] | 448 | def delAllSEOCustomMetaTagsProperties(self): |
---|
[1506] | 449 | """ Delete all seo custom metatags properties. |
---|
| 450 | """ |
---|
[1466] | 451 | context = aq_inner(self.context) |
---|
[1461] | 452 | delete_list = [] |
---|
| 453 | for property, value in context.propertyItems(): |
---|
[1466] | 454 | if property.startswith(PROP_CUSTOM_PREFIX) and not property == PROP_CUSTOM_PREFIX: |
---|
[1461] | 455 | delete_list.append(property) |
---|
[1466] | 456 | if delete_list: |
---|
| 457 | context.manage_delProperties(delete_list) |
---|
[1461] | 458 | |
---|
[1466] | 459 | def updateSEOCustomMetaTagsProperties(self, custommetatags): |
---|
[1506] | 460 | """ Update seo custom metatags properties. |
---|
| 461 | """ |
---|
[1461] | 462 | context = aq_inner(self.context) |
---|
| 463 | site_properties = getToolByName(context, 'portal_properties') |
---|
| 464 | globalCustomMetaTags = [] |
---|
| 465 | if hasattr(site_properties, 'seo_properties'): |
---|
| 466 | custom_meta_tags = getattr(site_properties.seo_properties, 'default_custom_metatags', []) |
---|
| 467 | for tag in custom_meta_tags: |
---|
| 468 | name_value = tag.split(SEPERATOR) |
---|
| 469 | if name_value[0]: |
---|
| 470 | globalCustomMetaTags.append({'meta_name' : name_value[0], |
---|
| 471 | 'meta_content' : len(name_value) == 1 and '' or name_value[1]}) |
---|
| 472 | for tag in custommetatags: |
---|
| 473 | meta_name, meta_content = tag['meta_name'], tag['meta_content'] |
---|
| 474 | if meta_name: |
---|
| 475 | if not [gmt for gmt in globalCustomMetaTags if (gmt['meta_name']==meta_name and gmt['meta_content']==meta_content)]: |
---|
[1466] | 476 | self.setProperty('%s%s' % (PROP_CUSTOM_PREFIX, meta_name), meta_content) |
---|
| 477 | |
---|
| 478 | def manageSEOCustomMetaTagsProperties(self, **kw): |
---|
[1506] | 479 | """ Update seo custom metatags properties, if enabled checkbox override or delete properties. |
---|
| 480 | |
---|
| 481 | Change object properties by passing either a mapping object |
---|
| 482 | of name:value pairs {'foo':6} or passing name=value parameters. |
---|
| 483 | """ |
---|
[1461] | 484 | context = aq_inner(self.context) |
---|
[1466] | 485 | self.delAllSEOCustomMetaTagsProperties() |
---|
| 486 | if kw.get('seo_custommetatags_override'): |
---|
| 487 | custommetatags = kw.get('seo_custommetatags', {}) |
---|
| 488 | self.updateSEOCustomMetaTagsProperties(custommetatags) |
---|
[1460] | 489 | |
---|
| 490 | def __call__( self ): |
---|
[1506] | 491 | """ Perform the update seo properties and redirect if necessary, or render the page Call method. |
---|
[1460] | 492 | """ |
---|
| 493 | context = aq_inner(self.context) |
---|
| 494 | request = self.request |
---|
| 495 | form = self.request.form |
---|
| 496 | submitted = form.get('form.submitted', False) |
---|
| 497 | if submitted: |
---|
[1466] | 498 | state = self.manageSEOProps(**form) |
---|
| 499 | if not state: |
---|
| 500 | state = _('seoproperties_saved', default=u'Content SEO properties have been saved.') |
---|
| 501 | context.plone_utils.addPortalMessage(state) |
---|
[1475] | 502 | kwargs = {'modification_date' : DateTime()} |
---|
| 503 | context.plone_utils.contentEdit(context, **kwargs) |
---|
[1466] | 504 | return request.response.redirect(self.context.absolute_url()) |
---|
| 505 | context.plone_utils.addPortalMessage(state, 'error') |
---|
| 506 | return self.template() |
---|