From 95102f0acd26e83807608a4349df9567d38fe68b Mon Sep 17 00:00:00 2001 From: Dabble Date: Tue, 29 Mar 2022 11:30:45 +0200 Subject: [PATCH] Added django-debug-toolbar This will only be active if installed - either manually or through `requirements_dev.txt`. --- docs/urls.py | 2 ++ internal/urls.py | 2 ++ requirements_dev.txt | 1 + util/url_utils.py | 10 ++++++++++ web/admin_urls.py | 4 ++++ web/settings.py | 31 +++++++++++++++++++++++++++++++ web/urls.py | 2 ++ 7 files changed, 52 insertions(+) create mode 100644 requirements_dev.txt diff --git a/docs/urls.py b/docs/urls.py index 317b84761..c88f1dad4 100644 --- a/docs/urls.py +++ b/docs/urls.py @@ -4,6 +4,7 @@ from django.urls import path, register_converter from django.views.generic import TemplateView +from util.url_utils import debug_toolbar_urls from . import converters, views @@ -13,6 +14,7 @@ path("robots.txt", TemplateView.as_view(template_name='docs/robots.txt', content_type='text/plain')), path(".well-known/security.txt", TemplateView.as_view(template_name='web/security.txt', content_type='text/plain')), + *debug_toolbar_urls(), path("i18n/", decorator_include( permission_required('docs.view_page'), 'django.conf.urls.i18n' diff --git a/internal/urls.py b/internal/urls.py index 71aaf5a16..1515d75fe 100644 --- a/internal/urls.py +++ b/internal/urls.py @@ -4,6 +4,7 @@ from django.urls import include, path from django.views.generic import TemplateView +from util.url_utils import debug_toolbar_urls from . import views @@ -11,6 +12,7 @@ path("robots.txt", TemplateView.as_view(template_name='internal/robots.txt', content_type='text/plain')), path(".well-known/security.txt", TemplateView.as_view(template_name='web/security.txt', content_type='text/plain')), + *debug_toolbar_urls(), path("i18n/", decorator_include( permission_required('internal.is_internal'), 'django.conf.urls.i18n' diff --git a/requirements_dev.txt b/requirements_dev.txt new file mode 100644 index 000000000..aec394f4b --- /dev/null +++ b/requirements_dev.txt @@ -0,0 +1 @@ +django-debug-toolbar diff --git a/util/url_utils.py b/util/url_utils.py index e69de29bb..a4e6ca0de 100644 --- a/util/url_utils.py +++ b/util/url_utils.py @@ -0,0 +1,10 @@ +from django.conf import settings +from django.urls import include, path + + +def debug_toolbar_urls(): + if not settings.USE_DEBUG_TOOLBAR: + return [] + return [ + path("__debug__/", include('debug_toolbar.urls')), + ] diff --git a/web/admin_urls.py b/web/admin_urls.py index faaa9dab7..256cda202 100644 --- a/web/admin_urls.py +++ b/web/admin_urls.py @@ -8,6 +8,8 @@ from django.views.generic import RedirectView, TemplateView from django_hosts import reverse +from util.url_utils import debug_toolbar_urls + # Updates the "View site" link to this url admin.site.site_url = f"//{settings.PARENT_HOST}/" @@ -15,6 +17,8 @@ urlpatterns = [ path("robots.txt", TemplateView.as_view(template_name='admin/robots.txt', content_type='text/plain')), path(".well-known/security.txt", TemplateView.as_view(template_name='web/security.txt', content_type='text/plain')), + + *debug_toolbar_urls(), path("i18n/", decorator_include( staff_member_required, 'django.conf.urls.i18n' diff --git a/web/settings.py b/web/settings.py index 64a1eb9f6..a33b9f4a6 100644 --- a/web/settings.py +++ b/web/settings.py @@ -1,6 +1,7 @@ import copy import logging import sys +from importlib.util import find_spec from pathlib import Path import django.views.static @@ -32,6 +33,7 @@ SECRET_KEY = ' ' DEBUG = True ALLOWED_HOSTS = ['*'] +INTERNAL_IPS = ['127.0.0.1'] MEDIA_ROOT = BASE_DIR.parent / 'media' MEDIA_URL = '/media/' SOCIAL_AUTH_DATAPORTEN_KEY = '' @@ -56,6 +58,9 @@ # be changed in production PARENT_HOST = "makentnu.localhost:8000" +# Is `True` if `django-debug-toolbar` is installed +USE_DEBUG_TOOLBAR = find_spec('debug_toolbar') is not None # (custom setting) + EVENT_TICKET_EMAIL = "ticket@makentnu.no" # (custom setting) EMAIL_SITE_URL = "https://makentnu.no" # (custom setting) @@ -111,6 +116,8 @@ # See this page for a list of all management commands: https://django-extensions.readthedocs.io/en/latest/command_extensions.html 'django_extensions', + *(['debug_toolbar'] if USE_DEBUG_TOOLBAR else []), + # Should be placed last, # "to ensure that exceptions inside other apps' signal handlers do not affect the integrity of file deletions within transactions" 'django_cleanup.apps.CleanupConfig', @@ -121,6 +128,8 @@ # Must be the first entry (see https://django-hosts.readthedocs.io/en/latest/#installation) 'django_hosts.middleware.HostsRequestMiddleware', + *(['debug_toolbar.middleware.DebugToolbarMiddleware'] if USE_DEBUG_TOOLBAR else []), + # (See hints for ordering at https://docs.djangoproject.com/en/stable/ref/middleware/#middleware-ordering) 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', @@ -355,6 +364,28 @@ def static_lazy(path): SIMPLE_HISTORY_FILEFIELD_TO_CHARFIELD = True +if USE_DEBUG_TOOLBAR: + DEBUG_TOOLBAR_CONFIG = { + 'RENDER_PANELS': False, + 'DISABLE_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.cache.CachePanel', + 'debug_toolbar.panels.signals.SignalsPanel', + # 'debug_toolbar.panels.logging.LoggingPanel', + 'debug_toolbar.panels.redirects.RedirectsPanel', + 'debug_toolbar.panels.profiling.ProfilingPanel', + }, + } + + # See https://docs.djangoproject.com/en/stable/topics/logging/ for # more details on how to customize your logging configuration. LOGGING = { diff --git a/web/urls.py b/web/urls.py index 93930f826..519d45458 100644 --- a/web/urls.py +++ b/web/urls.py @@ -15,6 +15,7 @@ from contentbox.views import DisplayContentBoxView, EditContentBoxView from dataporten.views import Logout, login_wrapper from news import urls as news_urls +from util.url_utils import debug_toolbar_urls from . import views @@ -24,6 +25,7 @@ path("robots.txt", TemplateView.as_view(template_name='web/robots.txt', content_type='text/plain')), path(".well-known/security.txt", TemplateView.as_view(template_name='web/security.txt', content_type='text/plain')), + *debug_toolbar_urls(), path("i18n/", include('django.conf.urls.i18n')), ]