Skip to content

Commit

Permalink
get_classes now allows top-level apps.
Browse files Browse the repository at this point in the history
Fixes #614
  • Loading branch information
maiksprenger committed May 9, 2013
1 parent 602eb61 commit cdd8af3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
7 changes: 5 additions & 2 deletions oscar/core/loading.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,11 @@ class can't be found in the overriding module, then we attempt to import it
# App must be local - check if module is in local app (it could be in
# oscar's)
app_label = module_label.split('.')[0]
base_package = app_module_path.rsplit('.' + app_label, 1)[0]
local_app = "%s.%s" % (base_package, module_label)
if '.' in app_module_path:
base_package = app_module_path.rsplit('.' + app_label, 1)[0]
local_app = "%s.%s" % (base_package, module_label)
else:
local_app = module_label
try:
imported_local_module = __import__(local_app, fromlist=classnames)
except ImportError:
Expand Down
17 changes: 17 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import sys


class temporary_python_path(object):
"""
Acts as a context manager to temporarily append a list of paths to sys.path
"""

def __init__(self, paths):
self.paths = paths

def __enter__(self):
self.original_paths = sys.path[:]
sys.path = self.paths + self.original_paths

def __exit__(self, exc_type, exc_value, traceback):
sys.path = self.original_paths
11 changes: 11 additions & 0 deletions tests/unit/core/loading_tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from os.path import dirname
from django.test import TestCase
from django.core.exceptions import ValidationError
from django.conf import settings
Expand All @@ -9,6 +10,7 @@
get_classes, get_class, ClassNotFoundError
from oscar.core.validators import ExtendedURLValidator
from oscar.core.validators import URLDoesNotExistValidator
from tests import temporary_python_path


class TestImportModule(TestCase):
Expand Down Expand Up @@ -78,6 +80,15 @@ def test_loading_classes_defined_in_both_local_and_oscar_modules(self):
self.assertEqual('tests._site.shipping.methods', Free.__module__)
self.assertEqual('oscar.apps.shipping.methods', FixedPrice.__module__)

def test_loading_classes_with_root_app(self):
import tests._site.shipping
path = dirname(dirname(tests._site.shipping.__file__))
with temporary_python_path([path]):
self.installed_apps[self.installed_apps.index('tests._site.shipping')] = 'shipping'
with override_settings(INSTALLED_APPS=self.installed_apps):
(Free,) = get_classes('shipping.methods', ('Free',))
self.assertEqual('shipping.methods', Free.__module__)


class TestExtendedURLValidator(TestCase):
"""
Expand Down

0 comments on commit cdd8af3

Please sign in to comment.