1
0
Fork 0
mirror of https://codeberg.org/Mo8it/AdvLabDB.git synced 2024-09-19 18:31:16 +00:00

Use configparser

This commit is contained in:
Mo 2022-08-08 18:01:33 +02:00
parent bc09e54e3d
commit 4749626ca0
8 changed files with 74 additions and 65 deletions

13
.gitignore vendored
View file

@ -1,5 +1,8 @@
# Do not commit the environment variables file with the secret key and password salt! # Do not commit/publish the secrets file with the secret key and password salt!
.env secrets.ini
# Own settings
settings.ini
# Python # Python
__pycache__ __pycache__
@ -8,14 +11,8 @@ __pycache__
# Database # Database
db/ db/
# Development
.flaskenv
# Poetry # Poetry
.venv/ .venv/
# Nginx
advlabdb.conf
# Flask-Migrate # Flask-Migrate
migrations/ migrations/

View file

@ -1,82 +1,43 @@
from os import environ, makedirs
from flask import Flask from flask import Flask
from flask_admin import Admin from flask_admin import Admin
from flask_security import Security, SQLAlchemyUserDatastore from flask_security import Security, SQLAlchemyUserDatastore
from flask_security.models import fsqla_v2 as fsqla from flask_security.models import fsqla_v2 as fsqla
from flask_sqlalchemy import SQLAlchemy from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate from flask_migrate import Migrate
from .config import set_config
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")
app = Flask(__name__) app = Flask(__name__)
set_from_env(app, "SECRET_KEY") set_config(app)
# Setup Flask-SQLAlchemy # 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) db = SQLAlchemy(app)
fsqla.FsModels.set_db_info(db) fsqla.FsModels.set_db_info(db)
# Setup Flask-Migrate # Setup Flask-Migrate
migrate = Migrate(app, db) migrate = Migrate(app, db)
from . import customClasses
# Setup Flask-Admin # Setup Flask-Admin
app.config["FLASK_ADMIN_FLUID_LAYOUT"] = True from .custom_classes import SecureAdminIndexView, SecureAssistantIndexView
adminSpace = Admin( adminSpace = Admin(
app, app,
name="Admin@AdvLabDB", name="Admin@AdvLabDB",
url="/admin", url="/admin",
template_mode="bootstrap3", template_mode="bootstrap3",
index_view=customClasses.SecureAdminIndexView(name="Home", url="/admin", endpoint="admin"), index_view=SecureAdminIndexView(name="Home", url="/admin", endpoint="admin"),
) )
assistantSpace = Admin( assistantSpace = Admin(
app, app,
name="Assistant@AdvLabDB", name="Assistant@AdvLabDB",
url="/assistant", url="/assistant",
template_mode="bootstrap3", 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 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) user_datastore = SQLAlchemyUserDatastore(db, models.User, models.Role)
Security(app, user_datastore) Security(app, user_datastore)

View file

@ -56,7 +56,7 @@ from .advlabdb_independent_funs import (
flashRandomPassword, flashRandomPassword,
str_without_semester_formatter, str_without_semester_formatter,
) )
from .customClasses import SecureAdminBaseView, SecureAdminModelView from .custom_classes import SecureAdminBaseView, SecureAdminModelView
from .database_import import importFromFile from .database_import import importFromFile
from .exceptions import ModelViewException from .exceptions import ModelViewException
from .model_dependent_funs import ( from .model_dependent_funs import (

View file

@ -10,16 +10,6 @@ def flashRandomPassword(email: str, password: str):
flash(f"New random password for email {email}: {password}", category="warning") 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): def deep_getattr(object, composed_name):
names = composed_name.split(".") names = composed_name.split(".")
attr = getattr(object, names[0]) attr = getattr(object, names[0])

View file

@ -11,7 +11,7 @@ from .advlabdb_independent_funs import (
flashRandomPassword, flashRandomPassword,
str_formatter, str_formatter,
) )
from .customClasses import SecureAssistantBaseView, SecureAssistantModelView from .custom_classes import SecureAssistantBaseView, SecureAssistantModelView
from .exceptions import ModelViewException from .exceptions import ModelViewException
from .forms import assistant_group_experiment_form_factory from .forms import assistant_group_experiment_form_factory
from .model_dependent_funs import ( from .model_dependent_funs import (

57
advlabdb/config.py Normal file
View 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
View file

@ -0,0 +1,4 @@
[Settings]
SQLITE_DB_PATH = "../db/advlab.db"
CHECK_EMAIL_DELIVERABILITY = True
SECURITY_PASSWORD_LENGTH_MIN = 15