diff options
| author | Tim Graham <timograham@gmail.com> | 2013-08-19 09:35:26 -0400 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2013-08-22 17:49:11 -0400 |
| commit | 616a4d385a79d6809b618dcc8bcbda05b032d5fc (patch) | |
| tree | ee40caee925ddc8ab4823edb2611945ae66443ae /django/utils/module_loading.py | |
| parent | 1b236048b9a915296dc13081fad7bb75d0ff1f4d (diff) | |
[1.5.x] Fixed #20922 -- Allowed customizing the serializer used by contrib.sessions
Added settings.SESSION_SERIALIZER which is the import path of a serializer
to use for sessions.
Thanks apollo13, carljm, shaib, akaariai, charettes, and dstufft for reviews.
Backport of b0ce6fe656 from master
Diffstat (limited to 'django/utils/module_loading.py')
| -rw-r--r-- | django/utils/module_loading.py | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/django/utils/module_loading.py b/django/utils/module_loading.py index f8aadb34df..ede585e562 100644 --- a/django/utils/module_loading.py +++ b/django/utils/module_loading.py @@ -2,6 +2,35 @@ import imp import os import sys +from django.core.exceptions import ImproperlyConfigured +from django.utils import six +from django.utils.importlib import import_module + + +def import_by_path(dotted_path, error_prefix=''): + """ + Import a dotted module path and return the attribute/class designated by the + last name in the path. Raise ImproperlyConfigured if something goes wrong. + """ + try: + module_path, class_name = dotted_path.rsplit('.', 1) + except ValueError: + raise ImproperlyConfigured("%s%s doesn't look like a module path" % ( + error_prefix, dotted_path)) + try: + module = import_module(module_path) + except ImportError as e: + msg = '%sError importing module %s: "%s"' % ( + error_prefix, module_path, e) + six.reraise(ImproperlyConfigured, ImproperlyConfigured(msg), + sys.exc_info()[2]) + try: + attr = getattr(module, class_name) + except AttributeError: + raise ImproperlyConfigured('%sModule "%s" does not define a "%s" attribute/class' % ( + error_prefix, module_path, class_name)) + return attr + def module_has_submodule(package, module_name): """See if 'module' is in 'package'.""" |
