Remove unused compat._resolve_model() (#5733)

Last use removed in c674687782.
This commit is contained in:
Jon Dufresne 2018-01-08 01:19:08 -08:00 committed by Tom Christie
parent 522d453546
commit 06e2ad0b7d
2 changed files with 0 additions and 91 deletions

View File

@ -5,14 +5,9 @@ versions of Django/Python, and compatibility wrappers around optional packages.
from __future__ import unicode_literals
import inspect
import django
from django.apps import apps
from django.conf import settings
from django.core import validators
from django.core.exceptions import ImproperlyConfigured
from django.db import models
from django.utils import six
from django.views.generic import View
@ -107,30 +102,6 @@ def distinct(queryset, base):
return queryset.distinct()
def _resolve_model(obj):
"""
Resolve supplied `obj` to a Django model class.
`obj` must be a Django model class itself, or a string
representation of one. Useful in situations like GH #1225 where
Django may not have resolved a string-based reference to a model in
another model's foreign key definition.
String representations should have the format:
'appname.ModelName'
"""
if isinstance(obj, six.string_types) and len(obj.split('.')) == 2:
app_name, model_name = obj.split('.')
resolved_model = apps.get_model(app_name, model_name)
if resolved_model is None:
msg = "Django did not return a model for {0}.{1}"
raise ImproperlyConfigured(msg.format(app_name, model_name))
return resolved_model
elif inspect.isclass(obj) and issubclass(obj, models.Model):
return obj
raise ValueError("{0} is not a Django model".format(obj))
# django.contrib.postgres requires psycopg2
try:
from django.contrib.postgres import fields as postgres_fields

View File

@ -2,12 +2,8 @@
from __future__ import unicode_literals
from django.conf.urls import url
from django.core.exceptions import ImproperlyConfigured
from django.test import TestCase, override_settings
from django.utils import six
import rest_framework.utils.model_meta
from rest_framework.compat import _resolve_model
from rest_framework.routers import SimpleRouter
from rest_framework.serializers import ModelSerializer
from rest_framework.utils import json
@ -124,64 +120,6 @@ class BreadcrumbTests(TestCase):
]
class ResolveModelTests(TestCase):
"""
`_resolve_model` should return a Django model class given the
provided argument is a Django model class itself, or a properly
formatted string representation of one.
"""
def test_resolve_django_model(self):
resolved_model = _resolve_model(BasicModel)
assert resolved_model == BasicModel
def test_resolve_string_representation(self):
resolved_model = _resolve_model('tests.BasicModel')
assert resolved_model == BasicModel
def test_resolve_unicode_representation(self):
resolved_model = _resolve_model(six.text_type('tests.BasicModel'))
assert resolved_model == BasicModel
def test_resolve_non_django_model(self):
with self.assertRaises(ValueError):
_resolve_model(TestCase)
def test_resolve_improper_string_representation(self):
with self.assertRaises(ValueError):
_resolve_model('BasicModel')
class ResolveModelWithPatchedDjangoTests(TestCase):
"""
Test coverage for when Django's `get_model` returns `None`.
Under certain circumstances Django may return `None` with `get_model`:
http://git.io/get-model-source
It usually happens with circular imports so it is important that DRF
excepts early, otherwise fault happens downstream and is much more
difficult to debug.
"""
def setUp(self):
"""Monkeypatch get_model."""
self.get_model = rest_framework.compat.apps.get_model
def get_model(app_label, model_name):
return None
rest_framework.compat.apps.get_model = get_model
def tearDown(self):
"""Revert monkeypatching."""
rest_framework.compat.apps.get_model = self.get_model
def test_blows_up_if_model_does_not_resolve(self):
with self.assertRaises(ImproperlyConfigured):
_resolve_model('tests.BasicModel')
class JsonFloatTests(TestCase):
"""
Internaly, wrapped json functions should adhere to strict float handling