Skip to content

Commit

Permalink
#26 Make EDTFField continue to work with derived DateField
Browse files Browse the repository at this point in the history
The change to returning `struct_time` objects from
the lower/upper strict/fuzzy methods broke the
`EDTFField` implementation.

This change fixes it so it can write to derived
`DateField`s again, though this is still subject
to the 1 to 9999 AD year restrictions of Python's
`datetime` module so a better fix would be to
support or require numeric target fields instead.
  • Loading branch information
jmurty committed Apr 13, 2018
1 parent bd0c577 commit f894987
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion edtf/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from edtf import parse_edtf, EDTFObject
from edtf.natlang import text_to_edtf
form edtf.utils import struct_time_to_date

DATE_ATTRS = (
'lower_strict',
Expand Down Expand Up @@ -116,7 +117,15 @@ def pre_save(self, instance, add):
g = getattr(self, field_attr, None)
if g:
if edtf:
setattr(instance, g, getattr(edtf, attr)())
target_field = getattr(instance, g)
value = getattr(edtf, attr)()
if isinstance(target_field, models.DateField):
value = struct_time_to_date(value)
else:
raise NotImplementedError(
u"EDTFField does not support %s as a derived data"
u" field, only DateField" % type(target_field))
setattr(instance, g, value)
else:
setattr(instance, g, None)
return edtf

0 comments on commit f894987

Please sign in to comment.