From bc6e2bda0ae1942bc9563ae21fb541e45bb8da06 Mon Sep 17 00:00:00 2001 From: Matthew Holmes Date: Tue, 3 Sep 2024 10:26:06 +0100 Subject: [PATCH] Enable Django Debug Toolbar in deployed environments. --- config/settings.py | 17 +++++++++++++++++ config/settings_local.py | 29 ----------------------------- config/settings_test.py | 4 ++++ config/urls.py | 2 +- requirements-base.txt | 1 + requirements-dev.txt | 1 - 6 files changed, 23 insertions(+), 31 deletions(-) diff --git a/config/settings.py b/config/settings.py index 9cfb47322..9b84d6554 100644 --- a/config/settings.py +++ b/config/settings.py @@ -528,3 +528,20 @@ "Apply for an export certificate": env.gtm_exporter_container_id, "Apply for an import licence": env.gtm_importer_container_id, } + +# Djando debug toolbar config. +# Almost exclusively used locally but can be temporarily enabled in deployed environments +SHOW_DEBUG_TOOLBAR = env.show_debug_toolbar +if SHOW_DEBUG_TOOLBAR: + INSTALLED_APPS += [ + "debug_toolbar", + ] + + MIDDLEWARE += [ + "debug_toolbar.middleware.DebugToolbarMiddleware", + ] + + DEBUG_TOOLBAR_CONFIG = { + "INTERCEPT_REDIRECTS": False, + "SHOW_TOOLBAR_CALLBACK": lambda x: SHOW_DEBUG_TOOLBAR, + } diff --git a/config/settings_local.py b/config/settings_local.py index 9b2dbb018..03811b0ae 100644 --- a/config/settings_local.py +++ b/config/settings_local.py @@ -10,45 +10,16 @@ AWS_STORAGE_BUCKET_NAME = "icms.local" AWS_S3_ENDPOINT_URL = env.local_aws_s3_endpoint_url -# Debug toolbar config -INTERNAL_IPS = ("127.0.0.1", "localhost") INSTALLED_APPS += [ - "debug_toolbar", "django_extensions", ] -MIDDLEWARE += [ - "debug_toolbar.middleware.DebugToolbarMiddleware", -] - # When debugging queries in console SHOW_DB_QUERIES = env.show_db_queries if SHOW_DB_QUERIES: MIDDLEWARE += ["web.middleware.common.DBQueriesMiddleware"] -DEBUG_TOOLBAR_PANELS = [ - "debug_toolbar.panels.versions.VersionsPanel", - "debug_toolbar.panels.timer.TimerPanel", - "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.cache.CachePanel", - "debug_toolbar.panels.signals.SignalsPanel", - "debug_toolbar.panels.logging.LoggingPanel", - "debug_toolbar.panels.redirects.RedirectsPanel", - "debug_toolbar.panels.profiling.ProfilingPanel", -] - -SHOW_DEBUG_TOOLBAR = env.show_debug_toolbar - -DEBUG_TOOLBAR_CONFIG = { - "INTERCEPT_REDIRECTS": False, - "SHOW_TOOLBAR_CALLBACK": lambda x: SHOW_DEBUG_TOOLBAR, -} - NPM_STATIC_FILES_LOCATION = "web/static/3rdparty" NPM_FILE_PATTERNS = { diff --git a/config/settings_test.py b/config/settings_test.py index bd6d02f25..42ef2f6e0 100644 --- a/config/settings_test.py +++ b/config/settings_test.py @@ -67,3 +67,7 @@ GOV_UK_ONE_LOGIN_AUTHENTICATION_LEVEL = one_login_types.AuthenticationLevel.MEDIUM_LEVEL SAVE_GENERATED_PDFS = env.save_generated_pdfs + +# Disable toolbar if enabled when running tests. +if SHOW_DEBUG_TOOLBAR: + DEBUG_TOOLBAR_CONFIG["SHOW_TOOLBAR_CALLBACK"] = lambda _: False diff --git a/config/urls.py b/config/urls.py index d7e8dd324..ab04a4f77 100644 --- a/config/urls.py +++ b/config/urls.py @@ -36,7 +36,7 @@ path("", include("web.urls")), ] -if settings.DEBUG: +if settings.SHOW_DEBUG_TOOLBAR: import debug_toolbar urlpatterns = [path("__debug__/", include(debug_toolbar.urls))] + urlpatterns diff --git a/requirements-base.txt b/requirements-base.txt index c54f36a11..f38d67984 100644 --- a/requirements-base.txt +++ b/requirements-base.txt @@ -6,6 +6,7 @@ dj-database-url==2.1.0 django-celery-results==2.5.1 django-chunk-upload-handlers==0.0.14 django-compressor==4.4 +django-debug-toolbar~=4.4 django-filter==23.4 django-guardian==2.4.0 django-htmlmin==0.11.0 diff --git a/requirements-dev.txt b/requirements-dev.txt index 7efeb2801..8a7680726 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -3,7 +3,6 @@ # add packages needed only for development black==24.4.2 -django-debug-toolbar==4.2 django-extensions==3.2.3 factory-boy==3.0.1 faker==4.1.2