Back to index

plone3  3.1.7
calendar.py
Go to the documentation of this file.
00001 from Acquisition import aq_inner
00002 import zope.deprecation
00003 from zope.i18nmessageid import MessageFactory
00004 from zope.interface import implements
00005 
00006 from DateTime import DateTime
00007 from Products.Five import BrowserView
00008 from Products.CMFCore.utils import getToolByName
00009 from Products.CMFPlone.browser.interfaces import ICalendarPortlet
00010 
00011 PLMF = MessageFactory('plonelocales')
00012 
00013 class CalendarPortlet(BrowserView):
00014     implements(ICalendarPortlet)
00015 
00016     def __init__(self, context, request):
00017         self.context = context
00018         self.request = request
00019         self.yearmonth = self.getYearAndMonthToDisplay()
00020         self.DateTime = DateTime
00021         self.current = DateTime()
00022         self.current_day = self.current.day()
00023         self.nextYearMax = self.current + 365
00024         self.prevYearMin = self.current - 365
00025         self.year = self.yearmonth[0]
00026         self.month = self.yearmonth[1]
00027         self.prevMonthTime = self.getPreviousMonth(self.month, self.year)
00028         self.nextMonthTime = self.getNextMonth(self.month, self.year)
00029         calendar = getToolByName(context, 'portal_calendar')
00030         self.weeks = calendar.getEventsForCalendar(self.month, self.year)
00031         self.daynumbers = calendar.getDayNumbers()
00032         self._translation_service = getToolByName(context, 'translation_service')
00033         self.showStates = calendar.getCalendarStates()
00034         self.showPrevMonth = self.yearmonth > (self.prevYearMin.year(), self.prevYearMin.month())
00035         self.showNextMonth = self.yearmonth < (self.nextYearMax.year(), self.nextYearMax.month())
00036 
00037     def getYearAndMonthToDisplay(self):
00038         """ from skins/plone_scripts/getYearAndMonthToDisplay.py """
00039 
00040         current = DateTime()
00041         context = aq_inner(self.context)
00042         request = self.request
00043         session = None
00044 
00045         # First priority goes to the data in the REQUEST
00046         year = request.get('year', None)
00047         month = request.get('month', None)
00048 
00049         # Next get the data from the SESSION
00050         if context.portal_calendar.getUseSession():
00051             session = request.get('SESSION', None)
00052             if session:
00053                 if not year:
00054                     year = session.get('calendar_year', None)
00055                 if not month:
00056                     month = session.get('calendar_month', None)
00057 
00058         # Last resort to today
00059         if not year:
00060             year = current.year()
00061         if not month:
00062             month = current.month()
00063 
00064         year, month = int(year), int(month)
00065 
00066         # Store the results in the session for next time
00067         if session:
00068             session.set('calendar_year', year)
00069             session.set('calendar_month', month)
00070 
00071         # Finally return the results
00072         return year, month
00073 
00074     def getPreviousMonth(self, month, year):
00075 
00076         month=int(month)
00077         year=int(year)
00078 
00079         if month==0 or month==1:
00080             month, year = 12, year - 1
00081         else:
00082             month-=1
00083 
00084         return DateTime(year, month, 1)
00085 
00086     def getNextMonth(self, month, year):
00087 
00088         month=int(month)
00089         year=int(year)
00090 
00091         if month==12:
00092             month, year = 1, year + 1
00093         else:
00094             month+=1
00095 
00096         return DateTime(year, month, 1)
00097 
00098     def getWeekdays(self):
00099         """Returns a list of Messages for the weekday names."""
00100 
00101         weekdays = []
00102         # list of ordered weekdays as numbers
00103         for day in self.daynumbers:
00104             msgid   = self._translation_service.day_msgid(day, format='s')
00105             english = self._translation_service.weekday_english(day, format='a')
00106             weekdays.append(PLMF(msgid, default=english))
00107 
00108         return weekdays
00109 
00110     def getEnglishMonthName(self):
00111         """Returns the current English month name."""
00112         return self._translation_service.month_english(self.month)
00113 
00114     def getMonthName(self):
00115         """Returns the current month name as a Message."""
00116         msgid   = self._translation_service.month_msgid(self.month)
00117         english = self._translation_service.month_english(self.month)
00118         return PLMF(msgid, default=english)
00119 
00120     def isToday(self, day):
00121         """Returns True if the given day and the current month and year equals
00122            today, otherwise False.
00123         """
00124         return self.current_day==day and self.current.month()==self.month and \
00125                self.current.year()==self.year
00126 
00127 zope.deprecation.deprecated(
00128   ('CalendarPortlet', ),
00129    "Plone's portlets are based on plone.app.portlets now. The old portlets "
00130    "will be removed in Plone 4.0."
00131   )