Fixed #17061 -- Factored out importing object from a dotted path
Thanks Carl Meyer for the report.
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
from django.test import TestCase
|
||||
|
||||
from django.contrib.formtools.wizard.storage import (get_storage,
|
||||
MissingStorageModule,
|
||||
MissingStorageClass)
|
||||
from django.contrib.formtools.wizard.storage import get_storage, MissingStorage
|
||||
from django.contrib.formtools.wizard.storage.base import BaseStorage
|
||||
|
||||
|
||||
@@ -12,11 +10,9 @@ class TestLoadStorage(TestCase):
|
||||
type(get_storage('django.contrib.formtools.wizard.storage.base.BaseStorage', 'wizard1')),
|
||||
BaseStorage)
|
||||
|
||||
def test_missing_module(self):
|
||||
self.assertRaises(MissingStorageModule, get_storage,
|
||||
def test_missing_storage(self):
|
||||
self.assertRaises(MissingStorage, get_storage,
|
||||
'django.contrib.formtools.wizard.storage.idontexist.IDontExistStorage', 'wizard1')
|
||||
|
||||
def test_missing_class(self):
|
||||
self.assertRaises(MissingStorageClass, get_storage,
|
||||
self.assertRaises(MissingStorage, get_storage,
|
||||
'django.contrib.formtools.wizard.storage.base.IDontExistStorage', 'wizard1')
|
||||
|
||||
|
||||
@@ -1,22 +1,14 @@
|
||||
from django.utils.importlib import import_module
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
from django.utils.module_loading import import_by_path
|
||||
|
||||
from django.contrib.formtools.wizard.storage.base import BaseStorage
|
||||
from django.contrib.formtools.wizard.storage.exceptions import (
|
||||
MissingStorageModule, MissingStorageClass, NoFileStorageConfigured)
|
||||
MissingStorage, NoFileStorageConfigured)
|
||||
|
||||
|
||||
def get_storage(path, *args, **kwargs):
|
||||
i = path.rfind('.')
|
||||
module, attr = path[:i], path[i+1:]
|
||||
try:
|
||||
mod = import_module(module)
|
||||
except ImportError as e:
|
||||
raise MissingStorageModule(
|
||||
'Error loading storage %s: "%s"' % (module, e))
|
||||
try:
|
||||
storage_class = getattr(mod, attr)
|
||||
except AttributeError:
|
||||
raise MissingStorageClass(
|
||||
'Module "%s" does not define a storage named "%s"' % (module, attr))
|
||||
storage_class = import_by_path(path)
|
||||
except ImproperlyConfigured as e:
|
||||
raise MissingStorage('Error loading storage: %s' % e)
|
||||
return storage_class(*args, **kwargs)
|
||||
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
|
||||
class MissingStorageModule(ImproperlyConfigured):
|
||||
pass
|
||||
|
||||
class MissingStorageClass(ImproperlyConfigured):
|
||||
class MissingStorage(ImproperlyConfigured):
|
||||
pass
|
||||
|
||||
class NoFileStorageConfigured(ImproperlyConfigured):
|
||||
|
||||
Reference in New Issue
Block a user