Back to index

plone3  3.1.7
viewlets.py
Go to the documentation of this file.
00001 from StringIO import StringIO
00002 
00003 from plone.memoize import ram
00004 from plone.memoize.compress import xhtml_compress
00005 
00006 from zope.component import getMultiAdapter
00007 from zope.app.pagetemplate.viewpagetemplatefile import ViewPageTemplateFile
00008 
00009 from Acquisition import aq_inner
00010 
00011 from plone.app.layout.viewlets import ViewletBase
00012 
00013 from Products.CMFCore.utils import getToolByName
00014 
00015 
00016 def get_language(context, request):
00017     portal_state = getMultiAdapter((context, request),
00018                                    name=u'plone_portal_state')
00019     return portal_state.locale().getLocaleID()
00020 
00021 
00022 def render_cachekey(fun, self):
00023     key = StringIO()
00024     # Include the name of the viewlet as the underlying cache key only
00025     # takes the module and function name into account, but not the class
00026     print >> key, self.__name__
00027     print >> key, self.site_url
00028     print >> key, get_language(aq_inner(self.context), self.request)
00029 
00030     return key.getvalue()
00031 
00032 
00033 class FaviconViewlet(ViewletBase):
00034 
00035     _template = ViewPageTemplateFile('favicon.pt')
00036 
00037     @ram.cache(render_cachekey)
00038     def render(self):
00039         return xhtml_compress(self._template())
00040 
00041 
00042 class SearchViewlet(ViewletBase):
00043 
00044     _template = ViewPageTemplateFile('search.pt')
00045 
00046     @ram.cache(render_cachekey)
00047     def render(self):
00048         return xhtml_compress(self._template())
00049 
00050 
00051 class AuthorViewlet(ViewletBase):
00052 
00053     render = ViewPageTemplateFile('author.pt')
00054 
00055 
00056 class NavigationViewlet(ViewletBase):
00057 
00058     _template = ViewPageTemplateFile('navigation.pt')
00059 
00060     @ram.cache(render_cachekey)
00061     def render(self):
00062         return xhtml_compress(self._template())
00063 
00064 class RSSViewlet(ViewletBase):
00065     def update(self):
00066         super(RSSViewlet, self).update()
00067         syntool = getToolByName(self.context, 'portal_syndication')
00068         if syntool.isSyndicationAllowed(self.context):
00069             self.allowed = True
00070             context_state = getMultiAdapter((self.context, self.request),
00071                                             name=u'plone_context_state')
00072             self.url = '%s/RSS' % context_state.object_url()
00073         else:
00074             self.allowed = False
00075 
00076     render = ViewPageTemplateFile('rsslink.pt')