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

Closes #3952: Remove old django checks from tests and compat #3953

Merged
merged 1 commit into from
Feb 19, 2016
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
87 changes: 2 additions & 85 deletions rest_framework/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,6 @@ def distinct(queryset, base):
crispy_forms = None


if django.VERSION >= (1, 6):
def clean_manytomany_helptext(text):
return text
else:
# Up to version 1.5 many to many fields automatically suffix
# the `help_text` attribute with hardcoded text.
def clean_manytomany_helptext(text):
if text.endswith(' Hold down "Control", or "Command" on a Mac, to select more than one.'):
text = text[:-69]
return text


# Django-guardian is optional. Import only if guardian is in INSTALLED_APPS
# Fixes (#1712). We keep the try/except for the test suite.
guardian = None
Expand All @@ -109,41 +97,6 @@ def clean_manytomany_helptext(text):
pass


# MinValueValidator, MaxValueValidator et al. only accept `message` in 1.8+
if django.VERSION >= (1, 8):
from django.core.validators import MinValueValidator, MaxValueValidator
from django.core.validators import MinLengthValidator, MaxLengthValidator
else:
from django.core.validators import MinValueValidator as DjangoMinValueValidator
from django.core.validators import MaxValueValidator as DjangoMaxValueValidator
from django.core.validators import MinLengthValidator as DjangoMinLengthValidator
from django.core.validators import MaxLengthValidator as DjangoMaxLengthValidator


class MinValueValidator(DjangoMinValueValidator):
def __init__(self, *args, **kwargs):
self.message = kwargs.pop('message', self.message)
super(MinValueValidator, self).__init__(*args, **kwargs)


class MaxValueValidator(DjangoMaxValueValidator):
def __init__(self, *args, **kwargs):
self.message = kwargs.pop('message', self.message)
super(MaxValueValidator, self).__init__(*args, **kwargs)


class MinLengthValidator(DjangoMinLengthValidator):
def __init__(self, *args, **kwargs):
self.message = kwargs.pop('message', self.message)
super(MinLengthValidator, self).__init__(*args, **kwargs)


class MaxLengthValidator(DjangoMaxLengthValidator):
def __init__(self, *args, **kwargs):
self.message = kwargs.pop('message', self.message)
super(MaxLengthValidator, self).__init__(*args, **kwargs)


# PATCH method is not implemented by Django
if 'patch' not in View.http_method_names:
View.http_method_names = View.http_method_names + ['patch']
Expand Down Expand Up @@ -188,13 +141,6 @@ def apply_markdown(text):
LONG_SEPARATORS = (b', ', b': ')
INDENT_SEPARATORS = (b',', b': ')

if django.VERSION >= (1, 8):
from django.db.models import DurationField
from django.utils.dateparse import parse_duration
from django.utils.duration import duration_string
else:
DurationField = duration_string = parse_duration = None

try:
# DecimalValidator is unavailable in Django < 1.9
from django.core.validators import DecimalValidator
Expand Down Expand Up @@ -223,14 +169,14 @@ def template_render(template, context=None, request=None):
"""
Passing Context or RequestContext to Template.render is deprecated in 1.9+,
see https://github.com/django/django/pull/3883 and
https://github.com/django/django/blob/1.9rc1/django/template/backends/django.py#L82-L84
https://github.com/django/django/blob/1.9/django/template/backends/django.py#L82-L84

:param template: Template instance
:param context: dict
:param request: Request instance
:return: rendered template as SafeText instance
"""
if django.VERSION < (1, 8) or isinstance(template, Template):
if isinstance(template, Template):
if request:
context = RequestContext(request, context)
else:
Expand All @@ -239,32 +185,3 @@ def template_render(template, context=None, request=None):
# backends template, e.g. django.template.backends.django.Template
else:
return template.render(context, request=request)


def get_all_related_objects(opts):
"""
Django 1.8 changed meta api, see
https://docs.djangoproject.com/en/1.8/ref/models/meta/#migrating-old-meta-api
https://code.djangoproject.com/ticket/12663
https://github.com/django/django/pull/3848

:param opts: Options instance
:return: list of relations except many-to-many ones
"""
if django.VERSION < (1, 8):
return opts.get_all_related_objects()
else:
return [r for r in opts.related_objects if not r.field.many_to_many]


def get_all_related_many_to_many_objects(opts):
"""
Django 1.8 changed meta api, see docstr in compat.get_all_related_objects()

:param opts: Options instance
:return: list of many-to-many relations
"""
if django.VERSION < (1, 8):
return opts.get_all_related_many_to_many_objects()
else:
return [r for r in opts.related_objects if r.field.many_to_many]
20 changes: 7 additions & 13 deletions rest_framework/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,23 @@
from django.core.exceptions import ValidationError as DjangoValidationError
from django.core.exceptions import ObjectDoesNotExist
from django.core.validators import (
EmailValidator, RegexValidator, URLValidator, ip_address_validators
EmailValidator, MaxLengthValidator, MaxValueValidator, MinLengthValidator,
MinValueValidator, RegexValidator, URLValidator, ip_address_validators
)
from django.forms import FilePathField as DjangoFilePathField
from django.forms import ImageField as DjangoImageField
from django.utils import six, timezone
from django.utils.dateparse import parse_date, parse_datetime, parse_time
from django.utils.dateparse import (
parse_date, parse_datetime, parse_duration, parse_time
)
from django.utils.duration import duration_string
from django.utils.encoding import is_protected_type, smart_text
from django.utils.functional import cached_property
from django.utils.ipv6 import clean_ipv6_address
from django.utils.translation import ugettext_lazy as _

from rest_framework import ISO_8601
from rest_framework.compat import (
MaxLengthValidator, MaxValueValidator, MinLengthValidator,
MinValueValidator, duration_string, parse_duration, unicode_repr,
unicode_to_repr
)
from rest_framework.compat import unicode_repr, unicode_to_repr
from rest_framework.exceptions import ValidationError
from rest_framework.settings import api_settings
from rest_framework.utils import html, humanize_datetime, representation
Expand Down Expand Up @@ -1215,12 +1215,6 @@ class DurationField(Field):
'invalid': _('Duration has wrong format. Use one of these formats instead: {format}.'),
}

def __init__(self, *args, **kwargs):
if parse_duration is None:
raise NotImplementedError(
'DurationField not supported for django versions prior to 1.8')
return super(DurationField, self).__init__(*args, **kwargs)

def to_internal_value(self, value):
if isinstance(value, datetime.timedelta):
return value
Expand Down
3 changes: 1 addition & 2 deletions rest_framework/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import json
from collections import OrderedDict

import django
from django import forms
from django.core.exceptions import ImproperlyConfigured
from django.core.paginator import Page
Expand Down Expand Up @@ -773,7 +772,7 @@ class MultiPartRenderer(BaseRenderer):
media_type = 'multipart/form-data; boundary=BoUnDaRyStRiNg'
format = 'multipart'
charset = 'utf-8'
BOUNDARY = 'BoUnDaRyStRiNg' if django.VERSION >= (1, 5) else b'BoUnDaRyStRiNg'
BOUNDARY = 'BoUnDaRyStRiNg'

def render(self, data, accepted_media_type=None, renderer_context=None):
if hasattr(data, 'items'):
Expand Down
2 changes: 1 addition & 1 deletion rest_framework/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
import warnings

from django.db import models
from django.db.models import DurationField as ModelDurationField
from django.db.models.fields import Field as DjangoModelField
from django.db.models.fields import FieldDoesNotExist
from django.utils.functional import cached_property
from django.utils.translation import ugettext_lazy as _

from rest_framework.compat import DurationField as ModelDurationField
from rest_framework.compat import JSONField as ModelJSONField
from rest_framework.compat import postgres_fields, unicode_to_repr
from rest_framework.utils import model_meta
Expand Down
11 changes: 5 additions & 6 deletions rest_framework/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
# to make it harder for the user to import the wrong thing without realizing.
from __future__ import unicode_literals

import django
from django.conf import settings
from django.test import testcases
from django.test.client import Client as DjangoClient
Expand Down Expand Up @@ -223,9 +222,9 @@ class APITestCase(testcases.TestCase):
client_class = APIClient


if django.VERSION >= (1, 4):
class APISimpleTestCase(testcases.SimpleTestCase):
client_class = APIClient
class APISimpleTestCase(testcases.SimpleTestCase):
client_class = APIClient


class APILiveServerTestCase(testcases.LiveServerTestCase):
client_class = APIClient
class APILiveServerTestCase(testcases.LiveServerTestCase):
client_class = APIClient
10 changes: 4 additions & 6 deletions rest_framework/utils/model_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@
from django.db import models
from django.utils import six

from rest_framework.compat import (
get_all_related_many_to_many_objects, get_all_related_objects
)

FieldInfo = namedtuple('FieldResult', [
'pk', # Model field instance
'fields', # Dict of field name -> model field instance
Expand Down Expand Up @@ -138,7 +134,8 @@ def _get_reverse_relationships(opts):
# See: https://code.djangoproject.com/ticket/24208

reverse_relations = OrderedDict()
for relation in get_all_related_objects(opts):
all_related_objects = [r for r in opts.related_objects if not r.field.many_to_many]
for relation in all_related_objects:
accessor_name = relation.get_accessor_name()
related = getattr(relation, 'related_model', relation.model)
reverse_relations[accessor_name] = RelationInfo(
Expand All @@ -150,7 +147,8 @@ def _get_reverse_relationships(opts):
)

# Deal with reverse many-to-many relationships.
for relation in get_all_related_many_to_many_objects(opts):
all_related_many_to_many_objects = [r for r in opts.related_objects if r.field.many_to_many]
for relation in all_related_many_to_many_objects:
accessor_name = relation.get_accessor_name()
related = getattr(relation, 'related_model', relation.model)
reverse_relations[accessor_name] = RelationInfo(
Expand Down
8 changes: 2 additions & 6 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import uuid
from decimal import Decimal

import django
import pytest
from django.http import QueryDict
from django.utils import six, timezone
Expand Down Expand Up @@ -951,7 +950,7 @@ class TestDateTimeField(FieldValues):
datetime.datetime(2001, 1, 1, 13, 00): datetime.datetime(2001, 1, 1, 13, 00, tzinfo=timezone.UTC()),
datetime.datetime(2001, 1, 1, 13, 00, tzinfo=timezone.UTC()): datetime.datetime(2001, 1, 1, 13, 00, tzinfo=timezone.UTC()),
# Django 1.4 does not support timezone string parsing.
'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())
'2001-01-01T13:00Z': datetime.datetime(2001, 1, 1, 13, 00, tzinfo=timezone.UTC())
}
invalid_inputs = {
'abc': ['Datetime has wrong format. Use one of these formats instead: YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HH:MM|-HH:MM|Z].'],
Expand Down Expand Up @@ -1077,8 +1076,6 @@ class TestNoOutputFormatTimeField(FieldValues):
field = serializers.TimeField(format=None)


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


# Choice types...
Expand Down
3 changes: 1 addition & 2 deletions tests/test_generics.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import unicode_literals

import django
import pytest
from django.db import models
from django.shortcuts import get_object_or_404
Expand Down Expand Up @@ -158,7 +157,7 @@ def test_post_error_root_view(self):
self.assertIn(expected_error, response.rendered_content.decode('utf-8'))


EXPECTED_QUERIES_FOR_PUT = 3 if django.VERSION < (1, 6) else 2
EXPECTED_QUERIES_FOR_PUT = 2


class TestInstanceView(TestCase):
Expand Down
6 changes: 1 addition & 5 deletions tests/test_model_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,16 @@
import decimal
from collections import OrderedDict

import django
import pytest
from django.core.exceptions import ImproperlyConfigured
from django.core.validators import (
MaxValueValidator, MinLengthValidator, MinValueValidator
)
from django.db import models
from django.db.models import DurationField as ModelDurationField
from django.test import TestCase
from django.utils import six

from rest_framework import serializers
from rest_framework.compat import DurationField as ModelDurationField
from rest_framework.compat import unicode_repr


Expand Down Expand Up @@ -341,8 +339,6 @@ class Meta:
assert implicit.data == explicit.data


@pytest.mark.skipif(django.VERSION < (1, 8),
reason='DurationField is only available for django1.8+')
class TestDurationFieldMapping(TestCase):
def test_duration_field(self):
class DurationFieldModel(models.Model):
Expand Down
4 changes: 0 additions & 4 deletions tests/test_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
"""
from __future__ import unicode_literals

import django
import pytest
from django.conf.urls import url
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.models import User
Expand Down Expand Up @@ -201,8 +199,6 @@ def test_auth_can_be_set(self):
self.assertEqual(request.auth, 'DUMMY')


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

def test_default_secure_false(self):
Expand Down