Back to index

plone3  3.1.7
news.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.CMFCore.utils import getToolByName
00016 from Products.CMFPlone import PloneMessageFactory as _
00017 
00018 class INewsPortlet(IPortletDataProvider):
00019 
00020     count = schema.Int(title=_(u'Number of items to display'),
00021                        description=_(u'How many items to list.'),
00022                        required=True,
00023                        default=5)
00024 
00025     state = schema.Tuple(title=_(u"Workflow state"),
00026                          description=_(u"Items in which workflow state to show."),
00027                          default=('published', ),
00028                          required=True,
00029                          value_type=schema.Choice(
00030                              vocabulary="plone.app.vocabularies.WorkflowStates")
00031                          )
00032 
00033 class Assignment(base.Assignment):
00034     implements(INewsPortlet)
00035 
00036     def __init__(self, count=5, state=('published', )):
00037         self.count = count
00038         self.state = state
00039 
00040     @property
00041     def title(self):
00042         return _(u"News")
00043 
00044 class Renderer(base.Renderer):
00045 
00046     _template = ViewPageTemplateFile('news.pt')
00047 
00048     @ram.cache(render_cachekey)
00049     def render(self):
00050         return xhtml_compress(self._template())
00051 
00052     @property
00053     def available(self):
00054         return len(self._data())
00055 
00056     def published_news_items(self):
00057         return self._data()
00058 
00059     def all_news_link(self):
00060         portal_state = getMultiAdapter((self.context, self.request), name=u'plone_portal_state')
00061         portal_url = portal_state.portal_url()
00062         portal = portal_state.portal()
00063         
00064         if 'news' in portal.objectIds():
00065             return '%s/news' % portal_url
00066         else:
00067             return None
00068 
00069     @memoize
00070     def _data(self):
00071         context = aq_inner(self.context)
00072         catalog = getToolByName(context, 'portal_catalog')
00073         limit = self.data.count
00074         state = self.data.state
00075         return catalog(portal_type='News Item',
00076                        review_state=state,
00077                        sort_on='Date',
00078                        sort_order='reverse',
00079                        sort_limit=limit)[:limit]
00080 
00081 class AddForm(base.AddForm):
00082     form_fields = form.Fields(INewsPortlet)
00083     label = _(u"Add News Portlet")
00084     description = _(u"This portlet displays recent News Items.")
00085 
00086     def create(self, data):
00087         return Assignment(count=data.get('count', 5), state=data.get('state', ('published',)))
00088 
00089 class EditForm(base.EditForm):
00090     form_fields = form.Fields(INewsPortlet)
00091     label = _(u"Edit News Portlet")
00092     description = _(u"This portlet displays recent News Items.")