12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- import json
- import sys
- import os
- from pathlib import Path
- from munch import munchify
- from flask import Flask
- from flask_migrate import Migrate
- from flask_sqlalchemy import SQLAlchemy
- from sqlalchemy import event
- from sqlalchemy.engine import Engine
- print('- Loading settings')
- with open('settings.json') as file:
- settings = munchify(json.load(file))
- # create projects folder
- if not os.path.exists(settings.projects_folder):
- os.mkdir(settings.projects_folder)
- app = Flask(__name__)
- if "unittest" in sys.modules:
- # creates an in-memory DB
- DB_FILE = ""
- else:
- DB_FILE = Path.cwd() / settings.database
- app.config["SQLALCHEMY_DATABASE_URI"] = f"sqlite:///{DB_FILE}"
- app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
- # pylint: disable=unused-argument
- @event.listens_for(Engine, "connect")
- def set_sqlite_pragma(dbapi_connection, connection_record):
- """ enables foreign keys on every established connection """
- cursor = dbapi_connection.cursor()
- cursor.execute("PRAGMA foreign_keys=ON")
- cursor.close()
- db = SQLAlchemy(app)
- migrate = Migrate(app, db)
|