Skip to content
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

Remove pytz as a dependency. #170

Merged
merged 1 commit into from
Apr 1, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion ChangeLog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ ChangeLog
master
------

No changes yet
*New:*
- Remove pytz as a dependency.

1.5.0 (15/01/2021)
------------------
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
Django>=1.11
pytz>=2015.7
17 changes: 8 additions & 9 deletions src/dirtyfields/compare.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import datetime
import pytz
import warnings
from datetime import datetime, timezone

from django.utils import timezone
from django.utils import timezone as django_timezone


def compare_states(new_state, original_state, compare_function, normalise_function):
Expand Down Expand Up @@ -33,13 +32,13 @@ def raw_compare(new_value, old_value):
return new_value == old_value


def timezone_support_compare(new_value, old_value, timezone_to_set=pytz.UTC):
def timezone_support_compare(new_value, old_value, timezone_to_set=timezone.utc):

if not (isinstance(new_value, datetime.datetime) and isinstance(old_value, datetime.datetime)):
if not (isinstance(new_value, datetime) and isinstance(old_value, datetime)):
return raw_compare(new_value, old_value)

db_value_is_aware = timezone.is_aware(old_value)
in_memory_value_is_aware = timezone.is_aware(new_value)
db_value_is_aware = django_timezone.is_aware(old_value)
in_memory_value_is_aware = django_timezone.is_aware(new_value)

if db_value_is_aware == in_memory_value_is_aware:
return raw_compare(new_value, old_value)
Expand All @@ -49,14 +48,14 @@ def timezone_support_compare(new_value, old_value, timezone_to_set=pytz.UTC):
warnings.warn(u"DateTimeField received a naive datetime (%s)"
u" while time zone support is active." % new_value,
RuntimeWarning)
new_value = timezone.make_aware(new_value, timezone_to_set).astimezone(pytz.utc)
new_value = django_timezone.make_aware(new_value, timezone_to_set).astimezone(timezone.utc)
else:
# The db is not timezone aware, but the value we are passing for comparison is aware.
warnings.warn(u"Time zone support is not active (settings.USE_TZ=False), "
u"and you pass a time zone aware value (%s)"
u" Converting database value before comparison." % new_value,
RuntimeWarning)
old_value = timezone.make_aware(old_value, pytz.utc).astimezone(timezone_to_set)
old_value = django_timezone.make_aware(old_value, timezone.utc).astimezone(timezone_to_set)

return raw_compare(new_value, old_value)

Expand Down
1 change: 0 additions & 1 deletion tests-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pytest==6.2.2
pytz
# Because we test a wide range of Django versions, leave versions of these unspecified.
pytest-django
jsonfield
11 changes: 7 additions & 4 deletions tests/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.db import models
from django.db.models.signals import pre_save
from django.utils import timezone
from django.utils import timezone as django_timezone
from jsonfield import JSONField as JSONFieldThirdParty

from dirtyfields import DirtyFieldsMixin
Expand Down Expand Up @@ -61,12 +61,15 @@ class ExpressionModelTest(DirtyFieldsMixin, models.Model):

class DatetimeModelTest(DirtyFieldsMixin, models.Model):
compare_function = (timezone_support_compare, {})
datetime_field = models.DateTimeField(default=timezone.now)
datetime_field = models.DateTimeField(default=django_timezone.now)


class CurrentDatetimeModelTest(DirtyFieldsMixin, models.Model):
compare_function = (timezone_support_compare, {'timezone_to_set': timezone.get_current_timezone()})
datetime_field = models.DateTimeField(default=timezone.now)
compare_function = (
timezone_support_compare,
{'timezone_to_set': django_timezone.get_current_timezone()},
)
datetime_field = models.DateTimeField(default=django_timezone.now)


class Many2ManyModelTest(DirtyFieldsMixin, models.Model):
Expand Down
28 changes: 14 additions & 14 deletions tests/test_timezone_aware_fields.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import pytest
import pytz
from datetime import datetime
from datetime import datetime, timedelta, timezone

from django.utils import timezone
import pytest
from django.test.utils import override_settings
from django.utils import timezone as django_timezone

from .models import DatetimeModelTest, CurrentDatetimeModelTest


@override_settings(USE_TZ=True, TIME_ZONE='America/Chicago')
@override_settings(USE_TZ=True)
@pytest.mark.django_db
def test_datetime_fields_when_aware_db_and_naive_current_value():
tm = DatetimeModelTest.objects.create(datetime_field=datetime(2000, 1, 1, tzinfo=pytz.utc))
tm = DatetimeModelTest.objects.create(datetime_field=datetime(2000, 1, 1, tzinfo=timezone.utc))

# Adding a naive datetime
tm.datetime_field = datetime(2016, 1, 1)
Expand All @@ -23,7 +22,7 @@ def test_datetime_fields_when_aware_db_and_naive_current_value():
r"while time zone support is active\."
),
):
assert tm.get_dirty_fields() == {'datetime_field': datetime(2000, 1, 1, tzinfo=pytz.utc)}
assert tm.get_dirty_fields() == {'datetime_field': datetime(2000, 1, 1, tzinfo=timezone.utc)}


@override_settings(USE_TZ=False)
Expand All @@ -32,7 +31,7 @@ def test_datetime_fields_when_naive_db_and_aware_current_value():
tm = DatetimeModelTest.objects.create(datetime_field=datetime(2000, 1, 1))

# Adding an aware datetime
tm.datetime_field = datetime(2016, 1, 1, tzinfo=pytz.utc)
tm.datetime_field = datetime(2016, 1, 1, tzinfo=timezone.utc)

with pytest.warns(
RuntimeWarning,
Expand All @@ -46,12 +45,13 @@ def test_datetime_fields_when_naive_db_and_aware_current_value():
assert tm.get_dirty_fields() == {'datetime_field': datetime(2000, 1, 1)}


@override_settings(USE_TZ=True)
@pytest.mark.django_db
def test_datetime_fields_when_aware_db_and_aware_current_value():
aware_dt = timezone.now()
aware_dt = django_timezone.now()
tm = DatetimeModelTest.objects.create(datetime_field=aware_dt)

tm.datetime_field = timezone.now()
tm.datetime_field = django_timezone.now()

assert tm.get_dirty_fields() == {'datetime_field': aware_dt}

Expand All @@ -69,7 +69,7 @@ def test_datetime_fields_when_naive_db_and_naive_current_value():
@override_settings(USE_TZ=True, TIME_ZONE='America/Chicago')
@pytest.mark.django_db
def test_datetime_fields_with_current_timezone_conversion():
tm = CurrentDatetimeModelTest.objects.create(datetime_field=datetime(2000, 1, 1, 12, 0, 0, tzinfo=pytz.utc))
tm = CurrentDatetimeModelTest.objects.create(datetime_field=datetime(2000, 1, 1, 12, 0, 0, tzinfo=timezone.utc))

# Adding a naive datetime, that will be converted to local timezone.
tm.datetime_field = datetime(2000, 1, 1, 6, 0, 0)
Expand All @@ -91,9 +91,9 @@ def test_datetime_fields_with_current_timezone_conversion():
def test_datetime_fields_with_current_timezone_conversion_without_timezone_support():
tm = CurrentDatetimeModelTest.objects.create(datetime_field=datetime(2000, 1, 1, 12, 0, 0))

# Adding an aware datetime
chicago_timezone = pytz.timezone('America/Chicago')
tm.datetime_field = chicago_timezone.localize(datetime(2000, 1, 1, 6, 0, 0), is_dst=None)
# Adding an aware datetime, Chicago is UTC-6h
chicago_timezone = timezone(timedelta(hours=-6))
tm.datetime_field = datetime(2000, 1, 1, 6, 0, 0, tzinfo=chicago_timezone)

# If the database is naive, then we consider that it is defined as in UTC.
with pytest.warns(
Expand Down