Skip to content

Commit

Permalink
Added django-debug-toolbar
Browse files Browse the repository at this point in the history
This will only be active if installed - either manually or through `requirements_dev.txt`.
  • Loading branch information
ddabble committed Mar 29, 2022
1 parent 6644401 commit 95102f0
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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'
Expand Down
2 changes: 2 additions & 0 deletions internal/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
from django.urls import include, path
from django.views.generic import TemplateView

from util.url_utils import debug_toolbar_urls
from . import views


urlpatterns = [
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'
Expand Down
1 change: 1 addition & 0 deletions requirements_dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
django-debug-toolbar
10 changes: 10 additions & 0 deletions util/url_utils.py
Original file line number Diff line number Diff line change
@@ -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')),
]
4 changes: 4 additions & 0 deletions web/admin_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@
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}/"

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'
Expand Down
31 changes: 31 additions & 0 deletions web/settings.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import copy
import logging
import sys
from importlib.util import find_spec
from pathlib import Path

import django.views.static
Expand Down Expand Up @@ -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 = ''
Expand All @@ -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 = "[email protected]" # (custom setting)
EMAIL_SITE_URL = "https://makentnu.no" # (custom setting)

Expand Down Expand Up @@ -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',
Expand All @@ -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',
Expand Down Expand Up @@ -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 = {
Expand Down
2 changes: 2 additions & 0 deletions web/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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')),
]

Expand Down

0 comments on commit 95102f0

Please sign in to comment.