2022-05-07 14:48:28 +00:00
|
|
|
from os import environ, makedirs
|
2022-04-18 16:04:48 +00:00
|
|
|
|
2021-03-18 13:53:55 +00:00
|
|
|
from flask import Flask
|
2021-06-02 21:43:41 +00:00
|
|
|
from flask_admin import Admin
|
2021-04-03 00:11:26 +00:00
|
|
|
from flask_security import Security, SQLAlchemyUserDatastore
|
|
|
|
from flask_security.models import fsqla_v2 as fsqla
|
2021-06-02 21:43:41 +00:00
|
|
|
from flask_sqlalchemy import SQLAlchemy
|
2022-05-29 16:13:28 +00:00
|
|
|
from flask_migrate import Migrate
|
2021-03-18 13:53:55 +00:00
|
|
|
|
2022-04-18 16:04:48 +00:00
|
|
|
from dotenv import load_dotenv
|
|
|
|
|
2022-05-16 20:20:36 +00:00
|
|
|
from .advlabdb_independent_funs import parse_bool
|
2022-04-18 16:04:48 +00:00
|
|
|
|
2021-06-28 16:43:13 +00:00
|
|
|
|
2022-04-18 16:04:48 +00:00
|
|
|
def set_from_env(app, var):
|
|
|
|
app.config[var] = environ[var]
|
|
|
|
|
|
|
|
|
|
|
|
load_dotenv(".env")
|
|
|
|
|
|
|
|
app = Flask(__name__)
|
2021-04-03 00:11:26 +00:00
|
|
|
|
2022-04-18 16:04:48 +00:00
|
|
|
set_from_env(app, "SECRET_KEY")
|
2021-04-03 00:11:26 +00:00
|
|
|
|
2022-05-29 17:34:20 +00:00
|
|
|
# Setup Flask-SQLAlchemy
|
2022-05-07 14:48:28 +00:00
|
|
|
app.config["SQLALCHEMY_DATABASE_URI"] = f"sqlite:///../{environ['RELATIVE_DB_DIR']}/advlab.db"
|
|
|
|
makedirs(environ["RELATIVE_DB_DIR"], exist_ok=True)
|
|
|
|
|
2021-07-13 23:58:35 +00:00
|
|
|
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
|
|
|
|
|
2021-03-18 13:53:55 +00:00
|
|
|
db = SQLAlchemy(app)
|
|
|
|
|
2021-04-03 00:11:26 +00:00
|
|
|
fsqla.FsModels.set_db_info(db)
|
|
|
|
|
2022-05-29 17:34:20 +00:00
|
|
|
# Setup Flask-Migrate
|
2022-05-29 16:13:28 +00:00
|
|
|
migrate = Migrate(app, db)
|
|
|
|
|
2022-05-08 19:26:25 +00:00
|
|
|
from . import customClasses
|
2021-06-02 21:43:41 +00:00
|
|
|
|
2022-05-29 17:34:20 +00:00
|
|
|
# Setup Flask-Admin
|
|
|
|
app.config["FLASK_ADMIN_FLUID_LAYOUT"] = True
|
|
|
|
|
2021-07-30 00:03:44 +00:00
|
|
|
adminSpace = Admin(
|
2021-06-02 21:43:41 +00:00
|
|
|
app,
|
|
|
|
name="Admin@AdvLabDB",
|
2021-07-30 00:03:44 +00:00
|
|
|
url="/admin",
|
2021-06-02 21:43:41 +00:00
|
|
|
template_mode="bootstrap3",
|
2022-06-28 14:57:55 +00:00
|
|
|
index_view=customClasses.SecureAdminIndexView(name="Home", url="/admin", endpoint="admin"),
|
2021-07-30 00:03:44 +00:00
|
|
|
)
|
|
|
|
assistantSpace = Admin(
|
|
|
|
app,
|
|
|
|
name="Assistant@AdvLabDB",
|
|
|
|
url="/assistant",
|
|
|
|
template_mode="bootstrap3",
|
2022-06-28 14:57:55 +00:00
|
|
|
index_view=customClasses.SecureAssistantIndexView(name="Home", url="/assistant", endpoint="assistant"),
|
2021-06-02 21:43:41 +00:00
|
|
|
)
|
2021-04-18 23:33:46 +00:00
|
|
|
|
2022-05-08 19:26:25 +00:00
|
|
|
from . import models
|
2021-04-03 00:11:26 +00:00
|
|
|
|
|
|
|
# Setup Flask-Security
|
2022-05-29 17:19:59 +00:00
|
|
|
# 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
|
2022-05-29 17:03:54 +00:00
|
|
|
|
2022-05-29 17:34:20 +00:00
|
|
|
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)
|
|
|
|
|
2021-07-14 02:26:18 +00:00
|
|
|
try:
|
2022-05-08 19:26:25 +00:00
|
|
|
from . import routes, adminModelViews, assistantModelViews
|
2021-07-14 02:26:18 +00:00
|
|
|
except Exception as ex:
|
|
|
|
print(
|
|
|
|
"\nYou are probably initializing the database with a script. If not, then you have to worry about not being able to import in __init__.py!\n"
|
|
|
|
)
|
2021-07-29 18:03:41 +00:00
|
|
|
print(str(ex))
|