mirror of
https://codeberg.org/Mo8it/AdvLabDB.git
synced 2025-04-16 21:03:42 +00:00
Use configparser
This commit is contained in:
parent
bc09e54e3d
commit
4749626ca0
8 changed files with 74 additions and 65 deletions
13
.gitignore
vendored
13
.gitignore
vendored
|
@ -1,5 +1,8 @@
|
|||
# Do not commit the environment variables file with the secret key and password salt!
|
||||
.env
|
||||
# Do not commit/publish the secrets file with the secret key and password salt!
|
||||
secrets.ini
|
||||
|
||||
# Own settings
|
||||
settings.ini
|
||||
|
||||
# Python
|
||||
__pycache__
|
||||
|
@ -8,14 +11,8 @@ __pycache__
|
|||
# Database
|
||||
db/
|
||||
|
||||
# Development
|
||||
.flaskenv
|
||||
|
||||
# Poetry
|
||||
.venv/
|
||||
|
||||
# Nginx
|
||||
advlabdb.conf
|
||||
|
||||
# Flask-Migrate
|
||||
migrations/
|
||||
|
|
|
@ -1,82 +1,43 @@
|
|||
from os import environ, makedirs
|
||||
|
||||
from flask import Flask
|
||||
from flask_admin import Admin
|
||||
from flask_security import Security, SQLAlchemyUserDatastore
|
||||
from flask_security.models import fsqla_v2 as fsqla
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
from flask_migrate import Migrate
|
||||
|
||||
from dotenv import load_dotenv
|
||||
|
||||
from .advlabdb_independent_funs import parse_bool
|
||||
|
||||
|
||||
def set_from_env(app, var):
|
||||
app.config[var] = environ[var]
|
||||
|
||||
|
||||
load_dotenv(".env")
|
||||
from .config import set_config
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
set_from_env(app, "SECRET_KEY")
|
||||
set_config(app)
|
||||
|
||||
|
||||
# Setup Flask-SQLAlchemy
|
||||
app.config["SQLALCHEMY_DATABASE_URI"] = f"sqlite:///../{environ['RELATIVE_DB_DIR']}/advlab.db"
|
||||
makedirs(environ["RELATIVE_DB_DIR"], exist_ok=True)
|
||||
|
||||
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
|
||||
|
||||
db = SQLAlchemy(app)
|
||||
|
||||
fsqla.FsModels.set_db_info(db)
|
||||
|
||||
# Setup Flask-Migrate
|
||||
migrate = Migrate(app, db)
|
||||
|
||||
from . import customClasses
|
||||
|
||||
# Setup Flask-Admin
|
||||
app.config["FLASK_ADMIN_FLUID_LAYOUT"] = True
|
||||
from .custom_classes import SecureAdminIndexView, SecureAssistantIndexView
|
||||
|
||||
adminSpace = Admin(
|
||||
app,
|
||||
name="Admin@AdvLabDB",
|
||||
url="/admin",
|
||||
template_mode="bootstrap3",
|
||||
index_view=customClasses.SecureAdminIndexView(name="Home", url="/admin", endpoint="admin"),
|
||||
index_view=SecureAdminIndexView(name="Home", url="/admin", endpoint="admin"),
|
||||
)
|
||||
assistantSpace = Admin(
|
||||
app,
|
||||
name="Assistant@AdvLabDB",
|
||||
url="/assistant",
|
||||
template_mode="bootstrap3",
|
||||
index_view=customClasses.SecureAssistantIndexView(name="Home", url="/assistant", endpoint="assistant"),
|
||||
index_view=SecureAssistantIndexView(name="Home", url="/assistant", endpoint="assistant"),
|
||||
)
|
||||
|
||||
from . import models
|
||||
|
||||
# Setup 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
|
||||
|
||||
app.config["SECURITY_EMAIL_VALIDATOR_ARGS"] = {
|
||||
"check_deliverability": parse_bool(environ["CHECK_EMAIL_DELIVERABILITY"])
|
||||
}
|
||||
set_from_env(app, "SECURITY_PASSWORD_SALT")
|
||||
app.config["SECURITY_PASSWORD_LENGTH_MIN"] = 15
|
||||
# TODO: app.config["SECURITY_LOGIN_USER_TEMPLATE"] =
|
||||
|
||||
user_datastore = SQLAlchemyUserDatastore(db, models.User, models.Role)
|
||||
Security(app, user_datastore)
|
||||
|
||||
|
|
|
@ -56,7 +56,7 @@ from .advlabdb_independent_funs import (
|
|||
flashRandomPassword,
|
||||
str_without_semester_formatter,
|
||||
)
|
||||
from .customClasses import SecureAdminBaseView, SecureAdminModelView
|
||||
from .custom_classes import SecureAdminBaseView, SecureAdminModelView
|
||||
from .database_import import importFromFile
|
||||
from .exceptions import ModelViewException
|
||||
from .model_dependent_funs import (
|
||||
|
|
|
@ -10,16 +10,6 @@ def flashRandomPassword(email: str, password: str):
|
|||
flash(f"New random password for email {email}: {password}", category="warning")
|
||||
|
||||
|
||||
def parse_bool(str):
|
||||
str_lower = str.lower()
|
||||
if str_lower == "false":
|
||||
return False
|
||||
elif str_lower == "true":
|
||||
return True
|
||||
else:
|
||||
raise ValueError(f'Can not parse a bool from "{str}"')
|
||||
|
||||
|
||||
def deep_getattr(object, composed_name):
|
||||
names = composed_name.split(".")
|
||||
attr = getattr(object, names[0])
|
||||
|
|
|
@ -11,7 +11,7 @@ from .advlabdb_independent_funs import (
|
|||
flashRandomPassword,
|
||||
str_formatter,
|
||||
)
|
||||
from .customClasses import SecureAssistantBaseView, SecureAssistantModelView
|
||||
from .custom_classes import SecureAssistantBaseView, SecureAssistantModelView
|
||||
from .exceptions import ModelViewException
|
||||
from .forms import assistant_group_experiment_form_factory
|
||||
from .model_dependent_funs import (
|
||||
|
|
57
advlabdb/config.py
Normal file
57
advlabdb/config.py
Normal file
|
@ -0,0 +1,57 @@
|
|||
import sys
|
||||
from configparser import ConfigParser
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def load_config(*files):
|
||||
config = ConfigParser()
|
||||
|
||||
for file in files:
|
||||
file = Path(file)
|
||||
|
||||
if not file.is_file():
|
||||
app.logger.critical(str(file) + " is missing")
|
||||
sys.exit(1)
|
||||
|
||||
config.read(file)
|
||||
|
||||
return config
|
||||
|
||||
|
||||
def set_config(app):
|
||||
config = load_config("secrets.ini", "settings.ini")
|
||||
secrets = config["Secrets"]
|
||||
settings = config["Settings"]
|
||||
|
||||
app.config["SECRET_KEY"] = secrets["SECRET_KEY"]
|
||||
|
||||
# SQLALCHEMY
|
||||
db_file = Path(settings["SQLITE_DB_PATH"])
|
||||
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"]
|
||||
app.config["SECURITY_PASSWORD_LENGTH_MIN"] = settings.getint(SECURITY_PASSWORD_LENGTH_MIN, 15)
|
||||
# TODO: app.config["SECURITY_LOGIN_USER_TEMPLATE"] =
|
4
settings_example.ini
Normal file
4
settings_example.ini
Normal file
|
@ -0,0 +1,4 @@
|
|||
[Settings]
|
||||
SQLITE_DB_PATH = "../db/advlab.db"
|
||||
CHECK_EMAIL_DELIVERABILITY = True
|
||||
SECURITY_PASSWORD_LENGTH_MIN = 15
|
Loading…
Add table
Reference in a new issue