[387] | 1 | from AccessControl import allow_module |
---|
[1308] | 2 | from util import SortedDict |
---|
| 3 | from Acquisition import aq_inner |
---|
| 4 | from DateTime import DateTime |
---|
[387] | 5 | |
---|
[1308] | 6 | from Products.CMFCore.utils import getToolByName |
---|
| 7 | |
---|
[392] | 8 | allow_module('quintagroup.seoptimizer.util') |
---|
[387] | 9 | qSEO_globals = globals() |
---|
[1308] | 10 | |
---|
| 11 | |
---|
| 12 | try: |
---|
| 13 | from Products.CMFPlone.PloneTool import PloneTool, METADATA_DCNAME |
---|
| 14 | _present = hasattr(PloneTool, "listMetaTags") |
---|
| 15 | except ImportError: |
---|
| 16 | _present = False |
---|
| 17 | |
---|
| 18 | |
---|
| 19 | |
---|
| 20 | if _present: |
---|
| 21 | old_lmt = PloneTool.listMetaTags |
---|
| 22 | |
---|
| 23 | def listMetaTags(self, context): |
---|
| 24 | """Lists meta tags helper. |
---|
| 25 | |
---|
| 26 | Creates a mapping of meta tags -> values for the listMetaTags script. |
---|
| 27 | """ |
---|
| 28 | result = {} |
---|
| 29 | site_props = getToolByName(self, 'portal_properties').site_properties |
---|
| 30 | use_all = site_props.getProperty('exposeDCMetaTags', None) |
---|
| 31 | |
---|
| 32 | if not use_all: |
---|
| 33 | metadata_names = {'Description': METADATA_DCNAME['Description']} |
---|
| 34 | else: |
---|
| 35 | metadata_names = METADATA_DCNAME |
---|
| 36 | |
---|
| 37 | for accessor, key in metadata_names.items(): |
---|
| 38 | method = getattr(aq_inner(context).aq_explicit, accessor, None) |
---|
| 39 | if not callable(method): |
---|
| 40 | continue |
---|
| 41 | |
---|
| 42 | # Catch AttributeErrors raised by some AT applications |
---|
| 43 | try: |
---|
| 44 | value = method() |
---|
| 45 | except AttributeError: |
---|
| 46 | value = None |
---|
| 47 | |
---|
| 48 | if not value: |
---|
| 49 | # No data |
---|
| 50 | continue |
---|
| 51 | if accessor == 'Publisher' and value == 'No publisher': |
---|
| 52 | # No publisher is hardcoded (TODO: still?) |
---|
| 53 | continue |
---|
| 54 | if isinstance(value, (list, tuple)): |
---|
| 55 | # convert a list to a string |
---|
| 56 | value = ', '.join(value) |
---|
| 57 | |
---|
| 58 | # Exclusion meta tag description and keywords |
---|
| 59 | # # Special cases |
---|
| 60 | # if accessor == 'Description': |
---|
| 61 | # result['description'] = value |
---|
| 62 | # elif accessor == 'Subject': |
---|
| 63 | # result['keywords'] = value |
---|
| 64 | |
---|
| 65 | if use_all: |
---|
| 66 | result[key] = value |
---|
| 67 | |
---|
| 68 | if use_all: |
---|
| 69 | created = context.CreationDate() |
---|
| 70 | |
---|
| 71 | try: |
---|
| 72 | effective = context.EffectiveDate() |
---|
| 73 | if effective == 'None': |
---|
| 74 | effective = None |
---|
| 75 | if effective: |
---|
| 76 | effective = DateTime(effective) |
---|
| 77 | except AttributeError: |
---|
| 78 | effective = None |
---|
| 79 | |
---|
| 80 | try: |
---|
| 81 | expires = context.ExpirationDate() |
---|
| 82 | if expires == 'None': |
---|
| 83 | expires = None |
---|
| 84 | if expires: |
---|
| 85 | expires = DateTime(expires) |
---|
| 86 | except AttributeError: |
---|
| 87 | expires = None |
---|
| 88 | |
---|
| 89 | # Filter out DWIMish artifacts on effective / expiration dates |
---|
| 90 | if effective is not None and \ |
---|
| 91 | effective > FLOOR_DATE and \ |
---|
| 92 | effective != created: |
---|
| 93 | eff_str = effective.Date() |
---|
| 94 | else: |
---|
| 95 | eff_str = '' |
---|
| 96 | |
---|
| 97 | if expires is not None and expires < CEILING_DATE: |
---|
| 98 | exp_str = expires.Date() |
---|
| 99 | else: |
---|
| 100 | exp_str = '' |
---|
| 101 | |
---|
| 102 | if exp_str or exp_str: |
---|
| 103 | result['DC.date.valid_range'] = '%s - %s' % (eff_str, exp_str) |
---|
| 104 | |
---|
| 105 | return result |
---|
| 106 | |
---|
| 107 | PloneTool.listMetaTags = listMetaTags |
---|