Back to index

plone3  3.1.7
recent.py
Go to the documentation of this file.
00001 from zope import schema
00002 from zope.component import getMultiAdapter
00003 from zope.formlib import form
00004 from zope.interface import implements
00005 
00006 from plone.app.portlets.portlets import base
00007 from plone.memoize import ram
00008 from plone.memoize.compress import xhtml_compress
00009 from plone.memoize.instance import memoize
00010 from plone.portlets.interfaces import IPortletDataProvider
00011 from plone.app.portlets.cache import render_cachekey
00012 
00013 from Acquisition import aq_inner
00014 from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
00015 from Products.CMFPlone import PloneMessageFactory as _
00016 
00017 class IRecentPortlet(IPortletDataProvider):
00018 
00019     count = schema.Int(title=_(u'Number of items to display'),
00020                        description=_(u'How many items to list.'),
00021                        required=True,
00022                        default=5)
00023 
00024 class Assignment(base.Assignment):
00025     implements(IRecentPortlet)
00026 
00027     def __init__(self, count=5):
00028         self.count = count
00029 
00030     @property
00031     def title(self):
00032         return _(u"Recent items")
00033 
00034 def _render_cachekey(fun, self):
00035     if self.anonymous:
00036         raise ram.DontCache()
00037     return render_cachekey(fun, self)
00038 
00039 class Renderer(base.Renderer):
00040     _template = ViewPageTemplateFile('recent.pt')
00041 
00042     def __init__(self, *args):
00043         base.Renderer.__init__(self, *args)
00044 
00045         context = aq_inner(self.context)
00046         portal_state = getMultiAdapter((context, self.request), name=u'plone_portal_state')
00047         self.anonymous = portal_state.anonymous()
00048         self.portal_url = portal_state.portal_url()
00049         self.typesToShow = portal_state.friendly_types()
00050 
00051         plone_tools = getMultiAdapter((context, self.request), name=u'plone_tools')
00052         self.catalog = plone_tools.catalog()
00053         
00054     @ram.cache(_render_cachekey)
00055     def render(self):
00056         return xhtml_compress(self._template())
00057 
00058     @property
00059     def available(self):
00060         return not self.anonymous and len(self._data())
00061 
00062     def recent_items(self):
00063         return self._data()
00064 
00065     def recently_modified_link(self):
00066         return '%s/recently_modified' % self.portal_url
00067 
00068     @memoize
00069     def _data(self):
00070         limit = self.data.count
00071         return self.catalog(portal_type=self.typesToShow,
00072                             sort_on='modified',
00073                             sort_order='reverse',
00074                             sort_limit=limit)[:limit]
00075 
00076 
00077 class AddForm(base.AddForm):
00078     form_fields = form.Fields(IRecentPortlet)
00079     label = _(u"Add Recent Portlet")
00080     description = _(u"This portlet displays recently modified content.")
00081 
00082     def create(self, data):
00083         return Assignment(count=data.get('count', 5))
00084 
00085 class EditForm(base.EditForm):
00086     form_fields = form.Fields(IRecentPortlet)
00087     label = _(u"Edit Recent Portlet")
00088     description = _(u"This portlet displays recently modified content.")