From 7cff202d864a09cc7519d72f9d82ea022564c368 Mon Sep 17 00:00:00 2001 From: ssorin Date: Fri, 13 Dec 2024 17:32:29 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=91=E2=80=8D=F0=9F=92=BB(dashboard)=20?= =?UTF-8?q?add=20Django=20Debug=20Toolbar=20for=20debugging=20in=20develop?= =?UTF-8?q?ment?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Integrated Django Debug Toolbar for enhanced debugging during development. Updated settings, middleware, and `urls.py` to conditionally enable the toolbar when `DEBUG` is true. Also added the package to `Pipfile` and updated the lock file. --- src/dashboard/Pipfile | 1 + src/dashboard/Pipfile.lock | 12 +++++-- src/dashboard/dashboard/settings.py | 50 +++++++++++++++++++++++++++++ src/dashboard/dashboard/urls.py | 8 +++++ 4 files changed, 69 insertions(+), 2 deletions(-) diff --git a/src/dashboard/Pipfile b/src/dashboard/Pipfile index 34c870af..92399463 100644 --- a/src/dashboard/Pipfile +++ b/src/dashboard/Pipfile @@ -15,6 +15,7 @@ whitenoise = "==6.8.2" [dev-packages] black = "==24.10.0" +django-debug-toolbar = "==4.4.6" django-stubs = {extras = ["compatible-mypy"], version = "==5.1.1"} djlint = "==1.36.3" factory-boy = "==3.3.1" diff --git a/src/dashboard/Pipfile.lock b/src/dashboard/Pipfile.lock index 7fa0c07a..4956868c 100644 --- a/src/dashboard/Pipfile.lock +++ b/src/dashboard/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "7bab31f60f4278df10774d080bdfe862b7e54443ceae7061468a47433a6be002" + "sha256": "ca97e618f77ebd4787cd983ff2e7ba719a7d41fe8e2a01242250e6544c0b186d" }, "pipfile-spec": 6, "requires": { @@ -515,10 +515,18 @@ "sha256:236e023f021f5ce7dee5779de7b286565fdea5f4ab86bae5338e3f7b69896cf0", "sha256:de450c09e91879fa5a307f696e57c851955c910a438a35e6b4c895e86bedc82a" ], - "index": "pypi", "markers": "python_version >= '3.10'", "version": "==5.1.4" }, + "django-debug-toolbar": { + "hashes": [ + "sha256:36e421cb908c2f0675e07f9f41e3d1d8618dc386392ec82d23bcfcd5d29c7044", + "sha256:3beb671c9ec44ffb817fad2780667f172bd1c067dbcabad6268ce39a81335f45" + ], + "index": "pypi", + "markers": "python_version >= '3.8'", + "version": "==4.4.6" + }, "django-stubs": { "extras": [ "compatible-mypy" diff --git a/src/dashboard/dashboard/settings.py b/src/dashboard/dashboard/settings.py index 280b4d57..932639bd 100644 --- a/src/dashboard/dashboard/settings.py +++ b/src/dashboard/dashboard/settings.py @@ -9,6 +9,7 @@ https://docs.djangoproject.com/en/5.1/ref/settings/ """ +import sys from pathlib import Path import environ @@ -16,6 +17,9 @@ from django.utils.translation import gettext_lazy as _ from sentry_sdk.integrations.django import DjangoIntegration +# is pytest running +TESTING = "pytest" in sys.modules + # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent @@ -169,3 +173,49 @@ # django.contrib.auth) you may enable sending PII data. send_default_pii=True, ) + +# Debug-toolbar + +# Despite the `DEBUG` being set to `False`, for some tests, +# pytest seems to consider `DEBUG` to be `True`. +# So, if `pytest` is running, we don't activate `debug_toolbar`. +if DEBUG and not TESTING: + INTERNAL_IPS = ("localhost",) + INSTALLED_APPS += ("debug_toolbar",) + + def show_toolbar(request): + """Force display of debug toolbar. + + The default use `SHOW_TOOLBAR_CALLBACK` has no effect. + `SHOW_TOOLBAR_CALLBACK` checks if `DEBUG` is `True` and if the `IP` of the + request is in `INTERNAL_IPS` but without effect here. + (https://django-debug-toolbar.readthedocs.io/en/latest/configuration.html#debug-toolbar-config). + """ + return True + + DEBUG_TOOLBAR_CONFIG = { + "SHOW_TOOLBAR_CALLBACK": show_toolbar, + "INTERCEPT_REDIRECTS": False, + } + + DEBUG_TOOLBAR_PANELS = ( + "debug_toolbar.panels.history.HistoryPanel", + "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.templates.TemplatesPanel", + "debug_toolbar.panels.alerts.AlertsPanel", + "debug_toolbar.panels.cache.CachePanel", + "debug_toolbar.panels.signals.SignalsPanel", + "debug_toolbar.panels.redirects.RedirectsPanel", + "debug_toolbar.panels.profiling.ProfilingPanel", + ) + + MIDDLEWARE = [ + "debug_toolbar.middleware.DebugToolbarMiddleware", + *MIDDLEWARE, + ] diff --git a/src/dashboard/dashboard/urls.py b/src/dashboard/dashboard/urls.py index 87f6266d..3da24562 100644 --- a/src/dashboard/dashboard/urls.py +++ b/src/dashboard/dashboard/urls.py @@ -15,6 +15,7 @@ 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ +from django.conf import settings from django.contrib import admin from django.urls import include, path @@ -27,3 +28,10 @@ # internationalization path("i18n/", include("django.conf.urls.i18n")), ] + +if settings.DEBUG and not settings.TESTING: + import debug_toolbar + + urlpatterns += [ + path("__debug__/", include(debug_toolbar.urls)), + ]