From ec1873d0114e1bd43c66d69a97661ca4b0752e5b Mon Sep 17 00:00:00 2001
From: George Hickman <george@ghickman.co.uk>
Date: Tue, 13 Aug 2019 07:33:00 +0100
Subject: [PATCH] Always configure debug toolbar in DEBUG mode

---
 opentech/settings/dev.py           | 39 ++++++++++++++++++++++++------
 opentech/settings/local.py.example |  4 ---
 opentech/urls.py                   |  2 +-
 requirements-dev.txt               |  1 +
 4 files changed, 34 insertions(+), 12 deletions(-)

diff --git a/opentech/settings/dev.py b/opentech/settings/dev.py
index 873783137..280e44591 100644
--- a/opentech/settings/dev.py
+++ b/opentech/settings/dev.py
@@ -8,8 +8,6 @@ SECRET_KEY = 'CHANGEME!!!'
 
 WAGTAIL_CACHE = False
 
-INTERNAL_IPS = ('127.0.0.1', '10.0.2.2')
-
 ALLOWED_HOSTS = ['apply.localhost', 'localhost', '127.0.0.1', 'dev.otf.is', 'dev-apply.otf.is']
 
 BASE_URL = 'http://localhost:8000'
@@ -106,15 +104,42 @@ if LOCAL_FILE_LOGGING:
         }
     }
 
-# Set up the Django debug toolbar. See also root urls.py.
-if DEBUGTOOLBAR:
-    INSTALLED_APPS += [
+
+# Debug Toolbar
+# https://django-debug-toolbar.readthedocs.io/en/latest/index.html
+if DEBUG:
+    INSTALLED_APPS = [
+        *INSTALLED_APPS,
         'debug_toolbar',
     ]
-
     MIDDLEWARE = [
         'debug_toolbar.middleware.DebugToolbarMiddleware',
-    ] + MIDDLEWARE
+        *MIDDLEWARE,
+    ]
+    # The 10.0.x address here allows vagrant users to use the debug toolbar
+    # https://django-debug-toolbar.readthedocs.io/en/latest/installation.html#configuring-internal-ips
+    INTERNAL_IPS = ['127.0.0.1', '10.0.2.2']
+
+# We disable all panels by default here since some of them (SQL, Template,
+# Profiling) can be very CPU intensive for this site.  However disabled panels
+# can be easily toggled on in the UI.
+DEBUG_TOOLBAR_CONFIG = {
+    "DISABLE_PANELS": {
+        'debug_toolbar.panels.versions.VersionsPanel',
+        'debug_toolbar.panels.settings.SettingsPanel',
+        'debug_toolbar.panels.headers.HeadersPanel',
+        'debug_toolbar.panels.request.RequestPanel',
+        'debug_toolbar.panels.sql.SQLPanel',
+        'debug_toolbar.panels.staticfiles.StaticFilesPanel',
+        'debug_toolbar.panels.templates.TemplatesPanel',
+        'debug_toolbar.panels.cache.CachePanel',
+        'debug_toolbar.panels.signals.SignalsPanel',
+        'debug_toolbar.panels.logging.LoggingPanel',
+        'debug_toolbar.panels.redirects.RedirectsPanel',
+        'debug_toolbar.panels.profiling.ProfilingPanel',
+    },
+    "SHOW_COLLAPSED": True,
+}
 
 
 WEBPACK_LOADER['DEFAULT'].update({
diff --git a/opentech/settings/local.py.example b/opentech/settings/local.py.example
index 92f433c82..571bc3a53 100644
--- a/opentech/settings/local.py.example
+++ b/opentech/settings/local.py.example
@@ -7,10 +7,6 @@ CACHES = {
     }
 }
 
-# Use the Django debug toolbar. First install via pip.
-# pip install django-debug-toolbar
-# DEBUGTOOLBAR = True
-
 # Write log to local file.
 # LOCAL_FILE_LOGGING = True
 
diff --git a/opentech/urls.py b/opentech/urls.py
index 2aa4a198c..e94c4103b 100644
--- a/opentech/urls.py
+++ b/opentech/urls.py
@@ -65,7 +65,7 @@ urlpatterns += [
 ]
 
 
-if settings.DEBUG and settings.DEBUGTOOLBAR:
+if settings.DEBUG:
     import debug_toolbar
     urlpatterns = [
         path('__debug__/', include(debug_toolbar.urls)),
diff --git a/requirements-dev.txt b/requirements-dev.txt
index fd0aaa57b..b62935277 100644
--- a/requirements-dev.txt
+++ b/requirements-dev.txt
@@ -1,5 +1,6 @@
 -r requirements.txt
 
+django-debug-toolbar==2.0
 factory_boy==2.9.2
 Faker==1.0.8  # Pinning to avoid API changes to pydecimal in 0.x - could go higher in future
 flake8==3.7.9
-- 
GitLab