l.a.w.d.DateWidget(DateTimeWidget) : class documentation

Part of lp.app.widgets.date View In Hierarchy

A date selection widget with popup selector.

The assumed underlying storage is a datetime (in the database) so this class modifies that datetime into a date for presentation purposes. That date is always in UTC.

The DateWidget subclass can limit requests to date ranges:

>>> from zope.publisher.browser import TestRequest
>>> from zope.schema import Field
>>> from datetime import date
>>> field = Field(__name__='foo', title=u'Foo')
>>> from_date = date(2004, 4, 5)
>>> to_date = date(2004, 4, 10)
>>> widget = DateWidget(field, TestRequest())
>>> widget.from_date = from_date
>>> widget.to_date = to_date
>>> '[[2004,04,05],[2004,04,10]]' in widget()
True

This widget ignores required_time_zone and system_time_zone and interprets everything as UTC. This does not matter, because it is only picking the date, and it will always be rendered as a date sans time zone even if it is stored as a datetime.

>>> widget.time_zone
<UTC>
>>> widget.system_time_zone = pytz.timezone('America/New_York')
>>> widget.time_zone
<UTC>
>>> widget.required_time_zone = pytz.timezone('America/Los_Angeles')
>>> widget.time_zone
<UTC>

A date picker can be disabled initially:

>>> 'disabled' in widget()
False
>>> widget.disabled = True
>>> 'disabled' in widget()
True
Method setRenderedValue Render a date from the underlying datetime.
Method _toFieldValue Return parsed input (datetime) as a date.
Method _toFormValue Convert a datetime to its string representation.

Inherited from DateTimeWidget:

Method __init__ Undocumented
Method supported_input_formats Undocumented
Method time_zone The widget time zone.
Method time_zone_name The name of the widget time zone for display in the widget.
Method disabled_flag Return a string to make the form input disabled if necessary.
Method daterange The javascript variable giving the allowed date range to pick.
Method getInputValue Return the date, if it is in the allowed date range.
Method formvalue Return the value for the form to render, accessed via the
Method _align_date_constraints_with_time_zone Ensure that from_date and to_date use the widget time zone.
Method _checkSupportedFormat Checks that the input is in a usable format.
Method _parseInput Convert a string to a datetime value.
def _toFieldValue(self, input):

Return parsed input (datetime) as a date.

The input is expected to be a text string in a format that datetime can parse. The input is parsed by the DateTimeWidget._parseInput method, which returns a datetime, and this method turns that into a date (without the time).

>>> from zope.publisher.browser import TestRequest
>>> from zope.schema import Field
>>> field = Field(__name__='foo', title=u'Foo')
>>> widget = DateWidget(field, TestRequest())

The widget converts an empty string to the missing value:

>>> widget._toFieldValue('') == field.missing_value
True

The widget ignores time and time zone information, returning only the date:

>>> print widget._toFieldValue('2006-01-01 12:00:00')
2006-01-01

Even if you feed it information that gives a time zone, it will ignore that:

>>> print widget._toFieldValue('2006-01-01 2:00:00+06:00')
2006-01-01
>>> print widget._toFieldValue('2006-01-01 23:00:00-06:00')
2006-01-01

Invalid dates result in a ConversionError:

>>> print widget._toFieldValue('not a date')  #doctest: +ELLIPSIS
Traceback (most recent call last):
  ...
ConversionError: ('Invalid date value', ...)
def _toFormValue(self, value):

Convert a datetime to its string representation.

>>> from zope.publisher.browser import TestRequest
>>> from zope.schema import Field
>>> field = Field(__name__='foo', title=u'Foo')
>>> widget = DateWidget(field, TestRequest())

The 'missing' value is converted to an empty string:

>>> widget._toFormValue(field.missing_value)
u''

The widget ignores time and time zone information, returning only the date:

>>> dt = datetime(
...     2006, 1, 1, 12, 0, 0, tzinfo=pytz.timezone('UTC'))
>>> widget._toFormValue(dt)
'2006-01-01'

The widget can handle a date just as well as a datetime, of course.

>>> a_date = dt.date()
>>> widget._toFormValue(a_date)
'2006-01-01'
def setRenderedValue(self, value):
Render a date from the underlying datetime.
API Documentation for Launchpad, generated by pydoctor at 2022-06-16 00:00:12.