source: products/quintagroup.seoptimizer/trunk/quintagroup/seoptimizer/__init__.py @ 1458

Last change on this file since 1458 was 1317, checked in by piv, 15 years ago

added pop method in SortedDict?

  • Property svn:eol-style set to native
File size: 4.8 KB
Line 
1from AccessControl import allow_module
2from zope.component import queryMultiAdapter
3
4from Acquisition import aq_inner
5from DateTime import DateTime
6
7from Products.CMFCore.utils import getToolByName
8
9from quintagroup.seoptimizer.interfaces import IKeywords, IMappingMetaTags
10from quintagroup.seoptimizer.util import SortedDict
11
12allow_module('quintagroup.seoptimizer.util')
13qSEO_globals = globals()
14
15
16try:
17    from Products.CMFPlone.PloneTool import PloneTool, METADATA_DCNAME, \
18        FLOOR_DATE, CEILING_DATE
19    _present = hasattr(PloneTool, "listMetaTags")
20except ImportError:
21    _present = False
22
23
24if _present:
25    old_lmt = PloneTool.listMetaTags
26
27    def listMetaTags(self, context):
28        """Lists meta tags helper.
29
30        Creates a mapping of meta tags.
31        """
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()
38        site_props = getToolByName(self, 'portal_properties').site_properties
39        use_all = site_props.getProperty('exposeDCMetaTags', None)
40
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
45        if not use_all:
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']
49        else:
50            metadata_names = mapping_metadata
51
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
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
85            # Special cases
86            if accessor == 'Description' and not metadata_names.has_key('description'):
87                result['description'] = value
88            elif accessor == 'Subject' and not metadata_names.has_key('keywords'):
89                result['keywords'] = value
90
91            if accessor not in ('Description', 'Subject'):
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
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
137        return result
138
139    PloneTool.listMetaTags = listMetaTags
Note: See TracBrowser for help on using the repository browser.