Skip to content

Commit

Permalink
Merge pull request #273 from creativecommons/improve-debug
Browse files Browse the repository at this point in the history
improve handling of DEBUG and debug_toolbar
  • Loading branch information
TimidRobot authored Nov 26, 2024
2 parents aba2f59 + 8a5795d commit 1470a0e
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 30 deletions.
86 changes: 58 additions & 28 deletions caselaw/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

# Standard library
import os
import sys
from distutils.util import strtobool

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

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = bool(
strtobool(str(os.environ.get("DJANGO_DEBUG_ENABLED", default=False)))
)
# 1) "dumpdata": avoid the Django Debug Toolbar when dumping data
# 2) "loaddata": avoid the Django Debug Toolbar when loading data
# 3) "publish": avoid debug output in published files
# 4) "test": the Django Debug Toolbar can't be used with tests:
# HINT: Django changes the DEBUG setting to False when running tests. By
# default the Django Debug Toolbar is installed because DEBUG is set to
# True. For most cases, you need to avoid installing the toolbar when
# running tests. If you feel this check is in error, you can set
# DEBUG_TOOLBAR_CONFIG['IS_RUNNING_TESTS'] = False to bypass this check.
DEBUG = False
if (
"dumpdata" not in sys.argv
and "loaddata" not in sys.argv
and "publish" not in sys.argv
and "test" not in sys.argv
):
DEBUG = bool(
strtobool(str(os.environ.get("DJANGO_DEBUG_ENABLED", default=False)))
)

ALLOWED_HOSTS = ["localhost", "127.0.0.1"]

Expand All @@ -51,29 +68,40 @@
"taggit",
"widget_tweaks",
]
# Debug toolbar
if DEBUG:
INSTALLED_APPS += [
"debug_toolbar",
]
if DEBUG: # Debug toolbar
INSTALLED_APPS.append("debug_toolbar")

MIDDLEWARE = [
# SecurityMiddleware must be listed before other middleware
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
# Check if this is necessary, not in cc-legal-tools-app
"django.middleware.locale.LocaleMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"whitenoise.middleware.WhiteNoiseMiddleware",
]
# Debug toolbar
if DEBUG:
MIDDLEWARE += [
# https://django-debug-toolbar.readthedocs.io/en/latest/installation.html
# The order of MIDDLEWARE is important. You should include the Debug
# Toolbar middleware as early as possible in the list. However, it must
# come after any other middleware that encodes the response’s content,
# such as GZipMiddleware.
MIDDLEWARE = [
# SecurityMiddleware must be listed before other middleware
"django.middleware.security.SecurityMiddleware",
"debug_toolbar.middleware.DebugToolbarMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.locale.LocaleMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"whitenoise.middleware.WhiteNoiseMiddleware",
]
else:
MIDDLEWARE = [
# SecurityMiddleware must be listed before other middleware
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.locale.LocaleMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"whitenoise.middleware.WhiteNoiseMiddleware",
]

ROOT_URLCONF = "caselaw.urls"
Expand All @@ -85,14 +113,17 @@
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
"django.template.context_processors.debug",
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
],
},
},
]
if DEBUG: # Debug toolbar
TEMPLATES[0]["OPTIONS"]["context_processors"].append(
"django.template.context_processors.debug",
)

WSGI_APPLICATION = "caselaw.wsgi.application"

Expand Down Expand Up @@ -237,10 +268,9 @@
if SECURE_SSL_REDIRECT:
SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")


# See https://devcenter.heroku.com/articles/deploying-python
django_heroku.settings(locals(), logging=False)

# Debug toolbar
if DEBUG:
DEBUG_TOOLBAR_CONFIG = {"SHOW_TOOLBAR_CALLBACK": lambda request: DEBUG}

# See https://devcenter.heroku.com/articles/deploying-python
django_heroku.settings(locals(), logging=False)
11 changes: 9 additions & 2 deletions caselaw/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@
"""

# Third-party
import debug_toolbar
from django.conf import settings
from django.conf.urls.i18n import i18n_patterns
from django.contrib import admin
from django.urls import include, path, re_path
from django.views.i18n import set_language

if settings.DEBUG:
# Third-party
import debug_toolbar

# Non-translated URLs (admin and markdownx)
urlpatterns = [
re_path(r"^markdownx/", include("markdownx.urls")),
Expand All @@ -31,5 +35,8 @@
# URLs with language code prefixes
urlpatterns += i18n_patterns(
path("", include("legal_db.urls")),
path("__debug__/", include(debug_toolbar.urls)),
)
if settings.DEBUG:
urlpatterns += i18n_patterns(
path("__debug__/", include(debug_toolbar.urls)),
)

0 comments on commit 1470a0e

Please sign in to comment.