Skip to content

Commit 59d117e

Browse files
authored
Merge pull request #287 from creativecommons/python-3-12-string-to-bool
Replace distutils.util.strtobool as it no long exists in Python 3.12
2 parents 8dea7bc + ba8e483 commit 59d117e

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed

caselaw/settings.py

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,32 @@
1111
# Standard library
1212
import os
1313
import sys
14-
from distutils.util import strtobool
1514

1615
# Third-party
1716
import django_heroku
1817
from django.utils.translation import gettext_lazy as _
1918

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+
2040
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
2141
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
2242

@@ -44,8 +64,8 @@
4464
and "publish" not in sys.argv
4565
and "test" not in sys.argv
4666
):
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)
4969
)
5070

5171
ALLOWED_HOSTS = ["localhost", "127.0.0.1"]
@@ -250,21 +270,22 @@
250270

251271
COMPRESS_PRECOMPILERS = (("text/x-scss", "django_libsass.SassCompiler"),)
252272

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)
255275
)
256276

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)
259279
)
260280

261281
LIBSASS_OUTPUT_STYLE = os.environ.get("DJANGO_LIBSASS_STYLE", "compressed")
262282

263283

264284
# 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)
267287
)
288+
268289
if SECURE_SSL_REDIRECT:
269290
SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")
270291

0 commit comments

Comments
 (0)