source: products/quintagroup.seoptimizer/branches/refactoring2.3.0/quintagroup/seoptimizer/adapters.py @ 1896

Last change on this file since 1896 was 1855, checked in by mylan, 14 years ago

#156: Refuse from stright depending from IPropertyManager interface and leave only ITraversable one, update adapter registration

  • Property svn:eol-style set to native
File size: 2.9 KB
Line 
1import re, commands
2
3from zope.component import adapts
4from zope.interface import implements
5from zope.component import queryMultiAdapter
6
7from Acquisition import aq_inner
8from OFS.interfaces import IPropertyManager
9from Products.CMFCore.utils import getToolByName
10
11from quintagroup.seoptimizer.util import SortedDict
12from quintagroup.seoptimizer.interfaces import IMetaKeywords, IMappingMetaTags
13
14METADATA_MAPS = dict([
15    ("DC.publisher", "Publisher"),
16    ("DC.description", "Description"),
17    ("DC.contributors", "Contributors"),
18    ("DC.creator", "Creator"),
19    ("DC.format", "Format"),
20    ("DC.rights", "Rights"),
21    ("DC.language", "Language"),
22    ("DC.date.modified", "ModificationDate"),
23    ("DC.date.created", "CreationDate"),
24    ("DC.type", "Type"),
25    ("DC.subject", "Subject"),
26    ("DC.distribution", "seo_distribution"),
27    ("description", "seo_description"),
28    ("keywords", "meta_keywords"),
29    ("robots", "seo_robots"),
30    ("distribution", "seo_distribution")])
31
32class MetaKeywordsAdapter(object):
33    implements(IMetaKeywords)
34
35    def __init__(self, context):
36        self.context = context
37
38    def getMetaKeywords(self):
39        """ See interface.
40        """
41        request = self.context.REQUEST
42        meta_keywords = []
43        filtered_keywords = []
44        portal_props = getToolByName(self.context, 'portal_properties')
45        seo_props = getToolByName(portal_props, 'seo_properties', None)
46        seo_context = queryMultiAdapter((self.context, request), name='seo_context')
47        if seo_context:
48            meta_keywords = list(seo_context['meta_keywords'])
49        return ', '.join(meta_keywords)
50
51
52class MappingMetaTags(object):
53    implements(IMappingMetaTags)
54
55    def __init__(self, context):
56        self.context = context
57        self.portal_props = getToolByName(self.context, 'portal_properties')
58        self.seo_props = getToolByName(self.portal_props, 'seo_properties', None)
59
60    def getMappingMetaTags(self):
61        """ See interface.
62        """
63        metadata_name = SortedDict()
64        if self.seo_props:
65            pmn = self.seo_props.getProperty('metatags_order', ())
66            for mt in pmn:
67                if METADATA_MAPS.has_key(mt):
68                    metadata_name[mt] = METADATA_MAPS[mt]
69        return metadata_name
70
71
72class canonicalPathAdapter(object):
73    """Adapts base content to canonical path, with taking into consideration
74       SEO canonical path value.
75    """
76
77    def __init__(self, context):
78        self.context = aq_inner(context)
79
80    def canonical_path(self):
81        purl = getToolByName(self.context,'portal_url')
82
83        # Calculate canonical path from qSEO_canonical property
84        if IPropertyManager.providedBy(self.context):
85            prop = self.context.getProperty('qSEO_canonical', None)
86            if prop is not None:
87                return prop[len(purl()):]
88
89        # Fallback for canonical path calculation
90        return '/'+'/'.join(purl.getRelativeContentPath(self.context))
Note: See TracBrowser for help on using the repository browser.