Skip to content

Commit

Permalink
added test for correct tz (#1999)
Browse files Browse the repository at this point in the history
* added test for correct tz

* added comment
  • Loading branch information
matthewhegarty authored Nov 11, 2024
1 parent e67d345 commit 6262a67
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
9 changes: 6 additions & 3 deletions import_export/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,13 +307,16 @@ def clean(self, value, row=None, **kwargs):

def render(self, value, obj=None, **kwargs):
self._obj_deprecation_warning(obj)
force_native_type = kwargs.get("force_native_type")
if not value or not isinstance(value, datetime):
return ""
if self.coerce_to_string is False or force_native_type:
return value.replace(tzinfo=None) if force_native_type else value
if settings.USE_TZ:
value = timezone.localtime(value)

force_native_type = kwargs.get("force_native_type")
if self.coerce_to_string is False or force_native_type:
# binary formats such as xlsx must not have tz set
return value.replace(tzinfo=None) if force_native_type else value

return format_datetime(value, self.formats[0])


Expand Down
23 changes: 22 additions & 1 deletion tests/core/tests/admin_integration/test_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@ def test_dynamic_export_with_custom_resource(
self.assertEqual(datetime(2000, 8, 2), wb.active["C2"].value)


@override_settings(USE_TZ=True)
@override_settings(USE_TZ=True, TIME_ZONE="UTC")
class ExportTzAwareDateTest(AdminTestMixin, TestCase):
# issue 1995
# test that tz aware dates do not crash on export
Expand Down Expand Up @@ -850,6 +850,27 @@ def test_datetime_export_xlsx(self, mock_choose_export_resource_class):
wb = load_workbook(filename=BytesIO(content))
self.assertEqual(date_added.replace(tzinfo=None), wb.active["B2"].value)

@override_settings(TIME_ZONE="Asia/Hong_Kong")
@patch("import_export.mixins.BaseExportMixin.choose_export_resource_class")
def test_datetime_export_xlsx_with_timezone(
self, mock_choose_export_resource_class
):
mock_choose_export_resource_class.return_value = self.BookResource_
date_added = datetime(2024, 11, 8, 14, 40, tzinfo=ZoneInfo("Asia/Hong_Kong"))
Book.objects.create(id=101, name="Moonraker", added=date_added)

data = {
"format": "2",
"bookresource_id": True,
"bookresource_title": True,
"bookresource_added": True,
}
response = self.client.post(self.book_export_url, data)
self.assertEqual(response.status_code, 200)
content = response.content
wb = load_workbook(filename=BytesIO(content))
self.assertEqual(date_added.replace(tzinfo=None), wb.active["B2"].value)

@patch("import_export.mixins.BaseExportMixin.choose_export_resource_class")
def test_datetime_export_xls(self, mock_choose_export_resource_class):
mock_choose_export_resource_class.return_value = self.BookResource_
Expand Down

0 comments on commit 6262a67

Please sign in to comment.