Skip to content

Validate duplicate middleware load #1052

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 13, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions debug_toolbar/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def check_middleware(app_configs, **kwargs):

errors = []
gzip_index = None
debug_toolbar_index = None
debug_toolbar_indexes = []

setting = getattr(settings, 'MIDDLEWARE', None)
setting_name = 'MIDDLEWARE'
Expand All @@ -34,9 +34,9 @@ def check_middleware(app_configs, **kwargs):
if is_middleware_class(GZipMiddleware, middleware):
gzip_index = i
elif is_middleware_class(DebugToolbarMiddleware, middleware):
debug_toolbar_index = i
debug_toolbar_indexes.append(i)

if debug_toolbar_index is None:
if not debug_toolbar_indexes:
# If the toolbar does not appear, report an error.
errors.append(
Error(
Expand All @@ -46,7 +46,17 @@ def check_middleware(app_configs, **kwargs):
"%s." % setting_name,
)
)
elif gzip_index is not None and debug_toolbar_index < gzip_index:
elif len(debug_toolbar_indexes) != 1:
# If the toolbar appears multiple times, report an error.
errors.append(
Error(
"debug_toolbar.middleware.DebugToolbarMiddleware occurs "
"multiple times in %s." % setting_name,
hint="Load debug_toolbar.middleware.DebugToolbarMiddleware only "
"once in %s." % setting_name,
)
)
elif gzip_index is not None and debug_toolbar_indexes[0] < gzip_index:
# If the toolbar appears before the gzip index, report an error.
errors.append(
Error(
Expand Down