Changeset 6867 for main/waeup.sirp/trunk


Ignore:
Timestamp:
10 Oct 2011, 15:09:28 (13 years ago)
Author:
Henrik Bettermann
Message:

Add FormattedDatetimeDisplayWidget?.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.sirp/trunk/src/waeup/sirp/widgets/datewidget.py

    r6060 r6867  
    55## $Id$
    66##
    7 ## Copyright (C) 2011 Uli Fouquet
     7## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann
    88## This program is free software; you can redistribute it and/or modify
    99## it under the terms of the GNU General Public License as published by
     
    2626from zope.formlib.i18n import _
    2727from zope.formlib.interfaces import ConversionError, IDisplayWidget
    28 from zope.formlib.textwidgets import DateWidget, DateDisplayWidget, escape
     28from zope.formlib.textwidgets import (
     29    DateWidget, DateDisplayWidget, escape, DatetimeDisplayWidget)
    2930from zope.formlib.widget import renderElement, CustomWidgetFactory
    3031from zope.interface import implements
     
    6869            return ""
    6970        content = content.strftime(self.date_format)
     71        return renderElement("span", contents=escape(content),
     72                             cssClass=self.cssClass)
     73
     74class FormattedDatetimeDisplayWidget(DatetimeDisplayWidget):
     75    """A datetime widget that supports different (and _explicit_) date formats.
     76
     77    This is a display widget.
     78    """
     79    datetime_format = '%Y-%m-%d %H:%M:%S'
     80    implements(IDisplayWidget)
     81
     82    def __call__(self):
     83        if self._renderedValueSet():
     84            content = self._data
     85        else:
     86            content = self.context.default
     87        if content == self.context.missing_value:
     88            return ""
     89        content = content.strftime(self.datetime_format)
    7090        return renderElement("span", contents=escape(content),
    7191                             cssClass=self.cssClass)
     
    145165        FormattedDateDisplayWidget,
    146166        date_format=date_format)
     167
     168def FriendlyDatetimeDisplayWidget(format):
     169    """Get a friendly datetime display widget for `format`.
     170
     171    This widget is suitable for display forms.
     172
     173    Valid `format` values are the keys of :var:`DATE_FORMATS`
     174    dict. Default is ``le`` (little endian; DD/MM/YYYY).
     175
     176    This widget is not rendered with a specialized CSS tag for
     177    enabling JavaScript datepickers. `css_class` is ignored which means
     178    there is nor difference between e.g. ``le`` and ``le-year``.`
     179    """
     180    css_class, date_format = DATE_FORMATS.get(format, DATE_FORMATS['le'])
     181    datetime_format = date_format + ' %H:%M:%S'
     182    return CustomWidgetFactory(
     183        FormattedDatetimeDisplayWidget,
     184        datetime_format=datetime_format)
Note: See TracChangeset for help on using the changeset viewer.