[1] | 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 |
---|
[1075] | 9 | ##parameters=title=None,description=None,keywords=None,html_comment=None,robots=None,distribution=None,canonical=None,title_override=0,description_override=0,keywords_override=0,html_comment_override=0,robots_override=0,distribution_override=0,custommetatags=[] |
---|
[1] | 10 | |
---|
| 11 | def setProperty(context, property, value, type='string'): |
---|
| 12 | if context.hasProperty(property): |
---|
| 13 | context.manage_changeProperties({property: value}) |
---|
| 14 | else: |
---|
| 15 | context.manage_addProperty(property, value, type) |
---|
| 16 | |
---|
| 17 | delete_list = [] |
---|
| 18 | |
---|
| 19 | # update custom meta tags |
---|
| 20 | property_prefix = 'qSEO_custom_' |
---|
| 21 | custom_existing = [] |
---|
| 22 | for property, value in context.propertyItems(): |
---|
| 23 | if property.find(property_prefix) == 0 and len(property) > len(property_prefix): |
---|
| 24 | custom_existing.append(property) |
---|
| 25 | |
---|
| 26 | custom_updated = [] |
---|
| 27 | for tag in custommetatags: |
---|
| 28 | meta_name, meta_content = tag['meta_name'], tag['meta_content'] |
---|
| 29 | if meta_name and meta_content: |
---|
| 30 | setProperty(context, '%s%s' % (property_prefix, meta_name), meta_content) |
---|
| 31 | custom_updated.append('%s%s' % (property_prefix, meta_name)) |
---|
| 32 | |
---|
| 33 | #add not updated custom metatags to delete list |
---|
| 34 | for tag in custom_existing: |
---|
| 35 | if tag not in custom_updated: |
---|
| 36 | delete_list.append(tag) |
---|
| 37 | |
---|
| 38 | setProperty(context, 'qSEO_title', title) |
---|
| 39 | setProperty(context, 'qSEO_description', description) |
---|
| 40 | setProperty(context, 'qSEO_keywords', keywords, 'lines') |
---|
| 41 | setProperty(context, 'qSEO_html_comment', html_comment) |
---|
| 42 | setProperty(context, 'qSEO_robots', robots) |
---|
| 43 | setProperty(context, 'qSEO_distribution', distribution) |
---|
[1075] | 44 | setProperty(context, 'qSEO_canonical', canonical) |
---|
[1] | 45 | |
---|
| 46 | if not title_override: delete_list.append('qSEO_title') |
---|
| 47 | if not description_override: delete_list.append('qSEO_description') |
---|
| 48 | if not keywords_override: delete_list.append('qSEO_keywords') |
---|
| 49 | if not html_comment_override: delete_list.append('qSEO_html_comment') |
---|
| 50 | if not robots_override: delete_list.append('qSEO_robots') |
---|
| 51 | if not distribution_override: delete_list.append('qSEO_distribution') |
---|
| 52 | |
---|
| 53 | if delete_list: context.manage_delProperties(delete_list) |
---|
| 54 | |
---|
[1459] | 55 | from DateTime import DateTime |
---|
| 56 | kwargs = {'modification_date' : DateTime()} |
---|
| 57 | context.plone_utils.contentEdit(context, **kwargs) |
---|
| 58 | |
---|
[1] | 59 | msg ='Content SEO properties have been saved.' |
---|
| 60 | try: |
---|
| 61 | context.plone_utils.addPortalMessage(msg) |
---|
| 62 | return state |
---|
| 63 | except AttributeError: |
---|
| 64 | return state.set(context=context, portal_status_message=msg) |
---|