From 9370b2178e7086a4732a887d9ae05f307f4edc30 Mon Sep 17 00:00:00 2001 From: Adam Dobrawy Date: Thu, 5 Apr 2018 22:43:57 +0200 Subject: [PATCH 1/3] Validate duplicate middleware load --- debug_toolbar/apps.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/debug_toolbar/apps.py b/debug_toolbar/apps.py index 6b9f71058..92ffad2c4 100644 --- a/debug_toolbar/apps.py +++ b/debug_toolbar/apps.py @@ -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' @@ -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( @@ -46,7 +46,16 @@ 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: + 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( From 64cf27879514a6eb9a434f6c69dbf3af98ce83d7 Mon Sep 17 00:00:00 2001 From: Adam Dobrawy Date: Fri, 13 Apr 2018 19:16:06 +0200 Subject: [PATCH 2/3] Fix flake8 issues --- debug_toolbar/apps.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/debug_toolbar/apps.py b/debug_toolbar/apps.py index 92ffad2c4..a8535c6a9 100644 --- a/debug_toolbar/apps.py +++ b/debug_toolbar/apps.py @@ -47,14 +47,14 @@ def check_middleware(app_configs, **kwargs): ) ) elif len(debug_toolbar_indexes) != 1: - errors.append( + 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( From 1d90573304da8bba2b9c7422af1552e9ab9683de Mon Sep 17 00:00:00 2001 From: Adam Dobrawy Date: Fri, 13 Apr 2018 19:27:20 +0200 Subject: [PATCH 3/3] Add comment in apps.py --- debug_toolbar/apps.py | 1 + 1 file changed, 1 insertion(+) diff --git a/debug_toolbar/apps.py b/debug_toolbar/apps.py index a8535c6a9..5bafec45d 100644 --- a/debug_toolbar/apps.py +++ b/debug_toolbar/apps.py @@ -47,6 +47,7 @@ def check_middleware(app_configs, **kwargs): ) ) elif len(debug_toolbar_indexes) != 1: + # If the toolbar appears multiple times, report an error. errors.append( Error( "debug_toolbar.middleware.DebugToolbarMiddleware occurs "