1 | from AccessControl import allow_module |
---|
2 | from zope.component import queryMultiAdapter |
---|
3 | from zope.i18nmessageid import MessageFactory |
---|
4 | |
---|
5 | from Acquisition import aq_inner |
---|
6 | from DateTime import DateTime |
---|
7 | |
---|
8 | from Products.CMFCore.utils import getToolByName |
---|
9 | |
---|
10 | from quintagroup.seoptimizer.interfaces import IKeywords, IMappingMetaTags |
---|
11 | from quintagroup.seoptimizer.util import SortedDict |
---|
12 | |
---|
13 | SeoptimizerMessageFactory = MessageFactory('quintagroup.seoptimizer') |
---|
14 | |
---|
15 | allow_module('quintagroup.seoptimizer.util') |
---|
16 | qSEO_globals = globals() |
---|
17 | |
---|
18 | |
---|
19 | try: |
---|
20 | from Products.CMFPlone.PloneTool import PloneTool, METADATA_DCNAME, \ |
---|
21 | FLOOR_DATE, CEILING_DATE |
---|
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 | |
---|
33 | Creates a mapping of meta tags. |
---|
34 | """ |
---|
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() |
---|
41 | site_props = getToolByName(self, 'portal_properties').site_properties |
---|
42 | use_all = site_props.getProperty('exposeDCMetaTags', None) |
---|
43 | |
---|
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 | |
---|
48 | if not use_all: |
---|
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'] |
---|
52 | else: |
---|
53 | metadata_names = mapping_metadata |
---|
54 | |
---|
55 | for key, accessor in metadata_names.items(): |
---|
56 | if accessor == 'seo_keywords': |
---|
57 | # Set the additional matching keywords, if any |
---|
58 | adapter = IKeywords(context, None) |
---|
59 | if adapter is not None: |
---|
60 | keywords = adapter.listKeywords() |
---|
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 | |
---|
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 | |
---|
88 | # Special cases |
---|
89 | if accessor == 'Description' and not metadata_names.has_key('description'): |
---|
90 | result['description'] = value |
---|
91 | elif accessor == 'Subject' and not metadata_names.has_key('keywords'): |
---|
92 | result['keywords'] = value |
---|
93 | |
---|
94 | if accessor not in ('Description', 'Subject'): |
---|
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 | |
---|
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 | |
---|
140 | return result |
---|
141 | |
---|
142 | PloneTool.listMetaTags = listMetaTags |
---|