Skip to content

Commit 23d729b

Browse files
author
Alex Hill
committed
Drop support for Django 1.8
1 parent e7544b5 commit 23d729b

File tree

3 files changed

+33
-47
lines changed

3 files changed

+33
-47
lines changed

.travis.yml

-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ env:
1111
global:
1212
- DJANGO_SETTINGS_MODULE=tests.settings
1313
matrix:
14-
- DJANGO_VERSION=1.8.19 DB=mysql
15-
- DJANGO_VERSION=1.8.19 DB=postgres
16-
- DJANGO_VERSION=1.8.19 DB=sqlite
1714
- DJANGO_VERSION=1.11.18 DB=mysql
1815
- DJANGO_VERSION=1.11.18 DB=postgres
1916
- DJANGO_VERSION=1.11.18 DB=sqlite

naivedatetimefield/__init__.py

+33-39
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,16 @@
22
import sys
33
import warnings
44

5-
import django
65
import pytz
76
from django.core import exceptions, checks
87
from django.db.models import DateTimeField, Func, Value
8+
from django.db.models.functions.datetime import TruncBase, Extract, ExtractYear
9+
from django.db.models.lookups import Exact, GreaterThan, GreaterThanOrEqual, \
10+
LessThan, LessThanOrEqual
911
from django.utils import timezone
1012
from django.utils.dateparse import parse_date, parse_datetime
1113
from django.utils.translation import gettext_lazy as _
1214

13-
if django.VERSION >= (1, 11):
14-
from django.db.models.functions.datetime import TruncBase, Extract, ExtractYear
15-
from django.db.models.lookups import Exact, GreaterThan, GreaterThanOrEqual, \
16-
LessThan, LessThanOrEqual
17-
1815

1916
class NaiveDateTimeField(DateTimeField):
2017
description = _("Naive Date (with time)")
@@ -123,7 +120,7 @@ def get_prep_value(self, value):
123120
return super(DateTimeField, self).get_prep_value(value)
124121

125122
def from_db_value(self, value, expression, connection, context):
126-
is_truncbase = django.VERSION >= (1, 11) and isinstance(expression, TruncBase)
123+
is_truncbase = isinstance(expression, TruncBase)
127124
if is_truncbase and not isinstance(expression, NaiveAsSQLMixin):
128125
raise TypeError(
129126
"Django's %s cannot be used with a NaiveDateTimeField"
@@ -134,8 +131,6 @@ def from_db_value(self, value, expression, connection, context):
134131
return timezone.make_naive(value, pytz.utc)
135132
return value
136133
if timezone.is_aware(value):
137-
if django.VERSION < (1, 9):
138-
return timezone.make_naive(value, pytz.utc)
139134
return timezone.make_naive(value, connection.timezone)
140135
return value
141136

@@ -210,33 +205,32 @@ def _resolve_output_field(self):
210205
_monkeypatching = False
211206

212207

213-
if django.VERSION >= (1, 11):
214-
_this_module = sys.modules[__name__]
215-
_db_functions = sys.modules['django.db.models.functions']
216-
_lookups = set(DateTimeField.get_lookups().values())
217-
_patch_classes = [
218-
(Extract, [NaiveAsSQLMixin, NaiveTimezoneMixin]),
219-
(TruncBase, [NaiveAsSQLMixin, NaiveTimezoneMixin, NaiveConvertValueMixin]),
220-
]
221-
for original, mixins in _patch_classes:
222-
for cls in original.__subclasses__():
223-
224-
bases = tuple(mixins) + (cls,)
225-
naive_cls = type(cls.__name__, bases, {})
226-
227-
if _monkeypatching:
228-
setattr(_db_functions, cls.__name__, naive_cls)
229-
230-
if cls in _lookups:
231-
NaiveDateTimeField.register_lookup(naive_cls)
232-
233-
# Year lookups don't need special handling with naive fields
234-
if cls is ExtractYear:
235-
naive_cls.register_lookup(Exact)
236-
naive_cls.register_lookup(GreaterThan)
237-
naive_cls.register_lookup(GreaterThanOrEqual)
238-
naive_cls.register_lookup(LessThan)
239-
naive_cls.register_lookup(LessThanOrEqual)
240-
241-
# Add an attribute to this module so these functions can be imported
242-
setattr(_this_module, cls.__name__, naive_cls)
208+
_this_module = sys.modules[__name__]
209+
_db_functions = sys.modules['django.db.models.functions']
210+
_lookups = set(DateTimeField.get_lookups().values())
211+
_patch_classes = [
212+
(Extract, [NaiveAsSQLMixin, NaiveTimezoneMixin]),
213+
(TruncBase, [NaiveAsSQLMixin, NaiveTimezoneMixin, NaiveConvertValueMixin]),
214+
]
215+
for original, mixins in _patch_classes:
216+
for cls in original.__subclasses__():
217+
218+
bases = tuple(mixins) + (cls,)
219+
naive_cls = type(cls.__name__, bases, {})
220+
221+
if _monkeypatching:
222+
setattr(_db_functions, cls.__name__, naive_cls)
223+
224+
if cls in _lookups:
225+
NaiveDateTimeField.register_lookup(naive_cls)
226+
227+
# Year lookups don't need special handling with naive fields
228+
if cls is ExtractYear:
229+
naive_cls.register_lookup(Exact)
230+
naive_cls.register_lookup(GreaterThan)
231+
naive_cls.register_lookup(GreaterThanOrEqual)
232+
naive_cls.register_lookup(LessThan)
233+
naive_cls.register_lookup(LessThanOrEqual)
234+
235+
# Add an attribute to this module so these functions can be imported
236+
setattr(_this_module, cls.__name__, naive_cls)

tests/tests.py

-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import datetime
22
from unittest import skipIf
33

4-
import django
54
import pytz
65
from django import db
76
from django.contrib.auth.models import User
@@ -76,7 +75,6 @@ def test_timezones_ignored(self):
7675
self.assertEqual(o1.naive, naive)
7776
self.assertEqual(o2.naive, naive)
7877

79-
@skipIf(django.VERSION < (1, 11), "date transforms and lookups unavailable before Django 1.11")
8078
def test_time_lookup(self):
8179
"""
8280
This should test that __time lookups work properly on naive datetime fields
@@ -94,7 +92,6 @@ def test_time_lookup(self):
9492

9593
self.assertEqual(results.count(), 1)
9694

97-
@skipIf(django.VERSION < (1, 11), "date transforms and lookups unavailable before Django 1.11")
9895
def test_date_trunc(self):
9996
"""
10097
Test that date truncating works regardless of active timezone.
@@ -137,7 +134,6 @@ def query_truncations(module):
137134
with self.assertRaisesRegex(TypeError, r"Django's \w+ cannot be used with a NaiveDateTimeField"):
138135
query_truncations(functions)
139136

140-
@skipIf(django.VERSION < (1, 11), "date transforms and lookups unavailable before Django 1.11")
141137
def test_date_transforms(self):
142138
"""
143139
Test that date transforms work regardless of active timezone.
@@ -238,7 +234,6 @@ def test_in_timezone(tz):
238234
test_in_timezone('Pacific/Chatham') # +12:45/+13:45
239235
test_in_timezone('Pacific/Marquesas') # -09:30
240236

241-
@skipIf(django.VERSION < (1, 11), "date transforms and lookups unavailable before Django 1.11")
242237
def test_date_extract_annotations(self):
243238
"""
244239
Test that date truncating works regardless of active timezone.

0 commit comments

Comments
 (0)