-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
71 lines (55 loc) · 2.22 KB
/
config.py
File metadata and controls
71 lines (55 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import os
from pathlib import Path
class BaseConfig:
SECRET_KEY = os.getenv("SECRET_KEY", "change-me")
DB_PATH = os.getenv("DB_PATH") # None until CSV import creates a database
ADMIN_PASSWORD = os.getenv("ADMIN_PASSWORD", "admin123")
STATION_DEFAULT_PIN = os.getenv("STATION_DEFAULT_PIN")
STATION_DEFAULT_NAME = os.getenv("STATION_DEFAULT_NAME", "Station")
STATION_DEFAULT_MAX_LOGINS = int(os.getenv("STATION_DEFAULT_MAX_LOGINS", "1"))
STATION_DEFAULT_PIN_LENGTH = int(os.getenv("STATION_DEFAULT_PIN_LENGTH", "6"))
WTF_CSRF_ENABLED = True
SESSION_COOKIE_SAMESITE = os.getenv("SESSION_COOKIE_SAMESITE", "Lax")
SESSION_COOKIE_HTTPONLY = True
SESSION_COOKIE_SECURE = False # override in production
PREFERRED_URL_SCHEME = os.getenv("PREFERRED_URL_SCHEME", "http")
# Flask
DEBUG = False
TESTING = False
@classmethod
def ensure_paths(cls) -> None:
"""Ensure directories for DB_PATH exist."""
if cls.DB_PATH:
Path(cls.DB_PATH).parent.mkdir(parents=True, exist_ok=True)
class DevelopmentConfig(BaseConfig):
DEBUG = True
SESSION_COOKIE_SECURE = False
class ProductionConfig(BaseConfig):
DEBUG = False
SESSION_COOKIE_SECURE = os.getenv("SESSION_COOKIE_SECURE", "true").lower() == "true"
class TestingConfig(BaseConfig):
TESTING = True
DEBUG = True
WTF_CSRF_ENABLED = False
SESSION_COOKIE_SECURE = False
DB_PATH = os.getenv("TEST_DB_PATH", "alles_neu/app/database/test.db")
CONFIG_MAP: dict[str, type[BaseConfig]] = {
"development": DevelopmentConfig,
"dev": DevelopmentConfig,
"production": ProductionConfig,
"prod": ProductionConfig,
"testing": TestingConfig,
"test": TestingConfig,
}
def get_config(env_name: str | None = None) -> type[BaseConfig]:
"""
Returns a config class based on the environment name.
Priority:
1) Explicit env_name argument
2) ENV or FLASK_ENV environment variable
3) Default to ProductionConfig
"""
env = (env_name or os.getenv("ENV") or os.getenv("FLASK_ENV") or "").lower()
config_cls = CONFIG_MAP.get(env, ProductionConfig)
config_cls.ensure_paths()
return config_cls