6
0

__init__.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import json
  2. import sys
  3. import os
  4. from pathlib import Path
  5. from munch import munchify
  6. from flask import Flask
  7. from flask_migrate import Migrate
  8. from flask_sqlalchemy import SQLAlchemy
  9. from sqlalchemy import event
  10. from sqlalchemy.engine import Engine
  11. print('- Loading settings')
  12. with open('settings.json') as file:
  13. settings = munchify(json.load(file))
  14. # create projects folder
  15. if not os.path.exists(settings.projects_folder):
  16. os.mkdir(settings.projects_folder)
  17. app = Flask(__name__)
  18. if "unittest" in sys.modules:
  19. # creates an in-memory DB
  20. DB_FILE = ""
  21. else:
  22. DB_FILE = Path.cwd() / settings.database
  23. app.config["SQLALCHEMY_DATABASE_URI"] = f"sqlite:///{DB_FILE}"
  24. app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
  25. # pylint: disable=unused-argument
  26. @event.listens_for(Engine, "connect")
  27. def set_sqlite_pragma(dbapi_connection, connection_record):
  28. """ enables foreign keys on every established connection """
  29. cursor = dbapi_connection.cursor()
  30. cursor.execute("PRAGMA foreign_keys=ON")
  31. cursor.close()
  32. db = SQLAlchemy(app)
  33. migrate = Migrate(app, db)