[387] | 1 | from AccessControl import allow_module |
---|
[1313] | 2 | from zope.component import queryMultiAdapter |
---|
| 3 | |
---|
[1308] | 4 | from Acquisition import aq_inner |
---|
| 5 | from DateTime import DateTime |
---|
[387] | 6 | |
---|
[1308] | 7 | from Products.CMFCore.utils import getToolByName |
---|
| 8 | |
---|
[1313] | 9 | from quintagroup.seoptimizer.interfaces import IKeywords, IMappingMetaTags |
---|
| 10 | from quintagroup.seoptimizer.util import SortedDict |
---|
| 11 | |
---|
[392] | 12 | allow_module('quintagroup.seoptimizer.util') |
---|
[387] | 13 | qSEO_globals = globals() |
---|
[1308] | 14 | |
---|
| 15 | |
---|
| 16 | try: |
---|
[1312] | 17 | from Products.CMFPlone.PloneTool import PloneTool, METADATA_DCNAME, \ |
---|
| 18 | FLOOR_DATE, CEILING_DATE |
---|
[1308] | 19 | _present = hasattr(PloneTool, "listMetaTags") |
---|
| 20 | except ImportError: |
---|
| 21 | _present = False |
---|
| 22 | |
---|
| 23 | |
---|
| 24 | if _present: |
---|
| 25 | old_lmt = PloneTool.listMetaTags |
---|
| 26 | |
---|
| 27 | def listMetaTags(self, context): |
---|
| 28 | """Lists meta tags helper. |
---|
| 29 | |
---|
[1313] | 30 | Creates a mapping of meta tags. |
---|
[1308] | 31 | """ |
---|
[1313] | 32 | |
---|
| 33 | from quintagroup.seoptimizer.browser.interfaces import IPloneSEOLayer |
---|
| 34 | if not IPloneSEOLayer.providedBy(self.REQUEST): |
---|
| 35 | return old_lmt(getToolByName(self, 'plone_utils'), context) |
---|
| 36 | |
---|
| 37 | result = SortedDict() |
---|
[1308] | 38 | site_props = getToolByName(self, 'portal_properties').site_properties |
---|
| 39 | use_all = site_props.getProperty('exposeDCMetaTags', None) |
---|
| 40 | |
---|
[1313] | 41 | seo_context = queryMultiAdapter((context, self.REQUEST), name='seo_context') |
---|
| 42 | adapter = IMappingMetaTags(context, None) |
---|
| 43 | mapping_metadata = adapter and adapter.getMappingMetaTags() or SortedDict() |
---|
| 44 | |
---|
[1308] | 45 | if not use_all: |
---|
[1313] | 46 | metadata_names = mapping_metadata.has_key('DC.description') and {'DC.description': mapping_metadata['DC.description']} or SortedDict() |
---|
| 47 | if mapping_metadata.has_key('description'): |
---|
| 48 | metadata_names['description'] = mapping_metadata['description'] |
---|
[1308] | 49 | else: |
---|
[1313] | 50 | metadata_names = mapping_metadata |
---|
[1308] | 51 | |
---|
[1313] | 52 | for key, accessor in metadata_names.items(): |
---|
| 53 | if accessor == 'seo_keywords': |
---|
| 54 | # Set the additional matching keywords, if any |
---|
| 55 | adapter = IKeywords(context, None) |
---|
| 56 | if adapter is not None: |
---|
| 57 | keywords = adapter.listKeywords() |
---|
| 58 | if keywords: |
---|
| 59 | result['keywords'] = keywords |
---|
| 60 | continue |
---|
| 61 | |
---|
| 62 | method = getattr(seo_context, accessor, None) |
---|
| 63 | if method is None: |
---|
| 64 | method = getattr(aq_inner(context).aq_explicit, accessor, None) |
---|
| 65 | |
---|
[1308] | 66 | if not callable(method): |
---|
| 67 | continue |
---|
| 68 | |
---|
| 69 | # Catch AttributeErrors raised by some AT applications |
---|
| 70 | try: |
---|
| 71 | value = method() |
---|
| 72 | except AttributeError: |
---|
| 73 | value = None |
---|
| 74 | |
---|
| 75 | if not value: |
---|
| 76 | # No data |
---|
| 77 | continue |
---|
| 78 | if accessor == 'Publisher' and value == 'No publisher': |
---|
| 79 | # No publisher is hardcoded (TODO: still?) |
---|
| 80 | continue |
---|
| 81 | if isinstance(value, (list, tuple)): |
---|
| 82 | # convert a list to a string |
---|
| 83 | value = ', '.join(value) |
---|
| 84 | |
---|
[1313] | 85 | # Special cases |
---|
[1317] | 86 | if accessor == 'Description' and not metadata_names.has_key('description'): |
---|
[1313] | 87 | result['description'] = value |
---|
[1317] | 88 | elif accessor == 'Subject' and not metadata_names.has_key('keywords'): |
---|
[1313] | 89 | result['keywords'] = value |
---|
[1308] | 90 | |
---|
[1313] | 91 | if accessor not in ('Description', 'Subject'): |
---|
[1308] | 92 | result[key] = value |
---|
| 93 | |
---|
| 94 | if use_all: |
---|
| 95 | created = context.CreationDate() |
---|
| 96 | |
---|
| 97 | try: |
---|
| 98 | effective = context.EffectiveDate() |
---|
| 99 | if effective == 'None': |
---|
| 100 | effective = None |
---|
| 101 | if effective: |
---|
| 102 | effective = DateTime(effective) |
---|
| 103 | except AttributeError: |
---|
| 104 | effective = None |
---|
| 105 | |
---|
| 106 | try: |
---|
| 107 | expires = context.ExpirationDate() |
---|
| 108 | if expires == 'None': |
---|
| 109 | expires = None |
---|
| 110 | if expires: |
---|
| 111 | expires = DateTime(expires) |
---|
| 112 | except AttributeError: |
---|
| 113 | expires = None |
---|
| 114 | |
---|
| 115 | # Filter out DWIMish artifacts on effective / expiration dates |
---|
| 116 | if effective is not None and \ |
---|
| 117 | effective > FLOOR_DATE and \ |
---|
| 118 | effective != created: |
---|
| 119 | eff_str = effective.Date() |
---|
| 120 | else: |
---|
| 121 | eff_str = '' |
---|
| 122 | |
---|
| 123 | if expires is not None and expires < CEILING_DATE: |
---|
| 124 | exp_str = expires.Date() |
---|
| 125 | else: |
---|
| 126 | exp_str = '' |
---|
| 127 | |
---|
| 128 | if exp_str or exp_str: |
---|
| 129 | result['DC.date.valid_range'] = '%s - %s' % (eff_str, exp_str) |
---|
| 130 | |
---|
[1313] | 131 | # add custom meta tags (added from qseo tab by user) for given context and default from configlet |
---|
| 132 | custom_meta_tags = seo_context and seo_context.seo_customMetaTags() or [] |
---|
| 133 | for tag in custom_meta_tags: |
---|
| 134 | if tag['meta_content']: |
---|
| 135 | result[tag['meta_name']] = tag['meta_content'] |
---|
| 136 | |
---|
[1308] | 137 | return result |
---|
| 138 | |
---|
| 139 | PloneTool.listMetaTags = listMetaTags |
---|