[387] | 1 | ## Controller Python Script "qseo_properties_edit" |
---|
| 2 | ##bind container=container |
---|
| 3 | ##bind context=context |
---|
| 4 | ##bind namespace= |
---|
| 5 | ##bind script=script |
---|
| 6 | ##bind state=state |
---|
| 7 | ##bind subpath=traverse_subpath |
---|
| 8 | ##title=Update SEO Properties |
---|
[1265] | 9 | ##parameters=title=None,description=None,canonical=None,keywords=None,html_comment=None,robots=None,distribution=None,title_override=0,description_override=0,canonical_override=0,keywords_override=0,html_comment_override=0,robots_override=0,distribution_override=0,custommetatags=[],custommetatags_override=0 |
---|
[387] | 10 | |
---|
[1269] | 11 | from Products.CMFCore.utils import getToolByName |
---|
| 12 | SEPERATOR = '|' |
---|
| 13 | |
---|
[387] | 14 | def setProperty(context, property, value, type='string'): |
---|
| 15 | if context.hasProperty(property): |
---|
| 16 | context.manage_changeProperties({property: value}) |
---|
| 17 | else: |
---|
| 18 | context.manage_addProperty(property, value, type) |
---|
| 19 | |
---|
| 20 | delete_list = [] |
---|
| 21 | |
---|
| 22 | # update custom meta tags |
---|
| 23 | property_prefix = 'qSEO_custom_' |
---|
| 24 | custom_existing = [] |
---|
| 25 | for property, value in context.propertyItems(): |
---|
| 26 | if property.find(property_prefix) == 0 and len(property) > len(property_prefix): |
---|
| 27 | custom_existing.append(property) |
---|
| 28 | |
---|
[1265] | 29 | if not custommetatags_override: custommetatags=[] |
---|
| 30 | |
---|
[1269] | 31 | globalCustomMetaTags = [] |
---|
| 32 | site_properties = getToolByName(context, 'portal_properties') |
---|
| 33 | if hasattr(site_properties, 'seo_properties'): |
---|
| 34 | custom_meta_tags = getattr(site_properties.seo_properties, 'default_custom_metatags', []) |
---|
| 35 | for tag in custom_meta_tags: |
---|
| 36 | name_value = tag.split(SEPERATOR) |
---|
| 37 | if name_value[0]: |
---|
| 38 | globalCustomMetaTags.append({'meta_name' : name_value[0], |
---|
| 39 | 'meta_content' : len(name_value) == 1 and '' or name_value[1]}) |
---|
| 40 | |
---|
[387] | 41 | custom_updated = [] |
---|
| 42 | for tag in custommetatags: |
---|
| 43 | meta_name, meta_content = tag['meta_name'], tag['meta_content'] |
---|
[1265] | 44 | if meta_name: |
---|
| 45 | if not [gmt for gmt in globalCustomMetaTags if (gmt['meta_name']==meta_name and gmt['meta_content']==meta_content)]: |
---|
| 46 | setProperty(context, '%s%s' % (property_prefix, meta_name), meta_content) |
---|
[387] | 47 | custom_updated.append('%s%s' % (property_prefix, meta_name)) |
---|
| 48 | |
---|
| 49 | #add not updated custom metatags to delete list |
---|
| 50 | for tag in custom_existing: |
---|
| 51 | if tag not in custom_updated: |
---|
| 52 | delete_list.append(tag) |
---|
| 53 | |
---|
| 54 | setProperty(context, 'qSEO_title', title) |
---|
| 55 | setProperty(context, 'qSEO_description', description) |
---|
| 56 | setProperty(context, 'qSEO_keywords', keywords, 'lines') |
---|
| 57 | setProperty(context, 'qSEO_html_comment', html_comment) |
---|
| 58 | setProperty(context, 'qSEO_robots', robots) |
---|
| 59 | setProperty(context, 'qSEO_distribution', distribution) |
---|
[896] | 60 | setProperty(context, 'qSEO_canonical', canonical) |
---|
[387] | 61 | |
---|
| 62 | if not title_override: delete_list.append('qSEO_title') |
---|
| 63 | if not description_override: delete_list.append('qSEO_description') |
---|
| 64 | if not keywords_override: delete_list.append('qSEO_keywords') |
---|
| 65 | if not html_comment_override: delete_list.append('qSEO_html_comment') |
---|
| 66 | if not robots_override: delete_list.append('qSEO_robots') |
---|
| 67 | if not distribution_override: delete_list.append('qSEO_distribution') |
---|
[896] | 68 | if not canonical_override: delete_list.append('qSEO_canonical') |
---|
[387] | 69 | |
---|
| 70 | if delete_list: context.manage_delProperties(delete_list) |
---|
| 71 | |
---|
[1458] | 72 | from DateTime import DateTime |
---|
| 73 | kwargs = {'modification_date' : DateTime()} |
---|
| 74 | context.plone_utils.contentEdit(context, **kwargs) |
---|
| 75 | |
---|
[387] | 76 | msg ='Content SEO properties have been saved.' |
---|
| 77 | try: |
---|
| 78 | context.plone_utils.addPortalMessage(msg) |
---|
| 79 | return state |
---|
| 80 | except AttributeError: |
---|
| 81 | return state.set(context=context, portal_status_message=msg) |
---|