source: products/quintagroup.seoptimizer/trunk/quintagroup/seoptimizer/browser/viewlets.py @ 1647

Last change on this file since 1647 was 1509, checked in by liebster, 14 years ago

Added doc strings.

  • Property svn:eol-style set to native
File size: 2.8 KB
Line 
1from cgi import escape
2from zope.component import getMultiAdapter
3from Products.CMFPlone.utils import safe_unicode
4from plone.app.layout.viewlets.common import ViewletBase
5from Products.CMFCore.utils import getToolByName
6
7
8class TitleCommentViewlet(ViewletBase):
9    """ Simple viewlet for custom title rendering.
10    """
11
12    def update(self):
13        self.portal_state = getMultiAdapter((self.context, self.request),
14                                            name=u'plone_portal_state')
15        self.context_state = getMultiAdapter((self.context, self.request),
16                                             name=u'plone_context_state')
17        self.page_title = self.context_state.object_title
18        self.portal_title = self.portal_state.portal_title
19
20        self.override_title = self.context.hasProperty('qSEO_title')
21        self.override_comments = self.context.hasProperty('qSEO_html_comment')
22
23    def std_title(self):
24        portal_title = safe_unicode(self.portal_title())
25        page_title = safe_unicode(self.page_title())
26        if page_title == portal_title:
27            return u"<title>%s</title>" % (escape(portal_title))
28        else:
29            return u"<title>%s &mdash; %s</title>" % (
30                escape(safe_unicode(page_title)),
31                escape(safe_unicode(portal_title)))
32
33    def render(self):
34        std_title = self.std_title()
35        seo_context = getMultiAdapter((self.context, self.request), name='seo_context')
36        if not self.override_title:
37            if not self.override_comments:
38                return std_title
39            else:
40                qseo_comments = u"<!--%s-->"%safe_unicode(seo_context.seo_html_comment())
41                return u"%s\n%s"%(std_title, qseo_comments)
42        else:
43            qseo_title = u"<title>%s</title>" % safe_unicode(seo_context.seo_title())
44            if not self.override_comments:
45                return qseo_title
46            else:
47                qseo_comments = u"<!--%s-->"%safe_unicode(seo_context.seo_html_comment())
48                return u"%s\n%s"%(qseo_title, qseo_comments)
49
50
51class CustomScriptViewlet( ViewletBase ):
52    """ Simple viewlet for custom script rendering.
53    """
54    def getCustomScript( self ):
55        context = self.context.aq_inner
56        portal_props = getToolByName(context, 'portal_properties')
57        seo_props = getToolByName(portal_props, 'seo_properties', None)
58        if seo_props is None:
59            return '' 
60        return seo_props.getProperty('custom_script', '')
61
62    def render( self ):
63        return safe_unicode("""%s"""% self.getCustomScript())
64
65
66class CanonicalUrlViewlet( ViewletBase ):
67    """ Simple viewlet for canonical url link rendering.
68    """
69
70    def render( self ):
71        seo_context = getMultiAdapter((self.context, self.request), name='seo_context')
72        return """<link rel="canonical" href="%s" />""" % seo_context.seo_canonical()
Note: See TracBrowser for help on using the repository browser.