Skip to content

Commit

Permalink
backend: simplify bool setting readers
Browse files Browse the repository at this point in the history
  • Loading branch information
rr- committed Jul 18, 2023
1 parent cb6f39a commit 076bfa8
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions backend/trcustoms/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,30 @@
from pathlib import Path


def get_setting(name: str) -> str:
def get_setting(name: str, allow_null: bool = False) -> str:
"""Return a setting from the environment variables.
Raises an error if the setting is not definied.
"""
ret = os.environ.get(name)
if ret is None:
if allow_null:
return ""
raise RuntimeError(f"Missing configuration variable {name}")
return ret


def get_bool_setting(name: str, **kwargs) -> bool:
return get_setting(name, **kwargs).lower() in {"true", "yes", "y", "1"}


BASE_DIR = Path(__file__).resolve().parent.parent
CACHE_DIR = BASE_DIR / "cache"

SECRET_KEY = get_setting("SECRET_KEY")
DEBUG = get_setting("DEBUG").lower() in {"1", "true"}
DEBUG_SQL = get_setting("DEBUG_SQL").lower() in {"1", "true"}
TESTING = os.environ.get("TESTING", "").lower() in {"1", "true"} or any(
DEBUG = get_bool_setting("DEBUG")
DEBUG_SQL = get_bool_setting("DEBUG_SQL")
TESTING = get_bool_setting("TESTING", allow_null=True) or any(
arg.endswith("test") for arg in sys.argv
)
ALLOWED_HOSTS = ["*"]
Expand Down Expand Up @@ -262,12 +268,7 @@ def get_setting(name: str) -> str:
AWS_S3_CUSTOM_DOMAIN = get_setting("AWS_S3_CUSTOM_DOMAIN")
AWS_S3_OBJECT_PARAMETERS = {"CacheControl": "max-age=86400"}
AWS_MEDIA_LOCATION = "media"
USE_AWS_STORAGE = get_setting("USE_AWS_STORAGE").lower() in {
"1",
"true",
"y",
"yes",
}
USE_AWS_STORAGE = get_bool_setting("USE_AWS_STORAGE")

CSRF_TRUSTED_ORIGINS = [
"http://localhost:8000",
Expand Down

0 comments on commit 076bfa8

Please sign in to comment.