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

Last change on this file since 6052 was 6052, checked in by uli, 14 years ago

Fix missing imports.

File size: 2.4 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
28from zope.formlib.textwidgets import DateWidget, DateDisplayWidget, escape
29from zope.formlib.widget import renderElement
30
31class FormattedDateWidget(DateWidget):
32    """A date widget that supports different (and _explicit_) date formats.
33
34    This is an input widget.
35    """
36    date_format = '%Y-%m-%d'
37
38    def _toFieldValue(self, input):
39        if input == self._missing:
40            return self.context.missing_value
41        else:
42            try:
43                value = datetime.strptime(input, self.date_format)
44            except (ValueError, IndexError), v:
45                raise ConversionError(_("Invalid datetime data"), v)
46        return value.date()
47
48    def _toFormValue(self, value):
49        if value:
50            value = value.strftime(self.date_format)
51        return value
52
53class FormattedDateDisplayWidget(DateDisplayWidget):
54    """A date widget that supports different (and _explicit_) date formats.
55
56    This is a display widget.
57    """
58    date_format = '%Y-%m-%d'
59
60    def __call__(self):
61        if self._renderedValueSet():
62            content = self._data
63        else:
64            content = self.context.default
65        if content == self.context.missing_value:
66            return ""
67        content = content.strftime(self.date_format)
68        return renderElement("span", contents=escape(content),
69                             cssClass=self.cssClass)
Note: See TracBrowser for help on using the repository browser.