Commit 94700a6c authored by Diederik van der Boor's avatar Diederik van der Boor
Browse files

Replace remaining `import_symbol()` with Django 1.7's `import_string()`

parent 317e92ff
...@@ -7,15 +7,14 @@ from django.conf import settings ...@@ -7,15 +7,14 @@ from django.conf import settings
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
from django.http import HttpResponse from django.http import HttpResponse
from django.utils.lru_cache import lru_cache from django.utils.lru_cache import lru_cache
from django.utils.module_loading import import_string
from django.views.static import serve from django.views.static import serve
from .utils import import_symbol
@lru_cache() @lru_cache()
def get_server_class(path): def get_server_class(path):
if '.' in path: if '.' in path:
return import_symbol(path) return import_string(path)
elif path == 'django': elif path == 'django':
return DjangoServer return DjangoServer
elif path == 'apache': elif path == 'apache':
......
from django.core.exceptions import ImproperlyConfigured
try:
from importlib import import_module # Python 2.7+
except ImportError:
from django.utils.importlib import import_module
def import_symbol(import_path):
"""
Import a class or attribute by name.
"""
try:
dot = import_path.rindex('.')
except ValueError:
raise ImproperlyConfigured("{0} isn't a Python path.".format(import_path))
module, classname = import_path[:dot], import_path[dot + 1:]
try:
mod = import_module(module)
except ImportError as e:
raise ImproperlyConfigured('Error importing module {0}: "{1}"'.format(module, e))
try:
return getattr(mod, classname)
except AttributeError:
raise ImproperlyConfigured('Module "{0}" does not define a "{1}" class.'.format(module, classname))
...@@ -2,13 +2,13 @@ ...@@ -2,13 +2,13 @@
Views to send private files. Views to send private files.
""" """
from django.http import HttpResponseForbidden, Http404 from django.http import HttpResponseForbidden, Http404
from django.utils.module_loading import import_string
from django.views.generic import View from django.views.generic import View
from . import appconfig from . import appconfig
from .models import PrivateFile from .models import PrivateFile
from .servers import get_server_class from .servers import get_server_class
from .storage import private_storage from .storage import private_storage
from .utils import import_symbol
class PrivateStorageView(View): class PrivateStorageView(View):
...@@ -20,7 +20,7 @@ class PrivateStorageView(View): ...@@ -20,7 +20,7 @@ class PrivateStorageView(View):
storage = private_storage storage = private_storage
#: The authorisation rule for accessing #: The authorisation rule for accessing
can_access_file = staticmethod(import_symbol(appconfig.PRIVATE_STORAGE_AUTH_FUNCTION)) can_access_file = staticmethod(import_string(appconfig.PRIVATE_STORAGE_AUTH_FUNCTION))
#: Import the server class once #: Import the server class once
server_class = get_server_class(appconfig.PRIVATE_STORAGE_SERVER) server_class = get_server_class(appconfig.PRIVATE_STORAGE_SERVER)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment