Skip to content

Commit

Permalink
LazyDate('') is interpreted as None
Browse files Browse the repository at this point in the history
  • Loading branch information
kedder committed Dec 11, 2012
1 parent eb3241b commit e3e3581
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGES.txt
Expand Up @@ -2,6 +2,10 @@
CHANGES
=======

1.1 (2012-12-11)

- LazyDate('') is interpreted as None

1.0.1 (2012-10-22)

- Fix packaging bugs.
Expand Down
3 changes: 3 additions & 0 deletions MANIFEST.in
@@ -0,0 +1,3 @@
include *.txt
include buildout.cfg
recursive-include src *.py
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -23,7 +23,7 @@ def read(*rnames):

setup(
name='cipher.lazydate',
version='1.1dev',
version='1.1',
description='Human-friendly zope.schema datetime field',
url="http://pypi.python.org/pypi/cipher.lazydate/",
author='Zope Foundation and Contributors',
Expand Down
10 changes: 10 additions & 0 deletions src/cipher/lazydate/lazydate.py
Expand Up @@ -25,11 +25,15 @@ def __init__(self, date):

def datetime(self):
timetuple, result = self._parse()
if not timetuple:
return None
dt = datetime.datetime(*timetuple[:6])
return self._addTimeZone(dt)

def date(self):
timetuple, result = self._parse()
if not timetuple:
return None
dt = datetime.datetime(*timetuple[:3])
return self._addTimeZone(dt)

Expand All @@ -44,6 +48,9 @@ def _addTimeZone(self, dt):
def _parse(self):
# Try dateutil.parser first, since it does a better job with real
# dates.
if not self.spec:
return None, 0

try:
dt = dateutil.parser.parse(self.spec)
except ValueError:
Expand Down Expand Up @@ -81,6 +88,9 @@ def __init__(self, **kw):
zope.schema.Object.__init__(self, interfaces.ILazyDate, **kw)

def fromUnicode(self, strvalue):
if strvalue == '':
return None

value = self.valueFactory(strvalue)
if not value.validate():
raise ValueError(strvalue)
Expand Down
21 changes: 21 additions & 0 deletions src/cipher/lazydate/tests/test_lazydate.py
Expand Up @@ -161,6 +161,27 @@ def doctest_LazyDateField_fromUnicode():
"""

def doctest_LazyDateField_empty():
"""LazyDateField converts empty strings to None
>>> field = lazydate.LazyDateField(title=u'Date')
>>> print field.fromUnicode('')
None
"""

def doctest_LazyDateF_empty():
"""LazyDate converts '' to None
>>> ld = lazydate.LazyDate('')
>>> ld.validate()
0
>>> print ld.date()
None
>>> print ld.datetime()
None
"""
def test_suite():
return doctest.DocTestSuite(
optionflags=doctest.REPORT_NDIFF|doctest.ELLIPSIS,
Expand Down

0 comments on commit e3e3581

Please sign in to comment.