Created
November 28, 2017 03:29
-
-
Save lyhapple/3b4cb50c865673b750e65bc58e8b9eb5 to your computer and use it in GitHub Desktop.
django自动注入app中间件
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 限制条件:中间件类名必须以Middleware结尾 | |
APP_ROOT = os.path.join(BASE_DIR, 'app') | |
APP_DIR_LIST = os.listdir(APP_ROOT) | |
APPS = {} # {'account': '/var/test/account'} | |
for app_name in APP_DIR_LIST: | |
if app_name == 'tpl': | |
continue | |
app = os.path.join(APP_ROOT, app_name) | |
if os.path.isdir(app): | |
APPS[app_name] = app | |
for app_name, app_dir in APPS.iteritems(): | |
mw_file = os.path.join(app_dir, 'middleware.py') | |
if os.path.exists(mw_file): | |
mw_module = importlib.import_module('%s.middleware' % app_name) | |
mw_all = mw_module.__dict__ | |
classes = dir(mw_module) | |
for c in classes: | |
if c.endswith('Middleware'): | |
MIDDLEWARE.append('%s.middleware.%s' % (app_name, c)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment