Skip to content

Remove unused compat._resolve_model() #5733

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

Merged
merged 1 commit into from
Jan 8, 2018
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
29 changes: 0 additions & 29 deletions rest_framework/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,9 @@

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

Expand Down Expand Up @@ -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
Expand Down
62 changes: 0 additions & 62 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -124,64 +120,6 @@ def test_modelviewset_resource_instance_breadcrumbs(self):
]


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
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy