Back to index

plone3  3.1.7
calendartool.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2006 Zope Corporation and Contributors. All Rights Reserved.
00004 #
00005 # This software is subject to the provisions of the Zope Public License,
00006 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
00007 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00008 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00009 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00010 # FOR A PARTICULAR PURPOSE.
00011 #
00012 ##############################################################################
00013 """Browser views for the portal calendar.
00014 
00015 $Id: calendartool.py 76996 2007-06-24 00:18:49Z hannosch $
00016 """
00017 
00018 from DateTime.DateTime import DateTime
00019 
00020 from Products.CMFDefault.browser.utils import decode
00021 from Products.CMFDefault.browser.utils import memoize
00022 from Products.CMFDefault.browser.utils import ViewBase
00023 
00024 
00025 class CalendarView(ViewBase):
00026 
00027     """ Helper class for calendar-related templates
00028     """
00029 
00030     # helpers
00031 
00032     def _getStartAsString(self, event_brain):
00033         """ Retrieve formatted start string
00034         """
00035         day = self.viewDay()
00036         event_start = event_brain.getObject().start()
00037         first_date = DateTime(day.Date()+" 00:00:00")
00038 
00039         if event_start < first_date:
00040             return event_start.aCommon()[:12]
00041         else:
00042             return event_start.TimeMinutes()
00043 
00044     def _getEndAsString(self, event_brain):
00045         """ Retrieve formatted end string
00046         """
00047         day = self.viewDay()
00048         event_end = event_brain.getObject().end()
00049         last_date = DateTime(day.Date()+" 23:59:59")
00050 
00051         if event_end > last_date:
00052             return event_end.aCommon()[:12]
00053         else:
00054             return event_end.TimeMinutes()
00055 
00056     @memoize
00057     def viewDay(self):
00058         """ Return a DateTime for a passed-in date or today
00059         """
00060         date = self.request.get('date', None) or DateTime().aCommon()[:12]
00061 
00062         return DateTime(date)
00063 
00064     # interface
00065 
00066     @memoize
00067     def formattedDate(self):
00068         """ Return a simple formatted date string
00069         """
00070         return self.viewDay().aCommon()[:12]
00071 
00072     @memoize
00073     def previousDayURL(self):
00074         """ URL to the previous day's view
00075         """
00076         day = self.viewDay()
00077         view_url = self._getViewURL()
00078 
00079         return '%s?date=%s' % (view_url, (day-1).Date())
00080 
00081     @memoize
00082     def nextDayURL(self):
00083         """ URL to the next day's view
00084         """
00085         day = self.viewDay()
00086         view_url = self._getViewURL()
00087 
00088         return '%s?date=%s' % (view_url, (day+1).Date())
00089 
00090     @memoize
00091     @decode
00092     def listItemInfos(self):
00093         """ List item infos for all event catalog records for a specific day.
00094         """
00095         caltool = self._getTool('portal_calendar')
00096         thisDay = self.viewDay()
00097 
00098         items = [ {'title': item.Title,
00099                    'url': item.getURL(),
00100                    'start': self._getStartAsString(item),
00101                    'stop': self._getEndAsString(item)}
00102                   for item in caltool.getEventsForThisDay(thisDay) ]
00103 
00104         return tuple(items)
00105 
00106 
00107 class CalendarBoxView(ViewBase):
00108 
00109     # calendarBox widget helpers
00110 
00111     @memoize
00112     def getMonthAndYear(self):
00113         """ Retrieve month/year tuple
00114         """
00115         caltool = self._getTool('portal_calendar')
00116         current = DateTime()
00117         session = None
00118 
00119         # First priority goes to the data in the request
00120         year = self.request.get('year', None)
00121         month = self.request.get('month', None)
00122 
00123         # Next get the data from the SESSION
00124         if caltool.getUseSession():
00125             session = self.request.get('SESSION', None)
00126             if session:
00127                 if not year:
00128                     year = session.get('calendar_year', None)
00129                 if not month:
00130                     month = session.get('calendar_month', None)
00131 
00132         # Last resort to today
00133         if not year:
00134             year = current.year()
00135         if not month:
00136             month = current.month()
00137 
00138         # Then store the results in the session for next time
00139         if session:
00140             session.set('calendar_year', year)
00141             session.set('calendar_month', month)
00142 
00143         # Finally return the results
00144         return (year, month)
00145 
00146     def getNextMonthLink(self, base_url, month, year):
00147         """ Return URL for the next month link
00148         """
00149         caltool = self._getTool('portal_calendar')
00150         nextMonthTime = caltool.getNextMonth(month, year)
00151 
00152         x = '%s?month:int=%d&year:int=%d' % ( base_url
00153                                             , nextMonthTime.month()
00154                                             , nextMonthTime.year()
00155                                             )
00156 
00157         return x
00158 
00159     def getPreviousMonthLink(self, base_url, month, year):
00160         """ Return URL for the previous month link
00161         """
00162         caltool = self._getTool('portal_calendar')
00163         prevMonthTime = caltool.getPreviousMonth(month, year)
00164 
00165         x = '%s?month:int=%d&year:int=%d' % ( base_url
00166                                             , prevMonthTime.month()
00167                                             , prevMonthTime.year()
00168                                             )
00169 
00170         return x
00171 
00172     def getDaysClass(self, day, month, year, event=None):
00173         """ Determine the CSS class to use for the given day
00174         """
00175         current = DateTime()
00176 
00177         if ( current.year()==year and
00178              current.month()==month and
00179              current.day()==int(day) ):
00180             if event:
00181                 return 'todayevent'
00182             else:
00183                 return 'todaynoevent'
00184 
00185         if event:
00186             return 'event'
00187         else:
00188             return ''