Back to index

plone3  3.1.7
datecomponents.py
Go to the documentation of this file.
00001 from zope.interface import implements
00002 from zope.component import getMultiAdapter
00003 
00004 from Acquisition import aq_inner
00005 from DateTime.DateTime import DateTime
00006 from DateTime.DateTime import DateTimeError
00007 from Products.CMFCore.utils import getToolByName
00008 from Products.Five.browser import BrowserView
00009 
00010 from interfaces import IDateComponents
00011 
00012 CEILING=DateTime(9999, 0)
00013 FLOOR=DateTime(1970, 0)
00014 PLONE_CEILING=DateTime(2021,0) # 2020-12-31
00015 
00016 
00017 def english_month_names():
00018     names={}
00019     for x in range(1,13):
00020         faux=DateTime(2004, x, 1)
00021         names[x]=faux.Month()
00022     return names
00023 
00024 ENGLISH_MONTH_NAMES=english_month_names()
00025 
00026 class DateComponents(BrowserView):
00027     """A view that provides some helper methods useful in date widgets.
00028     """
00029 
00030     implements(IDateComponents)
00031 
00032     def result(self, date=None,
00033                use_ampm=False,
00034                starting_year=None,
00035                ending_year=None,
00036                future_years=None,
00037                minute_step=5):
00038         """Returns a dict with date information.
00039         """
00040 
00041         ptool = getToolByName(self.context, 'portal_properties')
00042 
00043         site_props = ptool.site_properties
00044 
00045         # Get the date format from the locale
00046         context = aq_inner(self.context)
00047         portal_state = getMultiAdapter((context, self.request), name=u'plone_portal_state')
00048 
00049         locale = portal_state.locale()
00050         dates = portal_state.locale().dates
00051 
00052         timepattern = dates.getFormatter('time').getPattern()
00053         if 'a' in timepattern:
00054              use_ampm = True
00055         month_names = dates.getFormatter('date').calendar.months
00056 
00057         # 'id' is what shows up.  December for month 12. 
00058         # 'value' is the value for the form.
00059         # 'selected' is whether or not it is selected.
00060 
00061         default=0
00062         years=[]
00063         days=[]
00064         months=[]
00065         hours=[]
00066         minutes=[]
00067         ampm=[]
00068         now=DateTime()
00069 
00070         # This debacle is because the date that is usually passed in ends with GMT
00071         # and of course DateTime is too stupid to handle it.  So we strip it off.
00072 
00073         if isinstance(date, basestring):
00074             date=date.strip()
00075             if not date:
00076                 date=None
00077             elif date.split(' ')[-1].startswith('GMT'):
00078                 date=' '.join(date.split(' ')[:-1])
00079 
00080         if date is None:
00081             date=now
00082             default=1
00083         elif not isinstance(date, DateTime):
00084             try:
00085                 date=DateTime(date)
00086             except (TypeError, DateTimeError):
00087                 date=now
00088                 default=1
00089 
00090         # Anything above PLONE_CEILING should be PLONE_CEILING
00091         if date.greaterThan(PLONE_CEILING):
00092             date = PLONE_CEILING
00093 
00094         # Get portal year range
00095         if starting_year is None:
00096             min_year = site_props.getProperty('calendar_starting_year', 1999)
00097         else:
00098             min_year = int(starting_year)
00099         if ending_year is None:
00100             if future_years is None:
00101                 max_year = site_props.getProperty('calendar_future_years_available', 5) + now.year()
00102             else:
00103                 max_year = int(future_years) + now.year()
00104         else:
00105             max_year = int(ending_year)
00106 
00107         # keeps the existing date if it's out of range
00108         if not default:
00109             if min_year > date.year():
00110                 min_year = date.year()
00111             if max_year < date.year():
00112                 max_year = date.year()
00113 
00114         year=int(date.strftime('%Y'))
00115 
00116         if default:
00117             years.append({'id': '--', 'value': '0000', 'selected': 1})
00118         else:
00119             years.append({'id': '--', 'value': '0000', 'selected': None})
00120 
00121         for x in range(min_year, max_year+1):
00122             d={'id': x, 'value': x, 'selected': None}
00123             if x==year and not default:
00124                 d['selected']=1
00125             years.append(d)
00126 
00127         month=int(date.strftime('%m'))
00128 
00129         if default:
00130             months.append({'id': '--', 'value': '00', 'selected': 1, 'title': '--'})
00131         else:
00132             months.append({'id': '--', 'value': '00', 'selected': None, 'title': '--'})
00133 
00134         for x in range(1, 13):
00135             d={'id': ENGLISH_MONTH_NAMES[x], 'value': '%02d' % x, 'selected': None}
00136             if x==month and not default:
00137                 d['selected']=1
00138             d['title']=month_names[x][0]
00139             months.append(d)
00140 
00141         day=int(date.strftime('%d'))
00142 
00143         if default:
00144             days.append({'id': '--', 'value': '00', 'selected': 1})
00145         else:
00146             days.append({'id': '--', 'value': '00', 'selected': None})
00147 
00148         for x in range(1, 32):
00149             d={'id': x, 'value': '%02d' % x, 'selected': None}
00150             if x==day and not default:
00151                 d['selected']=1
00152             days.append(d)
00153 
00154         if use_ampm:
00155             hours_range=[12]+range(1,12)
00156             hour_default='12'
00157             hour=int(date.strftime('%I'))
00158         else:
00159             hours_range=range(0,24)
00160             hour_default='00'
00161             hour=int(date.strftime('%H'))
00162 
00163         if default:
00164             hours.append({'id': '--', 'value': hour_default, 'selected': 1})
00165         else:
00166             hours.append({'id': '--', 'value': hour_default, 'selected': None})
00167 
00168         for x in hours_range:
00169             d={'id': '%02d' % x, 'value': '%02d' % x, 'selected': None }
00170             if x==hour and not default:
00171                 d['selected']=1
00172             hours.append(d)
00173 
00174         minute=int(date.strftime('%M'))
00175 
00176         if default:
00177             minutes.append({'id': '--', 'value': '00', 'selected': 1})
00178         else:
00179             minutes.append({'id': '--', 'value': '00', 'selected': None})
00180 
00181         for x in range(0, 60, minute_step):
00182             d={'id': '%02d' % x, 'value': '%02d' % x, 'selected': None}
00183             if (x==minute or minute < x < minute+minute_step) and not default:
00184                 d['selected']=1
00185             minutes.append(d)
00186 
00187         if use_ampm:
00188             p=date.strftime('%p')
00189 
00190             if default:
00191                 ampm.append({'id': '--', 'value': 'AM', 'selected': 1})
00192             else:
00193                 ampm.append({'id': '--', 'value': 'AM', 'selected': None})
00194 
00195             for x in ('AM', 'PM'):
00196                 d={'id': x, 'value': x, 'selected': None}
00197                 if x==p and not default:
00198                     d['selected']=1
00199                 ampm.append(d)
00200 
00201         return {'years': years, 'months': months, 'days': days,
00202                 'hours': hours, 'minutes': minutes, 'ampm': ampm}