diff --git a/django_dump_die/constants.py b/django_dump_die/constants.py index d0d9f11eeaf1f6cc8befd2e32e738d4df2fa4c02..7c60ff1e161ba46d250648015a367537a4bd2e18 100644 --- a/django_dump_die/constants.py +++ b/django_dump_die/constants.py @@ -2,7 +2,11 @@ import datetime import types -import zoneinfo +try: + from zoneinfo import ZoneInfo + ZONEINFO_PRESENT = True +except ImportError: + ZONEINFO_PRESENT = False from decimal import Decimal @@ -36,7 +40,6 @@ INTERMEDIATE_TYPES = [ datetime.time, datetime.timedelta, timezone.timezone, - zoneinfo.ZoneInfo, PosixPath, PurePath, @@ -44,6 +47,9 @@ INTERMEDIATE_TYPES = [ PureWindowsPath, ] +if ZONEINFO_PRESENT: + INTERMEDIATE_TYPES.append(zoneinfo.ZoneInfo) + # List of additional simple types defined as strings that do not need to be recursively inspected. ADDITIONAL_SIMPLE_TYPES = getattr(settings, 'DJANGO_DD_ADDITIONAL_SIMPLE_TYPES', []) diff --git a/django_dump_die/views/example_helpers.py b/django_dump_die/views/example_helpers.py index 5455d628fec26f92447102baf598363710abb7e6..0a7172ef0b7f1609b9b17a1459cda4c8c2d91b33 100644 --- a/django_dump_die/views/example_helpers.py +++ b/django_dump_die/views/example_helpers.py @@ -16,7 +16,8 @@ from django.forms import ModelForm from django.utils import timezone from pathlib import Path, PosixPath, PurePath, WindowsPath from types import ModuleType -from zoneinfo import ZoneInfo + +from django_dump_die.constants import ZONEINFO_PRESENT BASE_DIR = os.path.dirname(os.path.abspath(__file__)) @@ -398,8 +399,6 @@ def dump_intermediate_types(): dump_datetime_types() - from pathlib import Path, PosixPath, PurePath, WindowsPath - # Generate variables to dump. sample_pure_path = PurePath(Path.cwd()) try: @@ -602,7 +601,8 @@ def dump_datetime_types(): sample_dt_timedelta = timedelta(days=1) sample_tz_timedelta = timezone.timedelta(days=2) sample_pytz_timezone = pytz.timezone('UTC') - sample_zoneinfo_timezone = ZoneInfo('UTC') + if ZONEINFO_PRESENT: + sample_zoneinfo_timezone = ZoneInfo('UTC') # Call dump on all generated variables. dump('') @@ -616,7 +616,8 @@ def dump_datetime_types(): dump(sample_dt_timedelta) dump(sample_tz_timedelta) dump(sample_pytz_timezone) - dump(sample_zoneinfo_timezone) + if ZONEINFO_PRESENT: + dump(sample_zoneinfo_timezone) def dump_model_types():