Skip to content

Commit 1470a0e

Browse files
authored
Merge pull request #273 from creativecommons/improve-debug
improve handling of DEBUG and debug_toolbar
2 parents aba2f59 + 8a5795d commit 1470a0e

File tree

2 files changed

+67
-30
lines changed

2 files changed

+67
-30
lines changed

caselaw/settings.py

Lines changed: 58 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
# Standard library
1212
import os
13+
import sys
1314
from distutils.util import strtobool
1415

1516
# Third-party
@@ -27,9 +28,25 @@
2728
SECRET_KEY = os.environ.get("DJANGO_SECRET_KEY")
2829

2930
# SECURITY WARNING: don't run with debug turned on in production!
30-
DEBUG = bool(
31-
strtobool(str(os.environ.get("DJANGO_DEBUG_ENABLED", default=False)))
32-
)
31+
# 1) "dumpdata": avoid the Django Debug Toolbar when dumping data
32+
# 2) "loaddata": avoid the Django Debug Toolbar when loading data
33+
# 3) "publish": avoid debug output in published files
34+
# 4) "test": the Django Debug Toolbar can't be used with tests:
35+
# HINT: Django changes the DEBUG setting to False when running tests. By
36+
# default the Django Debug Toolbar is installed because DEBUG is set to
37+
# True. For most cases, you need to avoid installing the toolbar when
38+
# running tests. If you feel this check is in error, you can set
39+
# DEBUG_TOOLBAR_CONFIG['IS_RUNNING_TESTS'] = False to bypass this check.
40+
DEBUG = False
41+
if (
42+
"dumpdata" not in sys.argv
43+
and "loaddata" not in sys.argv
44+
and "publish" not in sys.argv
45+
and "test" not in sys.argv
46+
):
47+
DEBUG = bool(
48+
strtobool(str(os.environ.get("DJANGO_DEBUG_ENABLED", default=False)))
49+
)
3350

3451
ALLOWED_HOSTS = ["localhost", "127.0.0.1"]
3552

@@ -51,29 +68,40 @@
5168
"taggit",
5269
"widget_tweaks",
5370
]
54-
# Debug toolbar
55-
if DEBUG:
56-
INSTALLED_APPS += [
57-
"debug_toolbar",
58-
]
71+
if DEBUG: # Debug toolbar
72+
INSTALLED_APPS.append("debug_toolbar")
5973

60-
MIDDLEWARE = [
61-
# SecurityMiddleware must be listed before other middleware
62-
"django.middleware.security.SecurityMiddleware",
63-
"django.contrib.sessions.middleware.SessionMiddleware",
64-
# Check if this is necessary, not in cc-legal-tools-app
65-
"django.middleware.locale.LocaleMiddleware",
66-
"django.middleware.common.CommonMiddleware",
67-
"django.middleware.csrf.CsrfViewMiddleware",
68-
"django.contrib.auth.middleware.AuthenticationMiddleware",
69-
"django.contrib.messages.middleware.MessageMiddleware",
70-
"django.middleware.clickjacking.XFrameOptionsMiddleware",
71-
"whitenoise.middleware.WhiteNoiseMiddleware",
72-
]
73-
# Debug toolbar
7474
if DEBUG:
75-
MIDDLEWARE += [
75+
# https://django-debug-toolbar.readthedocs.io/en/latest/installation.html
76+
# The order of MIDDLEWARE is important. You should include the Debug
77+
# Toolbar middleware as early as possible in the list. However, it must
78+
# come after any other middleware that encodes the response’s content,
79+
# such as GZipMiddleware.
80+
MIDDLEWARE = [
81+
# SecurityMiddleware must be listed before other middleware
82+
"django.middleware.security.SecurityMiddleware",
7683
"debug_toolbar.middleware.DebugToolbarMiddleware",
84+
"django.contrib.sessions.middleware.SessionMiddleware",
85+
"django.middleware.locale.LocaleMiddleware",
86+
"django.middleware.common.CommonMiddleware",
87+
"django.middleware.csrf.CsrfViewMiddleware",
88+
"django.contrib.auth.middleware.AuthenticationMiddleware",
89+
"django.contrib.messages.middleware.MessageMiddleware",
90+
"django.middleware.clickjacking.XFrameOptionsMiddleware",
91+
"whitenoise.middleware.WhiteNoiseMiddleware",
92+
]
93+
else:
94+
MIDDLEWARE = [
95+
# SecurityMiddleware must be listed before other middleware
96+
"django.middleware.security.SecurityMiddleware",
97+
"django.contrib.sessions.middleware.SessionMiddleware",
98+
"django.middleware.locale.LocaleMiddleware",
99+
"django.middleware.common.CommonMiddleware",
100+
"django.middleware.csrf.CsrfViewMiddleware",
101+
"django.contrib.auth.middleware.AuthenticationMiddleware",
102+
"django.contrib.messages.middleware.MessageMiddleware",
103+
"django.middleware.clickjacking.XFrameOptionsMiddleware",
104+
"whitenoise.middleware.WhiteNoiseMiddleware",
77105
]
78106

79107
ROOT_URLCONF = "caselaw.urls"
@@ -85,14 +113,17 @@
85113
"APP_DIRS": True,
86114
"OPTIONS": {
87115
"context_processors": [
88-
"django.template.context_processors.debug",
89116
"django.template.context_processors.request",
90117
"django.contrib.auth.context_processors.auth",
91118
"django.contrib.messages.context_processors.messages",
92119
],
93120
},
94121
},
95122
]
123+
if DEBUG: # Debug toolbar
124+
TEMPLATES[0]["OPTIONS"]["context_processors"].append(
125+
"django.template.context_processors.debug",
126+
)
96127

97128
WSGI_APPLICATION = "caselaw.wsgi.application"
98129

@@ -237,10 +268,9 @@
237268
if SECURE_SSL_REDIRECT:
238269
SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")
239270

240-
241-
# See https://devcenter.heroku.com/articles/deploying-python
242-
django_heroku.settings(locals(), logging=False)
243-
244271
# Debug toolbar
245272
if DEBUG:
246273
DEBUG_TOOLBAR_CONFIG = {"SHOW_TOOLBAR_CALLBACK": lambda request: DEBUG}
274+
275+
# See https://devcenter.heroku.com/articles/deploying-python
276+
django_heroku.settings(locals(), logging=False)

caselaw/urls.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,16 @@
1515
"""
1616

1717
# Third-party
18-
import debug_toolbar
18+
from django.conf import settings
1919
from django.conf.urls.i18n import i18n_patterns
2020
from django.contrib import admin
2121
from django.urls import include, path, re_path
2222
from django.views.i18n import set_language
2323

24+
if settings.DEBUG:
25+
# Third-party
26+
import debug_toolbar
27+
2428
# Non-translated URLs (admin and markdownx)
2529
urlpatterns = [
2630
re_path(r"^markdownx/", include("markdownx.urls")),
@@ -31,5 +35,8 @@
3135
# URLs with language code prefixes
3236
urlpatterns += i18n_patterns(
3337
path("", include("legal_db.urls")),
34-
path("__debug__/", include(debug_toolbar.urls)),
3538
)
39+
if settings.DEBUG:
40+
urlpatterns += i18n_patterns(
41+
path("__debug__/", include(debug_toolbar.urls)),
42+
)

0 commit comments

Comments
 (0)