Back to index

plone3  3.1.7
date_components_support.py
Go to the documentation of this file.
00001 ## Script (Python) "date_components_support"
00002 ##bind container=container
00003 ##bind context=context
00004 ##bind namespace=
00005 ##bind script=script
00006 ##bind subpath=traverse_subpath
00007 ##parameters=date=None, use_ampm=0, starting_year=None, ending_year=None, future_years=None, minute_step=5
00008 ##title=
00009 ##
00010 
00011 from DateTime.DateTime import DateTimeError
00012 
00013 # 'id' is what shows up.  December for month 12. 
00014 # 'value' is the value for the form.
00015 # 'selected' is whether or not it is selected.
00016 
00017 default=0
00018 years=[]
00019 days=[]
00020 months=[]
00021 hours=[]
00022 minutes=[]
00023 ampm=[]
00024 now=DateTime()
00025 
00026 # from CMFDefault.DublinCore
00027 CEILING=DateTime(9999, 0)
00028 FLOOR=DateTime(1970, 0)
00029 PLONE_CEILING=DateTime(2021,0) # 2020-12-31
00030 
00031 def month_names():
00032     names={}
00033     for x in range(1,13):
00034         faux=DateTime(2004, x, 1)
00035         names[x]=faux.Month()
00036     return names
00037 
00038 month_dict=month_names()
00039 
00040 # This debacle is because the date that is usually passed in ends with GMT
00041 # and of course DateTime is too stupid to handle it.  So we strip it off.
00042 
00043 if same_type(date, ''):
00044     date=date.strip()
00045     if not date:
00046         date=None
00047     elif date.split(' ')[-1].startswith('GMT'):
00048         date=' '.join(date.split(' ')[:-1])
00049 
00050 if date is None:
00051     date=now
00052     default=1
00053 elif not same_type(date, now):
00054     try:
00055         date=DateTime(date)
00056     except (TypeError, DateTimeError):
00057         date=now
00058         default=1
00059 
00060 # Anything above PLONE_CEILING should be PLONE_CEILING
00061 if date.greaterThan(PLONE_CEILING):
00062     date = PLONE_CEILING
00063 
00064 # Get portal year range
00065 site_properties = context.portal_properties.site_properties
00066 if starting_year is None:
00067     min_year = site_properties.getProperty('calendar_starting_year', 1999)
00068 else:
00069     min_year = starting_year
00070 if ending_year is None:
00071     if future_years is None:
00072         max_year = site_properties.getProperty('calendar_future_years_available', 5) + now.year()
00073     else:
00074         max_year = future_years + now.year()
00075 else:
00076     max_year = ending_year
00077 
00078 year=int(date.strftime('%Y'))
00079 
00080 if min_year != max_year:
00081     years.append({'id': '--', 'value': '0000', 'selected': None})
00082 
00083 for x in range(min_year, max_year+1):
00084     d={'id': x, 'value': x, 'selected': None}
00085     if x==year:
00086         d['selected']=1
00087     years.append(d)
00088 
00089 month=int(date.strftime('%m'))
00090 
00091 if default:
00092     months.append({'id': '--', 'value': '00', 'selected': 1})
00093 else:
00094     months.append({'id': '--', 'value': '00', 'selected': None})
00095 
00096 for x in range(1, 13):
00097     d={'id': month_dict[x], 'value': '%02d' % x, 'selected': None}
00098     if x==month and not default:
00099         d['selected']=1
00100     months.append(d)
00101 
00102 day=int(date.strftime('%d'))
00103 
00104 if default:
00105     days.append({'id': '--', 'value': '00', 'selected': 1})
00106 else:
00107     days.append({'id': '--', 'value': '00', 'selected': None})
00108 
00109 for x in range(1, 32):
00110     d={'id': x, 'value': '%02d' % x, 'selected': None}
00111     if x==day and not default:
00112         d['selected']=1
00113     days.append(d)
00114 
00115 if use_ampm:
00116     hours_range=[12]+range(1,12)
00117     hour_default='12'
00118     hour=int(date.strftime('%I'))
00119 else:
00120     hours_range=range(0,24)
00121     hour_default='00'
00122     hour=int(date.strftime('%H'))
00123 
00124 if default:
00125     hours.append({'id': '--', 'value': hour_default, 'selected': 1})
00126 else:
00127     hours.append({'id': '--', 'value': hour_default, 'selected': None})
00128 
00129 for x in hours_range:
00130     d={'id': '%02d' % x, 'value': '%02d' % x, 'selected': None }
00131     if x==hour and not default:
00132         d['selected']=1
00133     hours.append(d)
00134 
00135 minute=int(date.strftime('%M'))
00136 
00137 if default:
00138     minutes.append({'id': '--', 'value': '00', 'selected': 1})
00139 else:
00140     minutes.append({'id': '--', 'value': '00', 'selected': None})
00141 
00142 for x in range(0, 60, minute_step):
00143     d={'id': '%02d' % x, 'value': '%02d' % x, 'selected': None}
00144     if (x==minute or minute < x < minute+minute_step) and not default:
00145         d['selected']=1
00146     minutes.append(d)
00147 
00148 if use_ampm:
00149     p=date.strftime('%p')
00150 
00151     if default:
00152         ampm.append({'id': '--', 'value': 'AM', 'selected': 1})
00153     else:
00154         ampm.append({'id': '--', 'value': 'AM', 'selected': None})
00155 
00156     for x in ('AM', 'PM'):
00157         d={'id': x, 'value': x, 'selected': None}
00158         if x==p and not default:
00159             d['selected']=1
00160         ampm.append(d)
00161 
00162 return {'years': years, 'months': months, 'days': days,
00163         'hours': hours, 'minutes': minutes, 'ampm': ampm}