Explorar el Código

get_classes now allows top-level apps.

Fixes #614
master
Maik Hoepfel hace 12 años
padre
commit
7b809d3a5f
Se han modificado 3 ficheros con 39 adiciones y 6 borrados
  1. 5
    2
      oscar/core/loading.py
  2. 17
    0
      tests/__init__.py
  3. 17
    4
      tests/unit/core/loading_tests.py

+ 5
- 2
oscar/core/loading.py Ver fichero

@@ -44,8 +44,11 @@ def get_classes(module_label, classnames):
44 44
     # App must be local - check if module is in local app (it could be in
45 45
     # oscar's)
46 46
     app_label = module_label.split('.')[0]
47
-    base_package = app_module_path.rsplit('.' + app_label, 1)[0]
48
-    local_app = "%s.%s" % (base_package, module_label)
47
+    if '.' in app_module_path:
48
+        base_package = app_module_path.rsplit('.' + app_label, 1)[0]
49
+        local_app = "%s.%s" % (base_package, module_label)
50
+    else:
51
+        local_app = module_label
49 52
     try:
50 53
         imported_local_module = __import__(local_app, fromlist=classnames)
51 54
     except ImportError:

+ 17
- 0
tests/__init__.py Ver fichero

@@ -0,0 +1,17 @@
1
+import sys
2
+
3
+
4
+class temporary_python_path(object):
5
+    """
6
+    Acts as a context manager to temporarily prepend a list of paths to
7
+    sys.path
8
+    """
9
+    def __init__(self, paths):
10
+        self.paths = paths
11
+
12
+    def __enter__(self):
13
+        self.original_paths = sys.path[:]
14
+        sys.path = self.paths + self.original_paths
15
+
16
+    def __exit__(self, exc_type, exc_value, traceback):
17
+        sys.path = self.original_paths

+ 17
- 4
tests/unit/core/loading_tests.py Ver fichero

@@ -1,3 +1,4 @@
1
+from os.path import dirname
1 2
 from django.test import TestCase
2 3
 from django.core.exceptions import ValidationError
3 4
 from django.conf import settings
@@ -5,10 +6,12 @@ from django.contrib.flatpages.models import FlatPage
5 6
 from django.test.utils import override_settings
6 7
 
7 8
 import oscar
8
-from oscar.core.loading import import_module, AppNotFoundError, \
9
-        get_classes, get_class, ClassNotFoundError
10
-from oscar.core.validators import ExtendedURLValidator
11
-from oscar.core.validators import URLDoesNotExistValidator
9
+from oscar.core.loading import (
10
+    import_module, AppNotFoundError, get_classes,
11
+    get_class, ClassNotFoundError)
12
+from oscar.core.validators import (
13
+    ExtendedURLValidator, URLDoesNotExistValidator)
14
+from tests import temporary_python_path
12 15
 
13 16
 
14 17
 class TestImportModule(TestCase):
@@ -78,6 +81,16 @@ class ClassLoadingWithLocalOverrideTests(TestCase):
78 81
             self.assertEqual('tests._site.shipping.methods', Free.__module__)
79 82
             self.assertEqual('oscar.apps.shipping.methods', FixedPrice.__module__)
80 83
 
84
+    def test_loading_classes_with_root_app(self):
85
+        import tests._site.shipping
86
+        path = dirname(dirname(tests._site.shipping.__file__))
87
+        with temporary_python_path([path]):
88
+            self.installed_apps[
89
+                self.installed_apps.index('tests._site.shipping')] = 'shipping'
90
+            with override_settings(INSTALLED_APPS=self.installed_apps):
91
+                (Free,) = get_classes('shipping.methods', ('Free',))
92
+                self.assertEqual('shipping.methods', Free.__module__)
93
+
81 94
 
82 95
 class TestExtendedURLValidator(TestCase):
83 96
     """

Loading…
Cancelar
Guardar