Skip to content

Commit

Permalink
Apply make_aware when the original file contains actual datetimes (#1478
Browse files Browse the repository at this point in the history
)

* Apply make_aware when the original file contains actual datetimes rather than strings

Fix #1165

* Add myself to AUTHORS

* Add regression test

See #1165

* bugfix: make datetime tz aware when value is date object

* replaced zoneinfo with pytz

Co-authored-by: matthewhegarty <mrhegarty@gmail.com>
  • Loading branch information
vanschelven and matthewhegarty committed Aug 4, 2022
1 parent 2c1dac5 commit a0b4dfc
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 12 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,4 @@ The following is a list of much appreciated contributors:
* 2ykwang (Yeongkwang Yang)
* KamilRizatdinov (Kamil Rizatdinov)
* Mark Walker
* vanschelven (Klaas van Schelven)
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Changelog
------------------

- Fix deprecation in example application: Added support for transitional form renderer (#1451)
- Apply make_aware when the original file contains actual datetimes (#1478)


2.8.0 (2022-03-31)
Expand Down
24 changes: 12 additions & 12 deletions import_export/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,21 +212,21 @@ def __init__(self, format=None):
self.formats = formats

def clean(self, value, row=None, *args, **kwargs):
dt = None
if not value:
return None
if isinstance(value, datetime):
return value
for format in self.formats:
try:
dt = datetime.strptime(value, format)
if settings.USE_TZ:
# make datetime timezone aware so we don't compare
# naive datetime to an aware one
dt = timezone.make_aware(dt,
timezone.get_default_timezone())
return dt
except (ValueError, TypeError):
continue
dt = value
else:
for format_ in self.formats:
try:
dt = datetime.strptime(value, format_)
except (ValueError, TypeError):
continue
if dt:
if settings.USE_TZ:
dt = timezone.make_aware(dt)
return dt
raise ValueError("Enter a valid date/time.")

def render(self, value, obj=None):
Expand Down
6 changes: 6 additions & 0 deletions tests/core/tests/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ def test_use_tz(self):
self.assertEqual(self.widget.render(utc_dt), "13.08.2012 20:00:00")
self.assertEqual(self.widget.clean("13.08.2012 20:00:00"), utc_dt)

@override_settings(USE_TZ=True, TIME_ZONE='Europe/Ljubljana')
def test_clean_returns_tz_aware_datetime_when_naive_datetime_passed(self):
# issue 1165
target_dt = timezone.make_aware(self.datetime, pytz.timezone('Europe/Ljubljana'))
self.assertEqual(target_dt, self.widget.clean(self.datetime))

@override_settings(DATETIME_INPUT_FORMATS=None)
def test_default_format(self):
self.widget = widgets.DateTimeWidget()
Expand Down

0 comments on commit a0b4dfc

Please sign in to comment.