|
11 | 11 | # Standard library |
12 | 12 | import os |
13 | 13 | import sys |
14 | | -from distutils.util import strtobool |
15 | 14 |
|
16 | 15 | # Third-party |
17 | 16 | import django_heroku |
18 | 17 | from django.utils.translation import gettext_lazy as _ |
19 | 18 |
|
| 19 | + |
| 20 | +# Based on Python 3.11.2 distutils.util.strtobool |
| 21 | +def string_to_bool(string_value): |
| 22 | + """Convert a string to boolean. |
| 23 | +
|
| 24 | + True values are 'y', 'yes', 't', 'true', 'on', and '1' |
| 25 | + False values are 'n', 'no', 'f', 'false', 'off', and '0'. |
| 26 | +
|
| 27 | + Raises ValueError if 'string_value' is anything else. |
| 28 | + """ |
| 29 | + string_value = str(string_value).lower() |
| 30 | + if string_value in ("y", "yes", "t", "true", "on", "1"): |
| 31 | + return True |
| 32 | + elif string_value in ("n", "no", "f", "false", "off", "0"): |
| 33 | + return False |
| 34 | + else: |
| 35 | + raise ValueError( |
| 36 | + f"invalid string representation of boolean value: '{string_value}'" |
| 37 | + ) |
| 38 | + |
| 39 | + |
20 | 40 | # Build paths inside the project like this: os.path.join(BASE_DIR, ...) |
21 | 41 | BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
22 | 42 |
|
|
44 | 64 | and "publish" not in sys.argv |
45 | 65 | and "test" not in sys.argv |
46 | 66 | ): |
47 | | - DEBUG = bool( |
48 | | - strtobool(str(os.environ.get("DJANGO_DEBUG_ENABLED", default=False))) |
| 67 | + DEBUG = string_to_bool( |
| 68 | + os.environ.get("DJANGO_DEBUG_ENABLED", default=False) |
49 | 69 | ) |
50 | 70 |
|
51 | 71 | ALLOWED_HOSTS = ["localhost", "127.0.0.1"] |
|
250 | 270 |
|
251 | 271 | COMPRESS_PRECOMPILERS = (("text/x-scss", "django_libsass.SassCompiler"),) |
252 | 272 |
|
253 | | -COMPRESS_ENABLED = bool( |
254 | | - strtobool(str(os.environ.get("DJANGO_COMPRESS_ENABLED", True))) |
| 273 | +COMPRESS_ENABLED = string_to_bool( |
| 274 | + os.environ.get("DJANGO_COMPRESS_ENABLED", True) |
255 | 275 | ) |
256 | 276 |
|
257 | | -COMPRESS_OFFLINE = bool( |
258 | | - strtobool(str(os.environ.get("DJANGO_COMPRESS_OFFLINE", True))) |
| 277 | +COMPRESS_OFFLINE = string_to_bool( |
| 278 | + os.environ.get("DJANGO_COMPRESS_OFFLINE", True) |
259 | 279 | ) |
260 | 280 |
|
261 | 281 | LIBSASS_OUTPUT_STYLE = os.environ.get("DJANGO_LIBSASS_STYLE", "compressed") |
262 | 282 |
|
263 | 283 |
|
264 | 284 | # TLS/SSL (HTTPS) |
265 | | -SECURE_SSL_REDIRECT = bool( |
266 | | - strtobool(str(os.environ.get("DJANGO_SECURE_SSL_REDIRECT", True))) |
| 285 | +SECURE_SSL_REDIRECT = string_to_bool( |
| 286 | + os.environ.get("DJANGO_SECURE_SSL_REDIRECT", True) |
267 | 287 | ) |
| 288 | + |
268 | 289 | if SECURE_SSL_REDIRECT: |
269 | 290 | SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https") |
270 | 291 |
|
|
0 commit comments