Skip to content

Commit

Permalink
🧑‍💻(dashboard) add Django Debug Toolbar for debugging in development
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ssorin committed Dec 17, 2024
1 parent 0d31e77 commit 21bc253
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 2 deletions.
1 change: 1 addition & 0 deletions .github/workflows/dashboard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ jobs:
POSTGRES_DB: test-qualicharge-dashboard
POSTGRES_USER: qualicharge
POSTGRES_PASSWORD: pass
TEST: true
options: >-
--health-cmd pg_isready
--health-interval 10s
Expand Down
2 changes: 2 additions & 0 deletions bin/pytest
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

set -eo pipefail

declare TEST=true
declare service=${SERVICE:-api}
declare cmd
declare DOCKER_USER
Expand All @@ -20,5 +21,6 @@ DOCKER_USER=${DOCKER_USER} \
DOCKER_GID=${DOCKER_GID} \
docker compose run --rm \
-e QUALICHARGE_OIDC_IS_ENABLED=True \
-e TEST="${TEST}" \
"${service}" \
${cmd} "$@"
1 change: 1 addition & 0 deletions src/dashboard/Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
12 changes: 10 additions & 2 deletions src/dashboard/Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions src/dashboard/dashboard/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@
https://docs.djangoproject.com/en/5.1/ref/settings/
"""

import os
from pathlib import Path

import environ
import sentry_sdk
from django.utils.translation import gettext_lazy as _
from sentry_sdk.integrations.django import DjangoIntegration

# is pytest running
TEST = os.environ.get("TEST", False)

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

Expand Down Expand Up @@ -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 TEST:
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,
]
8 changes: 8 additions & 0 deletions src/dashboard/dashboard/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -27,3 +28,10 @@
# internationalization
path("i18n/", include("django.conf.urls.i18n")),
]

if settings.DEBUG and not settings.TEST:
import debug_toolbar

urlpatterns += [
path("__debug__/", include(debug_toolbar.urls)),
]

0 comments on commit 21bc253

Please sign in to comment.