|
10 | 10 |
|
11 | 11 | # Standard library |
12 | 12 | import os |
| 13 | +import sys |
13 | 14 | from distutils.util import strtobool |
14 | 15 |
|
15 | 16 | # Third-party |
|
27 | 28 | SECRET_KEY = os.environ.get("DJANGO_SECRET_KEY") |
28 | 29 |
|
29 | 30 | # 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 | + ) |
33 | 50 |
|
34 | 51 | ALLOWED_HOSTS = ["localhost", "127.0.0.1"] |
35 | 52 |
|
|
51 | 68 | "taggit", |
52 | 69 | "widget_tweaks", |
53 | 70 | ] |
54 | | -# Debug toolbar |
55 | | -if DEBUG: |
56 | | - INSTALLED_APPS += [ |
57 | | - "debug_toolbar", |
58 | | - ] |
| 71 | +if DEBUG: # Debug toolbar |
| 72 | + INSTALLED_APPS.append("debug_toolbar") |
59 | 73 |
|
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 |
74 | 74 | 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", |
76 | 83 | "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", |
77 | 105 | ] |
78 | 106 |
|
79 | 107 | ROOT_URLCONF = "caselaw.urls" |
|
85 | 113 | "APP_DIRS": True, |
86 | 114 | "OPTIONS": { |
87 | 115 | "context_processors": [ |
88 | | - "django.template.context_processors.debug", |
89 | 116 | "django.template.context_processors.request", |
90 | 117 | "django.contrib.auth.context_processors.auth", |
91 | 118 | "django.contrib.messages.context_processors.messages", |
92 | 119 | ], |
93 | 120 | }, |
94 | 121 | }, |
95 | 122 | ] |
| 123 | +if DEBUG: # Debug toolbar |
| 124 | + TEMPLATES[0]["OPTIONS"]["context_processors"].append( |
| 125 | + "django.template.context_processors.debug", |
| 126 | + ) |
96 | 127 |
|
97 | 128 | WSGI_APPLICATION = "caselaw.wsgi.application" |
98 | 129 |
|
|
237 | 268 | if SECURE_SSL_REDIRECT: |
238 | 269 | SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https") |
239 | 270 |
|
240 | | - |
241 | | -# See https://devcenter.heroku.com/articles/deploying-python |
242 | | -django_heroku.settings(locals(), logging=False) |
243 | | - |
244 | 271 | # Debug toolbar |
245 | 272 | if DEBUG: |
246 | 273 | 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) |
0 commit comments