From 4a188464e47e086ec8ad500e35ff7ed1dfa8fa30 Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Sun, 7 Jan 2018 07:15:36 -0800 Subject: [PATCH] Remove unused compat._resolve_model() Last use removed in c674687782ef96a3bb8466b412e99debd3d04f00. --- rest_framework/compat.py | 29 ------------------- tests/test_utils.py | 62 ---------------------------------------- 2 files changed, 91 deletions(-) diff --git a/rest_framework/compat.py b/rest_framework/compat.py index 9870fe77e..88a76000e 100644 --- a/rest_framework/compat.py +++ b/rest_framework/compat.py @@ -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 diff --git a/tests/test_utils.py b/tests/test_utils.py index 4aed5ee73..d63f266d6 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -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