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

Make utils.local_import check INSTALLED_APPS for compound paths #14

Merged
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
11 changes: 8 additions & 3 deletions rest_framework_serializer_extensions/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@ def import_local(path_to_object):
"""
path, name = path_to_object.rsplit('.', 1)

app = path.split('.')[0]

if app not in settings.INSTALLED_APPS:
path_pieces = path.split('.')
while path_pieces:
if '.'.join(path_pieces) not in settings.INSTALLED_APPS:
path_pieces.pop()
else:
break

if not path_pieces:
raise AssertionError(
"Cannot import from outside installed apps"
)
Expand Down
Empty file added test_package/__init__.py
Empty file.
Empty file.
2 changes: 2 additions & 0 deletions test_package/test_module/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class TestSerializer(object):
pass
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def pytest_configure():
'rest_framework.authtoken',
'rest_framework_serializer_extensions',
'tests',
'test_package.test_module',
),
PASSWORD_HASHERS=(
'django.contrib.auth.hashers.SHA1PasswordHasher',
Expand Down
7 changes: 7 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.test import override_settings, TestCase

from rest_framework_serializer_extensions import fields, utils
from test_package.test_module.serializers import TestSerializer
from tests import models
from tests.base import TEST_HASH_IDS

Expand All @@ -30,6 +31,12 @@ def test_import_within_installed_apps(self):
)
self.assertIs(imported, fields.HashIdField)

def test_import_complex_path_within_installed_apps(self):
imported = utils.import_local(
'test_package.test_module.serializers.TestSerializer'
)
self.assertIs(imported, TestSerializer)


class GetSettingTests(TestCase):
"""
Expand Down