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