__init__.py 992 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. @event.listens_for(Engine, "connect")
  26. def set_sqlite_pragma(dbapi_connection, connection_record):
  27. cursor = dbapi_connection.cursor()
  28. cursor.execute("PRAGMA foreign_keys=ON")
  29. cursor.close()
  30. db = SQLAlchemy(app)
  31. migrate = Migrate(app, db)