- Timestamp:
- 13 Apr 2012, 16:51:25 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.kofa/trunk/src/waeup/kofa/widgets/datewidget.py
r7668 r8147 20 20 """ 21 21 from datetime import datetime 22 from zope.formlib.i18n import _23 22 from zope.formlib.interfaces import ConversionError, IDisplayWidget 24 from zope.formlib.textwidgets import ( 25 DateWidget, DateDisplayWidget, escape, DatetimeDisplayWidget) 23 from zope.formlib.textwidgets import DateWidget, DateDisplayWidget, escape 26 24 from zope.formlib.widget import renderElement, CustomWidgetFactory 27 25 from zope.interface import implements 28 29 class FormattedDateWidget(DateWidget):30 """A date widget that supports different (and _explicit_) date formats.31 32 This is an input widget.33 """34 date_format = '%Y-%m-%d'35 36 def _toFieldValue(self, input):37 if input == self._missing:38 return self.context.missing_value39 else:40 try:41 # In import files we can use the hash symbol at the end of a42 # date string to avoid annoying automatic date transformation43 # by Excel or Calc44 input = input.strip('#')45 value = datetime.strptime(input, self.date_format)46 except (ValueError, IndexError), v:47 raise ConversionError("Invalid datetime data", v)48 return value.date()49 50 def _toFormValue(self, value):51 if value:52 value = value.strftime(self.date_format)53 return value54 55 class FormattedDateDisplayWidget(DateDisplayWidget):56 """A date widget that supports different (and _explicit_) date formats.57 58 This is a display widget.59 """60 date_format = '%Y-%m-%d'61 implements(IDisplayWidget)62 63 def __call__(self):64 if self._renderedValueSet():65 content = self._data66 else:67 content = self.context.default68 if content == self.context.missing_value:69 return ""70 content = content.strftime(self.date_format)71 return renderElement("span", contents=escape(content),72 cssClass=self.cssClass)73 26 74 27 #: A dictionary of supported date formats. … … 99 52 #: ``us-year`` 100 53 #: middle endian format common in the U.S.: ``MM/DD/YYYY`` 101 #: 54 #: 102 55 #: For date display widgets there is naturally no difference between a 103 56 #: year and non-year setting (you can for instance use 'le' or 'le-year' … … 113 66 'us-year': ('datepicker-us-year', '%m/%d/%Y'), 114 67 } 68 69 #: a dict containing tuples (<FORMAT>, <SHOW_YEAR>) as keys and 70 #: suitable CSS tags as values. 71 FORMATS_BY_VALUE = dict( 72 [((val[1], 'year' in key), val[0]) for key, val in DATE_FORMATS.items()]) 73 74 class FormattedDateWidget(DateWidget): 75 """A date widget that supports different (and _explicit_) date formats. 76 77 If the widget is bound to a schema field with respective 78 attributes, it reads its `show_year` and `date_format` attributes 79 (see waeup.kofa.schema.FormattedDate for an example) and sets a 80 CSS tag according to these values. 81 82 The widget also accepts ISO format as a fallback, even if a 83 different format was set. This should help with imports. 84 85 This is an input widget. 86 """ 87 date_format = '%Y-%m-%d' 88 show_year = False 89 90 def __init__(self, context, request, *args, **kw): 91 # try to grab date_format and show_year from bound schema field. 92 self.date_format = getattr(context, 'date_format', self.date_format) 93 self.show_year = getattr(context, 'show_year', self.show_year) 94 self.cssClass = FORMATS_BY_VALUE.get(( 95 self.date_format, self.show_year), self.cssClass) 96 return super(FormattedDateWidget, self).__init__( 97 context, request, *args, **kw) 98 99 def _toFieldValue(self, input): 100 # In import files we can use the hash symbol at the end of a 101 # date string to avoid annoying automatic date transformation 102 # by Excel or Calc 103 input = input.strip('#') 104 if input == self._missing: 105 return self.context.missing_value 106 else: 107 try: 108 value = datetime.strptime(input, self.date_format) 109 except (ValueError, IndexError), v: 110 try: 111 # Try ISO format as fallback. 112 # This is needed for instance during imports. 113 value = datetime.strptime( 114 input, FormattedDateWidget.date_format) 115 except (ValueError, IndexError), v: 116 raise ConversionError("Invalid datetime data", v) 117 return value.date() 118 119 def _toFormValue(self, value): 120 if value: 121 value = value.strftime(self.date_format) 122 return value 123 124 class FormattedDateDisplayWidget(DateDisplayWidget): 125 """A date widget that supports different (and _explicit_) date formats. 126 127 This is a display widget. 128 """ 129 date_format = '%Y-%m-%d' 130 implements(IDisplayWidget) 131 132 def __call__(self): 133 if self._renderedValueSet(): 134 content = self._data 135 else: 136 content = self.context.default 137 if content == self.context.missing_value: 138 return "" 139 content = content.strftime(self.date_format) 140 return renderElement("span", contents=escape(content), 141 cssClass=self.cssClass) 142 115 143 def FriendlyDateWidget(format): 116 144 """Get a friendly date input widget for `format`. 117 145 118 146 This widget is suitable for edit and add forms. 119 147 120 148 Valid `format` values are the keys of `DATE_FORMATS` 121 149 dict. Default is ``le`` (little endian; DD/MM/YYYY). … … 134 162 135 163 This widget is suitable for display forms. 136 164 137 165 Valid `format` values are the keys of `DATE_FORMATS` 138 166 dict. Default is ``le`` (little endian; DD/MM/YYYY). 139 167 140 168 This widget is not rendered with a specialized CSS tag for 141 enabling JavaScript datepickers. `css_class` is ignored which means 169 enabling JavaScript datepickers. `css_class` is ignored which means 142 170 there is nor difference between e.g. ``le`` and ``le-year``.` 143 171 """ … … 157 185 This widget is not rendered with a specialized CSS tag for 158 186 enabling JavaScript datepickers. `css_class` is ignored which means 159 there is no rdifference between e.g. ``le`` and ``le-year``.`187 there is no difference between e.g. ``le`` and ``le-year``.` 160 188 """ 161 189 css_class, date_format = DATE_FORMATS.get(format, DATE_FORMATS['le'])
Note: See TracChangeset for help on using the changeset viewer.