web/lib/django/contrib/messages/storage/__init__.py
changeset 38 77b6da96e6f1
parent 29 cc9b7e14412b
equal deleted inserted replaced
37:8d941af65caf 38:77b6da96e6f1
       
     1 from django.conf import settings
       
     2 from django.core.exceptions import ImproperlyConfigured
       
     3 from django.utils.importlib import import_module
       
     4 
       
     5 
       
     6 def get_storage(import_path):
       
     7     """
       
     8     Imports the message storage class described by import_path, where
       
     9     import_path is the full Python path to the class.
       
    10     """
       
    11     try:
       
    12         dot = import_path.rindex('.')
       
    13     except ValueError:
       
    14         raise ImproperlyConfigured("%s isn't a Python path." % import_path)
       
    15     module, classname = import_path[:dot], import_path[dot + 1:]
       
    16     try:
       
    17         mod = import_module(module)
       
    18     except ImportError, e:
       
    19         raise ImproperlyConfigured('Error importing module %s: "%s"' %
       
    20                                    (module, e))
       
    21     try:
       
    22         return getattr(mod, classname)
       
    23     except AttributeError:
       
    24         raise ImproperlyConfigured('Module "%s" does not define a "%s" '
       
    25                                    'class.' % (module, classname))
       
    26 
       
    27 
       
    28 # Callable with the same interface as the storage classes i.e.  accepts a
       
    29 # 'request' object.  It is wrapped in a lambda to stop 'settings' being used at
       
    30 # the module level
       
    31 default_storage = lambda request: get_storage(settings.MESSAGE_STORAGE)(request)