Changeset 8158 for main/waeup.kofa/trunk


Ignore:
Timestamp:
15 Apr 2012, 00:30:20 (13 years ago)
Author:
uli
Message:

Add datetime widgets and schema fields as we have them for date.

Location:
main/waeup.kofa/trunk/src/waeup/kofa
Files:
2 added
4 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.kofa/trunk/src/waeup/kofa/schema/field.py

    r8151 r8158  
    1919"""
    2020from zope.interface import implements
    21 from zope.schema import TextLine, Date
     21from zope.schema import TextLine, Date, Datetime
    2222from zope.schema.interfaces import (
    2323    ITextLine, IBaseVocabulary, ISource, IContextSourceBinder, InvalidValue,)
    2424from zope.schema.vocabulary import (
    2525    SimpleVocabulary, getVocabularyRegistry, VocabularyRegistryError)
    26 from waeup.kofa.schema.interfaces import IFormattedDate
     26from waeup.kofa.schema.interfaces import IFormattedDate, IFormattedDatetime
    2727
    2828class CustomizableErrorMsg(object):
     
    153153        self.show_year = show_year
    154154        return super(FormattedDate, self).__init__(*args, **kw)
     155
     156class FormattedDatetime(Datetime):
     157    """A datetime field that supports additional formatting attributes.
     158
     159    Stores extra attributes (see below). To make use of these
     160    attributes in forms, you have to provide widgets that read them
     161    and use them in their operations, for instance the
     162    `waeup.kofa.widgets.datewidget.FormattedDatetimeWidget`.
     163
     164    Extra attributes are as follows:
     165
     166    `date_format`
     167      additional attribute to describe desired date format. Must be a
     168      string that can be fed to strftime/strptime functions. By
     169      default `None`.
     170
     171    `show_time`
     172      boolean indicating whether also time should be displayed when
     173      rendering the value of this field. This should usually be
     174      `True`, so it's also the default.
     175    """
     176    implements(IFormattedDatetime)
     177    date_format = None
     178    show_time = True
     179    def __init__(self, date_format=None, show_time=True, *args, **kw):
     180        self.date_format = date_format
     181        self.show_time = show_time
     182        return super(FormattedDatetime, self).__init__(*args, **kw)
  • main/waeup.kofa/trunk/src/waeup/kofa/schema/interfaces.py

    r8146 r8158  
    1717##
    1818from zope import schema
    19 from zope.schema.interfaces import IDate
     19from zope.schema.interfaces import IDate, IDatetime
    2020
    2121class IFormattedDate(IDate):
     
    3737        default = '%Y-%m-%d',
    3838        )
     39
     40class IFormattedDatetime(IDatetime):
     41    """A formatted datetime.
     42
     43    Basically a zope.schema.IDatetime, but with optional additional
     44    attributes. These attributes _can_ be used by widgets to change
     45    the way of editing or displaying a date.
     46
     47    The waeup.kofa.widgets.datetimewidget.FormattedDatetimeWidget is a
     48    widget that supports these additional attributes.
     49    """
     50    show_time = schema.Bool(
     51        title = u'Show time when displaying this date?',
     52        default = True,
     53        )
     54    date_format = schema.ASCII(
     55        title = u'A date format string suitable for use with strftime.',
     56        default = '%Y-%m-%d %H:%M:%S',
     57        )
  • main/waeup.kofa/trunk/src/waeup/kofa/schema/tests/test_fields.py

    r8151 r8158  
    33import unittest
    44from zope.interface.verify import verifyClass, verifyObject
    5 from zope.schema.interfaces import IDate
    6 from waeup.kofa.schema import FormattedDate
    7 from waeup.kofa.schema.interfaces import IFormattedDate
     5from zope.schema.interfaces import IDate, IDatetime
     6from waeup.kofa.schema import FormattedDate, FormattedDatetime
     7from waeup.kofa.schema.interfaces import IFormattedDate, IFormattedDatetime
    88
    99class FormattedDateTests(unittest.TestCase):
     
    3232        self.assertEqual(obj.date_format, '%d.%m.%Y')
    3333        return
     34
     35class FormattedDatetimeTests(unittest.TestCase):
     36    # Tests for FormattedDatetime field.
     37
     38    def test_iface(self):
     39        # make sure we fullfill interface contracts
     40        obj = FormattedDatetime()
     41        verifyClass(IDatetime, FormattedDatetime)
     42        verifyClass(IFormattedDatetime, FormattedDatetime)
     43        verifyObject(IDatetime, obj)
     44        verifyObject(IFormattedDatetime, obj)
     45        return
     46
     47    def test_defaults(self):
     48        # we get expected default values for datetimes.
     49        obj = FormattedDatetime()
     50        self.assertEqual(obj.show_time, True)
     51        self.assertEqual(obj.date_format, None)
     52        return
     53
     54    def test_attribs(self):
     55        # we can set the promised attributes.
     56        obj = FormattedDatetime(show_time=False,
     57                                date_format='%d.%m.%Y %H:%M:%S Uhr')
     58        self.assertEqual(obj.show_time, False)
     59        self.assertEqual(obj.date_format, '%d.%m.%Y %H:%M:%S Uhr')
     60        return
  • main/waeup.kofa/trunk/src/waeup/kofa/widgets/overrides.zcml

    r8157 r8158  
    2525      />
    2626
     27  <adapter
     28      for="waeup.kofa.schema.interfaces.IFormattedDatetime
     29           zope.publisher.interfaces.browser.IBrowserRequest"
     30      provides="zope.formlib.interfaces.ISimpleInputWidget"
     31      factory="waeup.kofa.widgets.datetimewidget.DatetimeLEWidget"
     32      permission="zope.Public"
     33      />
     34
     35  <adapter
     36      for="waeup.kofa.schema.interfaces.IFormattedDatetime
     37           zope.publisher.interfaces.browser.IBrowserRequest"
     38      provides="zope.formlib.interfaces.IDisplayWidget"
     39      factory="waeup.kofa.widgets.datetimewidget.DatetimeLEDisplayWidget"
     40      permission="zope.Public"
     41      />
     42
    2743</configure>
Note: See TracChangeset for help on using the changeset viewer.