Skip to content

rework time zone support to set DatabaseFeatures.supports_timezones = False #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion django_mongodb/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ def _connect(self):
self.connection = MongoClient(
host=settings_dict["HOST"] or None,
port=int(settings_dict["PORT"] or 27017),
tz_aware=True,
**options,
)
db_name = settings_dict["NAME"]
Expand Down
7 changes: 4 additions & 3 deletions django_mongodb/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class DatabaseFeatures(BaseDatabaseFeatures):
supports_ignore_conflicts = False
# Not implemented: https://github.com/mongodb-labs/django-mongodb/issues/8
supports_json_field = False
# MongoDB stores datetimes in UTC.
supports_timezones = False
# Not implemented: https://github.com/mongodb-labs/django-mongodb/issues/7
supports_transactions = False
uses_savepoints = False
Expand All @@ -20,7 +22,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
"basic.tests.ModelLookupTest.test_does_not_exist",
"basic.tests.ModelLookupTest.test_equal_lookup",
"basic.tests.ModelLookupTest.test_rich_lookup",
"basic.tests.ModelTest.test_year_lookup_edge_case",
"lookup.tests.LookupTests.test_chain_date_time_lookups",
"lookup.test_timefield.TimeFieldLookupTests.test_hour_lookups",
"lookup.test_timefield.TimeFieldLookupTests.test_minute_lookups",
Expand Down Expand Up @@ -286,8 +287,8 @@ class DatabaseFeatures(BaseDatabaseFeatures):
"timezones.tests.LegacyDatabaseTests.test_cursor_execute_accepts_naive_datetime",
"timezones.tests.LegacyDatabaseTests.test_cursor_execute_returns_naive_datetime",
"timezones.tests.LegacyDatabaseTests.test_raw_sql",
"timezones.tests.NewDatabaseTests.test_cursor_execute_accepts_aware_datetime",
"timezones.tests.NewDatabaseTests.test_cursor_execute_returns_aware_datetime",
"timezones.tests.NewDatabaseTests.test_cursor_execute_accepts_naive_datetime",
"timezones.tests.NewDatabaseTests.test_cursor_execute_returns_naive_datetime",
Comment on lines +290 to +291
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand why the new test we skip is the naive version. So we will no longer support naive datetime instead?

Or is it that the naive tests were the original ones that should have been failing?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which tests run is controlled by DatabaseFeatures.supports_timezones, i.e. some tests are decorated with @skipUnlessDBFeature("supports_timezones") or @skipIfDBFeature("supports_timezones").

These are the tests that run when supports_timezones = False but we can't run them on MongoDB because they use SQL.

"timezones.tests.NewDatabaseTests.test_cursor_explicit_time_zone",
"timezones.tests.NewDatabaseTests.test_raw_sql",
},
Expand Down
19 changes: 14 additions & 5 deletions django_mongodb/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,16 @@ def adapt_datefield_value(self, value):
return datetime.datetime.combine(value, datetime.datetime.min.time())

def adapt_datetimefield_value(self, value):
if not settings.USE_TZ and value is not None and timezone.is_naive(value):
value = timezone.make_aware(value)
if value is None:
return None
if timezone.is_aware(value):
if settings.USE_TZ:
value = timezone.make_naive(value, self.connection.timezone)
else:
raise ValueError(
"MongoDB backend does not support timezone-aware "
"datetimes when USE_TZ is False."
)
return value

def adapt_decimalfield_value(self, value, max_digits=None, decimal_places=None):
Expand All @@ -31,6 +39,8 @@ def adapt_timefield_value(self, value):
"""Store TimeField as datetime."""
if value is None:
return None
if timezone.is_aware(value):
raise ValueError("MongoDB backend does not support timezone-aware times.")
return datetime.datetime.combine(datetime.datetime.min.date(), value)

def get_db_converters(self, expression):
Expand All @@ -39,7 +49,7 @@ def get_db_converters(self, expression):
if internal_type == "DateField":
converters.append(self.convert_datefield_value)
elif internal_type == "DateTimeField":
if not settings.USE_TZ:
if settings.USE_TZ:
converters.append(self.convert_datetimefield_value)
elif internal_type == "DecimalField":
converters.append(self.convert_decimalfield_value)
Expand All @@ -56,8 +66,7 @@ def convert_datefield_value(self, value, expression, connection):

def convert_datetimefield_value(self, value, expression, connection):
if value is not None:
# Django expects naive datetimes when settings.USE_TZ is False.
value = timezone.make_naive(value)
value = timezone.make_aware(value, self.connection.timezone)
return value

def convert_decimalfield_value(self, value, expression, connection):
Expand Down