Fixed #17061 -- Factored out importing object from a dotted path

Thanks Carl Meyer for the report.
This commit is contained in:
Claude Paroz
2013-02-02 22:58:02 +01:00
parent 4279e13bd3
commit 792e42c1ef
3 changed files with 11 additions and 26 deletions

View File

@@ -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')

View File

@@ -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)

View File

@@ -1,9 +1,6 @@
from django.core.exceptions import ImproperlyConfigured
class MissingStorageModule(ImproperlyConfigured):
pass
class MissingStorageClass(ImproperlyConfigured):
class MissingStorage(ImproperlyConfigured):
pass
class NoFileStorageConfigured(ImproperlyConfigured):