Remove template_render compat

Plus isort.
This commit is contained in:
Carlton Gibson 2017-10-05 16:54:34 +02:00
parent b3136427a1
commit e4f99430af
15 changed files with 40 additions and 64 deletions

View File

@ -1,7 +1,7 @@
from django.contrib.auth import get_user_model
from django.core.management.base import BaseCommand, CommandError
from rest_framework.authtoken.models import Token
from rest_framework.authtoken.models import Token
UserModel = get_user_model()

View File

@ -305,29 +305,6 @@ def set_rollback():
pass
# TODO: Remove
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.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 isinstance(template, Template):
if request:
context = RequestContext(request, context)
else:
context = Context(context)
return template.render(context)
# backends template, e.g. django.template.backends.django.Template
else:
return template.render(context, request=request)
# TODO: Remove
def set_many(instance, field, value):
if django.VERSION < (1, 10):

View File

@ -32,8 +32,7 @@ from django.utils.translation import ugettext_lazy as _
from rest_framework import ISO_8601
from rest_framework.compat import (
InvalidTimeError, MaxLengthValidator, MaxValueValidator,
MinLengthValidator, MinValueValidator, unicode_repr,
unicode_to_repr
MinLengthValidator, MinValueValidator, unicode_repr, unicode_to_repr
)
from rest_framework.exceptions import ErrorDetail, ValidationError
from rest_framework.settings import api_settings

View File

@ -16,9 +16,7 @@ from django.utils import six
from django.utils.encoding import force_text
from django.utils.translation import ugettext_lazy as _
from rest_framework.compat import (
coreapi, coreschema, distinct, guardian, template_render
)
from rest_framework.compat import coreapi, coreschema, distinct, guardian
from rest_framework.settings import api_settings
@ -129,7 +127,7 @@ class SearchFilter(BaseFilterBackend):
'term': term
}
template = loader.get_template(self.template)
return template_render(template, context)
return template.render(context)
def get_schema_fields(self, view):
assert coreapi is not None, 'coreapi must be installed to use `get_schema_fields()`'
@ -260,7 +258,7 @@ class OrderingFilter(BaseFilterBackend):
def to_html(self, request, queryset, view):
template = loader.get_template(self.template)
context = self.get_template_context(request, queryset, view)
return template_render(template, context)
return template.render(context)
def get_schema_fields(self, view):
assert coreapi is not None, 'coreapi must be installed to use `get_schema_fields()`'

View File

@ -16,7 +16,7 @@ from django.utils.encoding import force_text
from django.utils.six.moves.urllib import parse as urlparse
from django.utils.translation import ugettext_lazy as _
from rest_framework.compat import coreapi, coreschema, template_render
from rest_framework.compat import coreapi, coreschema
from rest_framework.exceptions import NotFound
from rest_framework.response import Response
from rest_framework.settings import api_settings
@ -285,7 +285,7 @@ class PageNumberPagination(BasePagination):
def to_html(self):
template = loader.get_template(self.template)
context = self.get_html_context()
return template_render(template, context)
return template.render(context)
def get_schema_fields(self, view):
assert coreapi is not None, 'coreapi must be installed to use `get_schema_fields()`'
@ -442,7 +442,7 @@ class LimitOffsetPagination(BasePagination):
def to_html(self):
template = loader.get_template(self.template)
context = self.get_html_context()
return template_render(template, context)
return template.render(context)
def get_schema_fields(self, view):
assert coreapi is not None, 'coreapi must be installed to use `get_schema_fields()`'
@ -793,7 +793,7 @@ class CursorPagination(BasePagination):
def to_html(self):
template = loader.get_template(self.template)
context = self.get_html_context()
return template_render(template, context)
return template.render(context)
def get_schema_fields(self, view):
assert coreapi is not None, 'coreapi must be installed to use `get_schema_fields()`'

View File

@ -16,7 +16,7 @@ from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.core.paginator import Page
from django.http.multipartparser import parse_header
from django.template import Template, loader
from django.template import engines, loader
from django.test.client import encode_multipart
from django.utils import six
from django.utils.html import mark_safe
@ -24,7 +24,7 @@ from django.utils.html import mark_safe
from rest_framework import VERSION, exceptions, serializers, status
from rest_framework.compat import (
INDENT_SEPARATORS, LONG_SEPARATORS, SHORT_SEPARATORS, coreapi,
pygments_css, template_render
pygments_css
)
from rest_framework.exceptions import ParseError
from rest_framework.request import is_form_media_type, override_method
@ -173,7 +173,7 @@ class TemplateHTMLRenderer(BaseRenderer):
context = self.resolve_context(data, request, response)
else:
context = self.get_template_context(data, renderer_context)
return template_render(template, context, request=request)
return template.render(context, request=request)
def resolve_template(self, template_names):
return loader.select_template(template_names)
@ -206,8 +206,9 @@ class TemplateHTMLRenderer(BaseRenderer):
return self.resolve_template(template_names)
except Exception:
# Fall back to using eg '404 Not Found'
return Template('%d %s' % (response.status_code,
response.status_text.title()))
body = '%d %s' % (response.status_code, response.status_text.title())
template = engines['django'].from_string(body)
return template
# Note, subclass TemplateHTMLRenderer simply for the exception behavior
@ -239,7 +240,7 @@ class StaticHTMLRenderer(TemplateHTMLRenderer):
context = self.resolve_context(data, request, response)
else:
context = self.get_template_context(data, renderer_context)
return template_render(template, context, request=request)
return template.render(context, request=request)
return data
@ -347,7 +348,7 @@ class HTMLFormRenderer(BaseRenderer):
template = loader.get_template(template_name)
context = {'field': field, 'style': style}
return template_render(template, context)
return template.render(context)
def render(self, data, accepted_media_type=None, renderer_context=None):
"""
@ -368,7 +369,7 @@ class HTMLFormRenderer(BaseRenderer):
'form': form,
'style': style
}
return template_render(template, context)
return template.render(context)
class BrowsableAPIRenderer(BaseRenderer):
@ -625,7 +626,7 @@ class BrowsableAPIRenderer(BaseRenderer):
template = loader.get_template(self.filter_template)
context = {'elements': elements}
return template_render(template, context)
return template.render(context)
def get_context(self, data, accepted_media_type, renderer_context):
"""
@ -705,7 +706,7 @@ class BrowsableAPIRenderer(BaseRenderer):
template = loader.get_template(self.template)
context = self.get_context(data, accepted_media_type, renderer_context)
ret = template_render(template, context, request=renderer_context['request'])
ret = template.render(context, request=renderer_context['request'])
# Munge DELETE Response code to allow us to return content
# (Do this *after* we've rendered the template so that we include
@ -741,7 +742,7 @@ class AdminRenderer(BrowsableAPIRenderer):
template = loader.get_template(self.template)
context = self.get_context(data, accepted_media_type, renderer_context)
ret = template_render(template, context, request=renderer_context['request'])
ret = template.render(context, request=renderer_context['request'])
# Creation and deletion should use redirects in the admin style.
if response.status_code == status.HTTP_201_CREATED and 'Location' in response:
@ -819,7 +820,7 @@ class DocumentationRenderer(BaseRenderer):
def render(self, data, accepted_media_type=None, renderer_context=None):
template = loader.get_template(self.template)
context = self.get_context(data, renderer_context['request'])
return template_render(template, context, request=renderer_context['request'])
return template.render(context, request=renderer_context['request'])
class SchemaJSRenderer(BaseRenderer):
@ -835,7 +836,7 @@ class SchemaJSRenderer(BaseRenderer):
template = loader.get_template(self.template)
context = {'schema': mark_safe(schema)}
request = renderer_context['request']
return template_render(template, context, request=request)
return template.render(context, request=request)
class MultiPartRenderer(BaseRenderer):

View File

@ -19,6 +19,7 @@ REST framework settings, checking for user settings first, then falling
back to the defaults.
"""
from __future__ import unicode_literals
from importlib import import_module
from django.conf import settings

View File

@ -11,8 +11,7 @@ from django.utils.html import escape, format_html, smart_urlquote
from django.utils.safestring import SafeData, mark_safe
from rest_framework.compat import (
NoReverseMatch, apply_markdown, pygments_highlight, reverse,
template_render
NoReverseMatch, apply_markdown, pygments_highlight, reverse
)
from rest_framework.renderers import HTMLFormRenderer
from rest_framework.utils.urls import replace_query_param
@ -216,11 +215,11 @@ def format_value(value):
else:
template = loader.get_template('rest_framework/admin/simple_list_value.html')
context = {'value': value}
return template_render(template, context)
return template.render(context)
elif isinstance(value, dict):
template = loader.get_template('rest_framework/admin/dict_value.html')
context = {'value': value}
return template_render(template, context)
return template.render(context)
elif isinstance(value, six.string_types):
if (
(value.startswith('http:') or value.startswith('https:')) and not

View File

@ -7,7 +7,6 @@ Usage: `get_field_info(model)` returns a `FieldInfo` instance.
"""
from collections import OrderedDict, namedtuple
FieldInfo = namedtuple('FieldResult', [
'pk', # Model field instance
'fields', # Dict of field name -> model field instance

View File

@ -16,8 +16,9 @@ from rest_framework import (
HTTP_HEADER_ENCODING, exceptions, permissions, renderers, status
)
from rest_framework.authentication import (
BaseAuthentication, BasicAuthentication, RemoteUserAuthentication, SessionAuthentication,
TokenAuthentication)
BaseAuthentication, BasicAuthentication, RemoteUserAuthentication,
SessionAuthentication, TokenAuthentication
)
from rest_framework.authtoken.models import Token
from rest_framework.authtoken.views import obtain_auth_token
from rest_framework.response import Response

View File

@ -5,7 +5,7 @@ import pytest
from django.conf.urls import url
from django.core.exceptions import ImproperlyConfigured, PermissionDenied
from django.http import Http404
from django.template import Template, TemplateDoesNotExist
from django.template import TemplateDoesNotExist, engines
from django.test import TestCase, override_settings
from django.utils import six
@ -60,12 +60,12 @@ class TemplateHTMLRendererTests(TestCase):
def get_template(template_name, dirs=None):
if template_name == 'example.html':
return Template("example: {{ object }}")
return engines['django'].from_string("example: {{ object }}")
raise TemplateDoesNotExist(template_name)
def select_template(template_name_list, dirs=None, using=None):
if template_name_list == ['example.html']:
return Template("example: {{ object }}")
return engines['django'].from_string("example: {{ object }}")
raise TemplateDoesNotExist(template_name_list[0])
django.template.loader.get_template = get_template
@ -139,9 +139,9 @@ class TemplateHTMLRendererExceptionTests(TestCase):
def get_template(template_name):
if template_name == '404.html':
return Template("404: {{ detail }}")
return engines['django'].from_string("404: {{ detail }}")
if template_name == '403.html':
return Template("403: {{ detail }}")
return engines['django'].from_string("403: {{ detail }}")
raise TemplateDoesNotExist(template_name)
django.template.loader.get_template = get_template

View File

@ -5,8 +5,6 @@ from django.test import TestCase
from rest_framework import serializers
from tests.models import RESTFrameworkModel
# Models
from tests.test_multitable_inheritance import ChildModel

View File

@ -6,8 +6,9 @@ from django.utils import six
from rest_framework import serializers
from tests.models import (
ForeignKeySource, ForeignKeyTarget, ManyToManySource, ManyToManyTarget,
NullableForeignKeySource, NullableOneToOneSource, NullableUUIDForeignKeySource,
OneToOnePKSource, OneToOneTarget, UUIDForeignKeyTarget
NullableForeignKeySource, NullableOneToOneSource,
NullableUUIDForeignKeySource, OneToOnePKSource, OneToOneTarget,
UUIDForeignKeyTarget
)

View File

@ -4,6 +4,7 @@ URLConf for test suite.
We need only the docs urls for DocumentationRenderer tests.
"""
from django.conf.urls import url
from rest_framework.documentation import include_docs_urls
urlpatterns = [

View File

@ -1,4 +1,5 @@
from django.core.exceptions import ObjectDoesNotExist
from rest_framework.compat import NoReverseMatch