Back to index

plone3  3.1.7
events.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.instance import memoize
00008 from plone.memoize import ram
00009 from plone.memoize.compress import xhtml_compress
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 DateTime.DateTime import DateTime
00015 from Products.CMFCore.utils import getToolByName
00016 from Products.CMFPlone import PloneMessageFactory as _
00017 from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
00018 
00019 class IEventsPortlet(IPortletDataProvider):
00020 
00021     count = schema.Int(title=_(u'Number of items to display'),
00022                        description=_(u'How many items to list.'),
00023                        required=True,
00024                        default=5)
00025 
00026     state = schema.Tuple(title=_(u"Workflow state"),
00027                          description=_(u"Items in which workflow state to show."),
00028                          default=('published', ),
00029                          required=True,
00030                          value_type=schema.Choice(
00031                              vocabulary="plone.app.vocabularies.WorkflowStates")
00032                          )
00033 
00034 class Assignment(base.Assignment):
00035     implements(IEventsPortlet)
00036 
00037     def __init__(self, count=5, state=('published', )):
00038         self.count = count
00039         self.state = state
00040 
00041     @property
00042     def title(self):
00043         return _(u"Events")
00044 
00045 class Renderer(base.Renderer):
00046 
00047     _template = ViewPageTemplateFile('events.pt')
00048 
00049     def __init__(self, *args):
00050         base.Renderer.__init__(self, *args)
00051 
00052         portal_state = getMultiAdapter((self.context, self.request), name=u'plone_portal_state')
00053         self.portal_url = portal_state.portal_url()
00054         self.portal = portal_state.portal()
00055 
00056         self.have_events_folder = 'events' in self.portal.objectIds()
00057 
00058     @ram.cache(render_cachekey)
00059     def render(self):
00060         return xhtml_compress(self._template())
00061 
00062     @property
00063     def available(self):
00064         return len(self._data())
00065 
00066     def published_events(self):
00067         return self._data()
00068 
00069     def all_events_link(self):
00070         if self.have_events_folder:
00071             return '%s/events' % self.portal_url
00072         else:
00073             return '%s/events_listing' % self.portal_url
00074 
00075     def prev_events_link(self):
00076         if (self.have_events_folder and
00077             'aggregator' in self.portal['events'].objectIds() and
00078             'previous' in self.portal['events']['aggregator'].objectIds()):
00079             return '%s/events/aggregator/previous' % self.portal_url
00080             
00081         elif (self.have_events_folder and
00082             'previous' in self.portal['events'].objectIds()):
00083             return '%s/events/previous' % self.portal_url
00084         else:
00085             return None
00086 
00087     @memoize
00088     def _data(self):
00089         context = aq_inner(self.context)
00090         catalog = getToolByName(context, 'portal_catalog')
00091         limit = self.data.count
00092         state = self.data.state
00093         return catalog(portal_type='Event',
00094                        review_state=state,
00095                        end={'query': DateTime(),
00096                             'range': 'min'},
00097                        sort_on='start',
00098                        sort_limit=limit)[:limit]
00099 
00100 class AddForm(base.AddForm):
00101     form_fields = form.Fields(IEventsPortlet)
00102     label = _(u"Add Events Portlet")
00103     description = _(u"This portlet lists upcoming Events.")
00104 
00105     def create(self, data):
00106         return Assignment(count=data.get('count', 5), state=data.get('state', ('published',)))
00107 
00108 class EditForm(base.EditForm):
00109     form_fields = form.Fields(IEventsPortlet)
00110     label = _(u"Edit Events Portlet")
00111     description = _(u"This portlet lists upcoming Events.")