source: products/qSEOptimizer/trunk/__init__.py

Last change on this file was 566, checked in by gerrykirk, 17 years ago

removed '.' after Support Team - no need for punctuation

File size: 2.9 KB
Line 
1import Globals
2from Products.CMFCore.DirectoryView import registerDirectory
3from AccessControl import allow_module
4from Products.CMFCore.utils import getToolByName
5from util import SortedDict
6from os import path
7
8from Products.qSEOptimizer import config
9
10try:
11    from Products.qSEOptimizer.interfaces import IKeywords
12    zope_interface_available = True
13except ImportError:
14    zope_interface_available = False
15
16allow_module('Products.qSEOptimizer.util')
17qSEO_globals = globals()
18registerDirectory('skins', qSEO_globals)
19
20try:
21    # for Plone-2.1 and higher
22    from Products.CMFPlone.PloneTool import PloneTool
23    _present = hasattr(PloneTool, "listMetaTags")
24except ImportError:
25    _present = False
26
27def propertyItems(context):
28    """Return a list of (id,property) tuples.
29    """
30    return map(lambda i,c=context: (i['id'],getattr(c,i['id'],None)),
31                                context._properties)
32
33if _present:
34    old_lmt = PloneTool.listMetaTags
35
36    def listMetaTags(self, context):
37        portal_props = getToolByName(context, 'portal_properties')
38        seo_props = getToolByName(portal_props, 'seo_properties', None)
39        if seo_props is None:
40            return old_lmt(self, context)
41
42        site_props = getToolByName(portal_props, 'site_properties')
43        exposeDCMetaTags = site_props.exposeDCMetaTags
44
45        metaTags = SortedDict()
46        metaTags.update(old_lmt(self, context))
47        metadataList = [
48            ('qSEO_Description', 'description'),
49            ('qSEO_Keywords',    'keywords'),
50            ('qSEO_Robots',      'robots'),
51            ('qSEO_Distribution','distribution')]
52
53        if exposeDCMetaTags:
54            metadataList.append(('qSEO_Distribution', 'DC.distribution'))
55
56        for accessor, key in metadataList:
57            method = getattr(context, accessor, None)
58            if not callable(method):
59                # ups
60                continue
61            # Catch AttributeErrors raised by some AT applications
62            try:
63                value = method()
64            except AttributeError:
65                value = None
66
67            if not value:
68                continue
69            if isinstance(value, (tuple, list)):
70                value = ', '.join(value)
71
72            metaTags[key] = value
73
74        # add custom meta tags (added from qseo tab by user) for given context
75        property_prefix = 'qSEO_custom_'
76        for property, value in propertyItems(context):
77            idx = property.find(property_prefix)
78            if idx == 0 and len(property) > len(property_prefix):
79                metaTags[property[len(property_prefix):]] = value
80
81        # Set the additional matching keywords, if any
82        if zope_interface_available:
83            adapter = IKeywords(context, None)
84            if adapter is not None:
85                keywords = adapter.listKeywords()
86                if keywords:
87                    metaTags['keywords'] = keywords
88
89        return metaTags
90
91    PloneTool.listMetaTags = listMetaTags
Note: See TracBrowser for help on using the repository browser.