Skip to content

Commit 69f1e3a

Browse files
committed
Remove old django checks from tests and compat
Remove skipping of tests for Django<1.8. Remove several functions from compat.py not needed for Django>=1.8
1 parent 5ace717 commit 69f1e3a

File tree

10 files changed

+24
-130
lines changed

10 files changed

+24
-130
lines changed

rest_framework/compat.py

Lines changed: 2 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -86,18 +86,6 @@ def distinct(queryset, base):
8686
crispy_forms = None
8787

8888

89-
if django.VERSION >= (1, 6):
90-
def clean_manytomany_helptext(text):
91-
return text
92-
else:
93-
# Up to version 1.5 many to many fields automatically suffix
94-
# the `help_text` attribute with hardcoded text.
95-
def clean_manytomany_helptext(text):
96-
if text.endswith(' Hold down "Control", or "Command" on a Mac, to select more than one.'):
97-
text = text[:-69]
98-
return text
99-
100-
10189
# Django-guardian is optional. Import only if guardian is in INSTALLED_APPS
10290
# Fixes (#1712). We keep the try/except for the test suite.
10391
guardian = None
@@ -109,41 +97,6 @@ def clean_manytomany_helptext(text):
10997
pass
11098

11199

112-
# MinValueValidator, MaxValueValidator et al. only accept `message` in 1.8+
113-
if django.VERSION >= (1, 8):
114-
from django.core.validators import MinValueValidator, MaxValueValidator
115-
from django.core.validators import MinLengthValidator, MaxLengthValidator
116-
else:
117-
from django.core.validators import MinValueValidator as DjangoMinValueValidator
118-
from django.core.validators import MaxValueValidator as DjangoMaxValueValidator
119-
from django.core.validators import MinLengthValidator as DjangoMinLengthValidator
120-
from django.core.validators import MaxLengthValidator as DjangoMaxLengthValidator
121-
122-
123-
class MinValueValidator(DjangoMinValueValidator):
124-
def __init__(self, *args, **kwargs):
125-
self.message = kwargs.pop('message', self.message)
126-
super(MinValueValidator, self).__init__(*args, **kwargs)
127-
128-
129-
class MaxValueValidator(DjangoMaxValueValidator):
130-
def __init__(self, *args, **kwargs):
131-
self.message = kwargs.pop('message', self.message)
132-
super(MaxValueValidator, self).__init__(*args, **kwargs)
133-
134-
135-
class MinLengthValidator(DjangoMinLengthValidator):
136-
def __init__(self, *args, **kwargs):
137-
self.message = kwargs.pop('message', self.message)
138-
super(MinLengthValidator, self).__init__(*args, **kwargs)
139-
140-
141-
class MaxLengthValidator(DjangoMaxLengthValidator):
142-
def __init__(self, *args, **kwargs):
143-
self.message = kwargs.pop('message', self.message)
144-
super(MaxLengthValidator, self).__init__(*args, **kwargs)
145-
146-
147100
# PATCH method is not implemented by Django
148101
if 'patch' not in View.http_method_names:
149102
View.http_method_names = View.http_method_names + ['patch']
@@ -188,13 +141,6 @@ def apply_markdown(text):
188141
LONG_SEPARATORS = (b', ', b': ')
189142
INDENT_SEPARATORS = (b',', b': ')
190143

191-
if django.VERSION >= (1, 8):
192-
from django.db.models import DurationField
193-
from django.utils.dateparse import parse_duration
194-
from django.utils.duration import duration_string
195-
else:
196-
DurationField = duration_string = parse_duration = None
197-
198144
try:
199145
# DecimalValidator is unavailable in Django < 1.9
200146
from django.core.validators import DecimalValidator
@@ -223,14 +169,14 @@ def template_render(template, context=None, request=None):
223169
"""
224170
Passing Context or RequestContext to Template.render is deprecated in 1.9+,
225171
see https://github.com/django/django/pull/3883 and
226-
https://github.com/django/django/blob/1.9rc1/django/template/backends/django.py#L82-L84
172+
https://github.com/django/django/blob/1.9/django/template/backends/django.py#L82-L84
227173
228174
:param template: Template instance
229175
:param context: dict
230176
:param request: Request instance
231177
:return: rendered template as SafeText instance
232178
"""
233-
if django.VERSION < (1, 8) or isinstance(template, Template):
179+
if isinstance(template, Template):
234180
if request:
235181
context = RequestContext(request, context)
236182
else:
@@ -239,32 +185,3 @@ def template_render(template, context=None, request=None):
239185
# backends template, e.g. django.template.backends.django.Template
240186
else:
241187
return template.render(context, request=request)
242-
243-
244-
def get_all_related_objects(opts):
245-
"""
246-
Django 1.8 changed meta api, see
247-
https://docs.djangoproject.com/en/1.8/ref/models/meta/#migrating-old-meta-api
248-
https://code.djangoproject.com/ticket/12663
249-
https://github.com/django/django/pull/3848
250-
251-
:param opts: Options instance
252-
:return: list of relations except many-to-many ones
253-
"""
254-
if django.VERSION < (1, 8):
255-
return opts.get_all_related_objects()
256-
else:
257-
return [r for r in opts.related_objects if not r.field.many_to_many]
258-
259-
260-
def get_all_related_many_to_many_objects(opts):
261-
"""
262-
Django 1.8 changed meta api, see docstr in compat.get_all_related_objects()
263-
264-
:param opts: Options instance
265-
:return: list of many-to-many relations
266-
"""
267-
if django.VERSION < (1, 8):
268-
return opts.get_all_related_many_to_many_objects()
269-
else:
270-
return [r for r in opts.related_objects if r.field.many_to_many]

rest_framework/fields.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,23 @@
1414
from django.core.exceptions import ValidationError as DjangoValidationError
1515
from django.core.exceptions import ObjectDoesNotExist
1616
from django.core.validators import (
17-
EmailValidator, RegexValidator, URLValidator, ip_address_validators
17+
EmailValidator, MaxLengthValidator, MaxValueValidator, MinLengthValidator,
18+
MinValueValidator, RegexValidator, URLValidator, ip_address_validators
1819
)
1920
from django.forms import FilePathField as DjangoFilePathField
2021
from django.forms import ImageField as DjangoImageField
2122
from django.utils import six, timezone
22-
from django.utils.dateparse import parse_date, parse_datetime, parse_time
23+
from django.utils.dateparse import (
24+
parse_date, parse_datetime, parse_duration, parse_time
25+
)
26+
from django.utils.duration import duration_string
2327
from django.utils.encoding import is_protected_type, smart_text
2428
from django.utils.functional import cached_property
2529
from django.utils.ipv6 import clean_ipv6_address
2630
from django.utils.translation import ugettext_lazy as _
2731

2832
from rest_framework import ISO_8601
29-
from rest_framework.compat import (
30-
MaxLengthValidator, MaxValueValidator, MinLengthValidator,
31-
MinValueValidator, duration_string, parse_duration, unicode_repr,
32-
unicode_to_repr
33-
)
33+
from rest_framework.compat import unicode_repr, unicode_to_repr
3434
from rest_framework.exceptions import ValidationError
3535
from rest_framework.settings import api_settings
3636
from rest_framework.utils import html, humanize_datetime, representation
@@ -1215,12 +1215,6 @@ class DurationField(Field):
12151215
'invalid': _('Duration has wrong format. Use one of these formats instead: {format}.'),
12161216
}
12171217

1218-
def __init__(self, *args, **kwargs):
1219-
if parse_duration is None:
1220-
raise NotImplementedError(
1221-
'DurationField not supported for django versions prior to 1.8')
1222-
return super(DurationField, self).__init__(*args, **kwargs)
1223-
12241218
def to_internal_value(self, value):
12251219
if isinstance(value, datetime.timedelta):
12261220
return value

rest_framework/renderers.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import json
1212
from collections import OrderedDict
1313

14-
import django
1514
from django import forms
1615
from django.core.exceptions import ImproperlyConfigured
1716
from django.core.paginator import Page
@@ -773,7 +772,7 @@ class MultiPartRenderer(BaseRenderer):
773772
media_type = 'multipart/form-data; boundary=BoUnDaRyStRiNg'
774773
format = 'multipart'
775774
charset = 'utf-8'
776-
BOUNDARY = 'BoUnDaRyStRiNg' if django.VERSION >= (1, 5) else b'BoUnDaRyStRiNg'
775+
BOUNDARY = 'BoUnDaRyStRiNg'
777776

778777
def render(self, data, accepted_media_type=None, renderer_context=None):
779778
if hasattr(data, 'items'):

rest_framework/serializers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
import warnings
1616

1717
from django.db import models
18+
from django.db.models import DurationField as ModelDurationField
1819
from django.db.models.fields import Field as DjangoModelField
1920
from django.db.models.fields import FieldDoesNotExist
2021
from django.utils.functional import cached_property
2122
from django.utils.translation import ugettext_lazy as _
2223

23-
from rest_framework.compat import DurationField as ModelDurationField
2424
from rest_framework.compat import JSONField as ModelJSONField
2525
from rest_framework.compat import postgres_fields, unicode_to_repr
2626
from rest_framework.utils import model_meta

rest_framework/test.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
# to make it harder for the user to import the wrong thing without realizing.
55
from __future__ import unicode_literals
66

7-
import django
87
from django.conf import settings
98
from django.test import testcases
109
from django.test.client import Client as DjangoClient
@@ -223,9 +222,9 @@ class APITestCase(testcases.TestCase):
223222
client_class = APIClient
224223

225224

226-
if django.VERSION >= (1, 4):
227-
class APISimpleTestCase(testcases.SimpleTestCase):
228-
client_class = APIClient
225+
class APISimpleTestCase(testcases.SimpleTestCase):
226+
client_class = APIClient
227+
229228

230-
class APILiveServerTestCase(testcases.LiveServerTestCase):
231-
client_class = APIClient
229+
class APILiveServerTestCase(testcases.LiveServerTestCase):
230+
client_class = APIClient

rest_framework/utils/model_meta.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@
1313
from django.db import models
1414
from django.utils import six
1515

16-
from rest_framework.compat import (
17-
get_all_related_many_to_many_objects, get_all_related_objects
18-
)
19-
2016
FieldInfo = namedtuple('FieldResult', [
2117
'pk', # Model field instance
2218
'fields', # Dict of field name -> model field instance
@@ -138,7 +134,8 @@ def _get_reverse_relationships(opts):
138134
# See: https://code.djangoproject.com/ticket/24208
139135

140136
reverse_relations = OrderedDict()
141-
for relation in get_all_related_objects(opts):
137+
all_related_objects = [r for r in opts.related_objects if not r.field.many_to_many]
138+
for relation in all_related_objects:
142139
accessor_name = relation.get_accessor_name()
143140
related = getattr(relation, 'related_model', relation.model)
144141
reverse_relations[accessor_name] = RelationInfo(
@@ -150,7 +147,8 @@ def _get_reverse_relationships(opts):
150147
)
151148

152149
# Deal with reverse many-to-many relationships.
153-
for relation in get_all_related_many_to_many_objects(opts):
150+
all_related_many_to_many_objects = [r for r in opts.related_objects if r.field.many_to_many]
151+
for relation in all_related_many_to_many_objects:
154152
accessor_name = relation.get_accessor_name()
155153
related = getattr(relation, 'related_model', relation.model)
156154
reverse_relations[accessor_name] = RelationInfo(

tests/test_fields.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import uuid
44
from decimal import Decimal
55

6-
import django
76
import pytest
87
from django.http import QueryDict
98
from django.utils import six, timezone
@@ -951,7 +950,7 @@ class TestDateTimeField(FieldValues):
951950
datetime.datetime(2001, 1, 1, 13, 00): datetime.datetime(2001, 1, 1, 13, 00, tzinfo=timezone.UTC()),
952951
datetime.datetime(2001, 1, 1, 13, 00, tzinfo=timezone.UTC()): datetime.datetime(2001, 1, 1, 13, 00, tzinfo=timezone.UTC()),
953952
# Django 1.4 does not support timezone string parsing.
954-
'2001-01-01T14:00+01:00' if (django.VERSION > (1, 4)) else '2001-01-01T13:00Z': datetime.datetime(2001, 1, 1, 13, 00, tzinfo=timezone.UTC())
953+
'2001-01-01T13:00Z': datetime.datetime(2001, 1, 1, 13, 00, tzinfo=timezone.UTC())
955954
}
956955
invalid_inputs = {
957956
'abc': ['Datetime has wrong format. Use one of these formats instead: YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HH:MM|-HH:MM|Z].'],
@@ -1077,8 +1076,6 @@ class TestNoOutputFormatTimeField(FieldValues):
10771076
field = serializers.TimeField(format=None)
10781077

10791078

1080-
@pytest.mark.skipif(django.VERSION < (1, 8),
1081-
reason='DurationField is only available for django1.8+')
10821079
class TestDurationField(FieldValues):
10831080
"""
10841081
Valid and invalid values for `DurationField`.
@@ -1097,8 +1094,7 @@ class TestDurationField(FieldValues):
10971094
outputs = {
10981095
datetime.timedelta(days=3, hours=8, minutes=32, seconds=1, microseconds=123): '3 08:32:01.000123',
10991096
}
1100-
if django.VERSION >= (1, 8):
1101-
field = serializers.DurationField()
1097+
field = serializers.DurationField()
11021098

11031099

11041100
# Choice types...

tests/test_generics.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import unicode_literals
22

3-
import django
43
import pytest
54
from django.db import models
65
from django.shortcuts import get_object_or_404
@@ -158,7 +157,7 @@ def test_post_error_root_view(self):
158157
self.assertIn(expected_error, response.rendered_content.decode('utf-8'))
159158

160159

161-
EXPECTED_QUERIES_FOR_PUT = 3 if django.VERSION < (1, 6) else 2
160+
EXPECTED_QUERIES_FOR_PUT = 2
162161

163162

164163
class TestInstanceView(TestCase):

tests/test_model_serializer.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,16 @@
1010
import decimal
1111
from collections import OrderedDict
1212

13-
import django
14-
import pytest
1513
from django.core.exceptions import ImproperlyConfigured
1614
from django.core.validators import (
1715
MaxValueValidator, MinLengthValidator, MinValueValidator
1816
)
1917
from django.db import models
18+
from django.db.models import DurationField as ModelDurationField
2019
from django.test import TestCase
2120
from django.utils import six
2221

2322
from rest_framework import serializers
24-
from rest_framework.compat import DurationField as ModelDurationField
2523
from rest_framework.compat import unicode_repr
2624

2725

@@ -341,8 +339,6 @@ class Meta:
341339
assert implicit.data == explicit.data
342340

343341

344-
@pytest.mark.skipif(django.VERSION < (1, 8),
345-
reason='DurationField is only available for django1.8+')
346342
class TestDurationFieldMapping(TestCase):
347343
def test_duration_field(self):
348344
class DurationFieldModel(models.Model):

tests/test_request.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
"""
44
from __future__ import unicode_literals
55

6-
import django
7-
import pytest
86
from django.conf.urls import url
97
from django.contrib.auth import authenticate, login, logout
108
from django.contrib.auth.models import User
@@ -201,8 +199,6 @@ def test_auth_can_be_set(self):
201199
self.assertEqual(request.auth, 'DUMMY')
202200

203201

204-
@pytest.mark.skipif(django.VERSION < (1, 7),
205-
reason='secure argument is only available for django1.7+')
206202
class TestSecure(TestCase):
207203

208204
def test_default_secure_false(self):

0 commit comments

Comments
 (0)