source: main/waeup.sirp/trunk/src/waeup/sirp/widgets/datewidget.py @ 6436

Last change on this file since 6436 was 6060, checked in by Henrik Bettermann, 14 years ago

Use correct year format, Capital Y instead of y.

File size: 5.0 KB
Line 
1##
2## datewidget.py
3## Login : <uli@pu.smp.net>
4## Started on  Wed May 11 15:53:35 2011 Uli Fouquet
5## $Id$
6##
7## Copyright (C) 2011 Uli Fouquet
8## This program is free software; you can redistribute it and/or modify
9## it under the terms of the GNU General Public License as published by
10## the Free Software Foundation; either version 2 of the License, or
11## (at your option) any later version.
12##
13## This program is distributed in the hope that it will be useful,
14## but WITHOUT ANY WARRANTY; without even the implied warranty of
15## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16## GNU General Public License for more details.
17##
18## You should have received a copy of the GNU General Public License
19## along with this program; if not, write to the Free Software
20## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21##
22"""
23A datewidget with customizable date format.
24"""
25from datetime import datetime
26from zope.formlib.i18n import _
27from zope.formlib.interfaces import ConversionError, IDisplayWidget
28from zope.formlib.textwidgets import DateWidget, DateDisplayWidget, escape
29from zope.formlib.widget import renderElement, CustomWidgetFactory
30from zope.interface import implements
31
32class FormattedDateWidget(DateWidget):
33    """A date widget that supports different (and _explicit_) date formats.
34
35    This is an input widget.
36    """
37    date_format = '%Y-%m-%d'
38
39    def _toFieldValue(self, input):
40        if input == self._missing:
41            return self.context.missing_value
42        else:
43            try:
44                value = datetime.strptime(input, self.date_format)
45            except (ValueError, IndexError), v:
46                raise ConversionError(_("Invalid datetime data"), v)
47        return value.date()
48
49    def _toFormValue(self, value):
50        if value:
51            value = value.strftime(self.date_format)
52        return value
53
54class FormattedDateDisplayWidget(DateDisplayWidget):
55    """A date widget that supports different (and _explicit_) date formats.
56
57    This is a display widget.
58    """
59    date_format = '%Y-%m-%d'
60    implements(IDisplayWidget)
61   
62    def __call__(self):
63        if self._renderedValueSet():
64            content = self._data
65        else:
66            content = self.context.default
67        if content == self.context.missing_value:
68            return ""
69        content = content.strftime(self.date_format)
70        return renderElement("span", contents=escape(content),
71                             cssClass=self.cssClass)
72
73#: A dictionary of supported date formats.
74#:
75#: The following formats are supported:
76#:
77#: ``iso``
78#:    ISO format ``YYYY-MM-DD``
79#: ``le``
80#:    little endian with slashes: ``DD/MM/YYYY``
81#: ``de``
82#:    german date format: ``DD.MM.YYYY``
83#: ``us``
84#:    middle endian format common in the U.S.: ``MM/DD/YYYY``
85#:
86#: Furthermore we support for input widgets an additional year
87#: marker. Input date widgets with this marker provide also a year
88#: selector, handy for dates of birth etc.
89#:
90#: The year-supporting formats are similar to the basic versions above:
91#:
92#: ``iso-year``
93#:    ISO format ``YYYY-MM-DD``
94#: ``le-year``
95#:    little endian with slashes: ``DD/MM/YYYY``
96#: ``de-year``
97#:    german date format: ``DD.MM.YYYY``
98#: ``us-year``
99#:    middle endian format common in the U.S.: ``MM/DD/YYYY``
100#:   
101#: For date display widgets there is naturally no difference between a
102#: year and non-year setting (you can for instance use 'le' or 'le-year'
103#: with the same output).
104DATE_FORMATS = {
105    'iso': ('datepicker', '%Y-%m-%d'),
106    'le':  ('datepicker-le', '%d/%m/%Y'),
107    'de':  ('datepicker-de', '%d.%m.%Y'),
108    'us':  ('datepicker-us', '%m/%d/%Y'),
109    'iso-year': ('datepicker-year', '%Y-%m-%d'),
110    'le-year':  ('datepicker-le-year', '%d/%m/%Y'),
111    'de-year':  ('datepicker-de-year', '%d.%m.%Y'),
112    'us-year':  ('datepicker-us-year', '%m/%d/%Y'),
113    }
114def FriendlyDateWidget(format):
115    """Get a friendly date input widget for `format`.
116
117    This widget is suitable for edit and add forms.
118   
119    Valid `format` values are the keys of :var:`DATE_FORMATS`
120    dict. Default is ``le`` (little endian; DD/MM/YYYY).
121
122    Friendly date widgets are rendered with a specialized CSS tag for
123    enabling JavaScript datepickers.
124    """
125    css_class, date_format = DATE_FORMATS.get(format, DATE_FORMATS['le'])
126    return CustomWidgetFactory(
127        FormattedDateWidget,
128        cssClass=css_class,
129        date_format=date_format)
130
131def FriendlyDateDisplayWidget(format):
132    """Get a friendly date display widget for `format`.
133
134    This widget is suitable for display forms.
135   
136    Valid `format` values are the keys of :var:`DATE_FORMATS`
137    dict. Default is ``le`` (little endian; DD/MM/YYYY).
138
139    This widget is not rendered with a specialized CSS tag for
140    enabling JavaScript datepickers. `css_class` is ignored which means
141    there is nor difference between e.g. ``le`` and ``le-year``.`
142    """
143    css_class, date_format = DATE_FORMATS.get(format, DATE_FORMATS['le'])
144    return CustomWidgetFactory(
145        FormattedDateDisplayWidget,
146        date_format=date_format)
Note: See TracBrowser for help on using the repository browser.