from django.utils.importlib import import_module
from django.core.exceptions import MiddlewareNotUsed
from django.utils.functional import memoize

from . import app_settings

def get_path(path):
    module_name, attr = path.rsplit('.', 1)

    module = import_module(module_name)

    return getattr(module, attr)

def get_backend():
    return get_path(app_settings.BACKEND)()

def get_middleware():
    middleware = []

    for path in app_settings.MIDDLEWARE:
        try:
            middleware.append(get_path(path)())
        except MiddlewareNotUsed:
            pass

    return middleware

try:
    import setproctitle

    original_title = setproctitle.getproctitle()

    def set_process_title(title):
        setproctitle.setproctitle("%s [%s]" % (original_title, title))
except ImportError:
    def set_process_title(title):
        pass

get_path = memoize(get_path, {}, 1)
get_backend = memoize(get_backend, {}, 0)
get_middleware = memoize(get_middleware, {}, 0)

@@ -9,44 +9,33 @@
 ngo.
-utils.importlib import import_module
+db.models import get_apps
 %0Afro
@@ -106,32 +106,142 @@
 ils.
-functional import memoiz
+importlib import import_module%0Afrom django.utils.functional import memoize%0Afrom django.utils.module_loading import module_has_submodul
 e%0A%0Af
@@ -691,16 +691,517 @@
 leware%0A%0A
+def import_all_submodules(name):%0A    for app_module in get_apps():%0A        parts = app_module.__name__.split('.')%0A        prefix, last = parts%5B:-1%5D, parts%5B-1%5D%0A%0A        try:%0A            import_module('.'.join(prefix + %5Bname%5D))%0A        except ImportError:%0A            # Distinguise between tasks.py existing and failing to import%0A            if last == 'models':%0A                app_module = import_module('.'.join(prefix))%0A%0A            if module_has_submodule(app_module, name):%0A                raise%0A%0A
 try:%0A   

Add utility to import all submodules of all apps.