source: products/qSEOptimizer/branches/additional-keywords/__init__.py @ 1

Last change on this file since 1 was 1, checked in by myroslav, 18 years ago

Building directory structure

File size: 2.4 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
27if _present:
28    old_lmt = PloneTool.listMetaTags
29
30    def listMetaTags(self, context):
31        portal_props = getToolByName(context, 'portal_properties')
32        seo_props = getToolByName(portal_props, 'seo_properties', None)
33        if seo_props is None:
34            return old_lmt(self, context)
35
36        site_props = getToolByName(portal_props, 'site_properties')
37        exposeDCMetaTags = site_props.exposeDCMetaTags
38
39        metaTags = SortedDict()
40        metaTags.update(old_lmt(self, context))
41        metadataList = [
42            ('qSEO_Description', 'description'),
43            ('qSEO_Keywords',    'keywords'),
44            ('qSEO_Robots',      'robots'),
45            ('qSEO_Distribution','distribution')]
46
47        if exposeDCMetaTags:
48            metadataList.append(('qSEO_Distribution', 'DC.distribution'))
49
50        for accessor, key in metadataList:
51            method = getattr(context, accessor, None)
52            if not callable(method):
53                # ups
54                continue
55            # Catch AttributeErrors raised by some AT applications
56            try:
57                value = method()
58            except AttributeError:
59                value = None
60
61            if not value:
62                continue
63            if isinstance(value, (tuple, list)):
64                value = ', '.join(value)
65
66            metaTags[key] = value
67
68        # Set the additional matching keywords, if any
69        if zope_interface_available:
70            adapter = IKeywords(context, None)
71            if adapter is not None:
72                keywords = adapter.listKeywords()
73                if keywords:
74                    metaTags['keywords'] = keywords
75
76        return metaTags
77
78    PloneTool.listMetaTags = listMetaTags
Note: See TracBrowser for help on using the repository browser.