1
0
Fork 0
mirror of https://codeberg.org/Mo8it/AdvLabDB.git synced 2024-09-17 18:31:15 +00:00
AdvLabDB/advlabdb/config.py

83 lines
2.3 KiB
Python
Raw Normal View History

2022-08-08 16:01:33 +00:00
import sys
from configparser import ConfigParser
2022-08-18 01:20:52 +00:00
from os import environ
2022-08-08 16:01:33 +00:00
from pathlib import Path
2022-08-18 01:20:52 +00:00
def get_data_dir() -> Path:
data_dir_env_variable = "ADVLABDB_DATA_DIR"
data_dir = Path(environ.get(data_dir_env_variable, "dev/data")).resolve()
2022-08-18 01:20:52 +00:00
if not data_dir.is_dir():
sys.exit(
f"""
You did not set the environment variable {data_dir_env_variable} which is the path to the directory which holds the data of AdvLabDB including the configuration.
Read the documentation for more information!
"""
)
return data_dir
def load_config(file_name: str, data_dir: Path):
2022-08-08 16:01:33 +00:00
config = ConfigParser()
2022-08-18 01:20:52 +00:00
file = data_dir / file_name
2022-08-08 16:01:33 +00:00
2022-08-18 01:20:52 +00:00
if not file.is_file():
sys.exit(f"{file} is missing!")
2022-08-08 16:01:33 +00:00
2022-08-18 01:20:52 +00:00
config.read(file)
2022-08-08 16:01:33 +00:00
return config
2022-08-18 01:20:52 +00:00
def get_secrets(data_dir: Path):
config = load_config("secrets.ini", data_dir)
2023-11-02 18:24:14 +00:00
return config["Secrets"]
2022-08-18 01:20:52 +00:00
def get_settings(data_dir: Path):
config = load_config("settings.ini", data_dir)
2022-08-09 12:46:48 +00:00
2023-11-02 18:24:14 +00:00
return config["Settings"]
2022-08-09 12:46:48 +00:00
2022-08-18 01:20:52 +00:00
def set_config(app, data_dir: Path):
secrets = get_secrets(data_dir)
settings = get_settings(data_dir)
2022-08-08 16:01:33 +00:00
app.config["SECRET_KEY"] = secrets["SECRET_KEY"]
# SQLALCHEMY
2023-02-02 12:39:58 +00:00
db_file = data_dir / "db/advlabdb.sqlite"
2022-08-08 16:01:33 +00:00
db_file.parent.mkdir(parents=True, exist_ok=True)
app.config["SQLALCHEMY_DATABASE_URI"] = f"sqlite:///{db_file}"
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
# Flask-Admin
app.config["FLASK_ADMIN_FLUID_LAYOUT"] = True
# Flask-Security
# Enable features
app.config["SECURITY_TRACKABLE"] = True
# Explicitly disable features
app.config["SECURITY_CONFIRMABLE"] = False
app.config["SECURITY_REGISTERABLE"] = False
app.config["SECURITY_RECOVERABLE"] = False
app.config["SECURITY_PASSWORDLESS"] = False
app.config["SECURITY_CHANGEABLE"] = False
app.config["SECURITY_TWO_FACTOR"] = False
app.config["SECURITY_UNIFIED_SIGNIN"] = False
check_email_deliverability = settings.getboolean("CHECK_EMAIL_DELIVERABILITY", True)
app.config["SECURITY_EMAIL_VALIDATOR_ARGS"] = {
"check_deliverability": check_email_deliverability,
}
app.config["SECURITY_PASSWORD_SALT"] = secrets["SECURITY_PASSWORD_SALT"]
2022-08-08 20:51:52 +00:00
app.config["SECURITY_PASSWORD_LENGTH_MIN"] = settings.getint("SECURITY_PASSWORD_LENGTH_MIN", 15)
2022-09-17 13:55:09 +00:00
app.config["SECURITY_POST_LOGIN_VIEW"] = "/post-login"