lazyflat/apply/settings.py
EiSiMo 9fbe1ce728 apply: define LANGUAGE setting (fixes 500 on first handled ApplicationResult)
apply/language.py references LANGUAGE via \`from settings import *\`, but
the constant was never added to apply/settings.py. Python only errored at
the first runtime use — which is \`str(ApplicationResult)\` in the /apply
handler. Any outcome that didn't short-circuit before the final \`return
ApplyResponse(message=str(result), …)\` blew up with NameError → 500.

Add LANGUAGE = getenv("LANGUAGE", "de") so existing translations.toml
keys resolve. Reproduced live inside the apply container: NameError:
name 'LANGUAGE' is not defined at language.py:15.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-21 17:45:42 +02:00

60 lines
2 KiB
Python

"""
apply service config.
Personal info and WBS fields used to live here as env vars; they now come in
with every /apply request as a Profile. The only remaining settings are
browser + service-level knobs.
"""
import logging
import sys
from os import getenv
from dotenv import load_dotenv
logger = logging.getLogger("flat-apply")
load_dotenv()
def get_env_or_fail(key: str, default: str | None = None, required: bool = True) -> str:
value = getenv(key, default)
if required and value is None:
logger.error(f"Missing required environment variable: {key}")
sys.exit(1)
return value
def get_bool_env(key: str, default: str = "False") -> bool:
return (getenv(key, default) or "").lower() in ("true", "1", "yes", "on")
def get_int_env(key: str, default: str) -> int:
v = getenv(key, default) or default
try:
return int(v)
except ValueError:
logger.error(f"Env var {key} must be int, got {v!r}")
sys.exit(1)
# --- Browser -----------------------------------------------------------------
HEADLESS: bool = get_bool_env("HEADLESS", "True")
BROWSER_WIDTH: int = get_int_env("BROWSER_WIDTH", "600")
BROWSER_HEIGHT: int = get_int_env("BROWSER_HEIGHT", "800")
BROWSER_LOCALE: str = getenv("BROWSER_LOCALE", "de-DE")
POST_SUBMISSION_SLEEP_MS: int = get_int_env("POST_SUBMISSION_SLEEP_MS", "0")
# --- Service -----------------------------------------------------------------
INTERNAL_API_KEY: str = get_env_or_fail("INTERNAL_API_KEY")
# --- Translations ------------------------------------------------------------
# Key into translations.toml (e.g. "de", "en"). Used by language.get_text.
LANGUAGE: str = getenv("LANGUAGE", "de")
def log_settings() -> None:
logger.debug("--- Settings ---")
logger.debug(f"HEADLESS: {HEADLESS}")
logger.debug(f"BROWSER_WIDTH: {BROWSER_WIDTH}")
logger.debug(f"BROWSER_HEIGHT: {BROWSER_HEIGHT}")
logger.debug(f"BROWSER_LOCALE: {BROWSER_LOCALE}")
logger.debug(f"POST_SUBMISSION_SLEEP_MS: {POST_SUBMISSION_SLEEP_MS}")