def init_database(): db.create_all() db.session.commit() yield db db.session.remove() db.drop_all()
def create_app(): logger.info("Creating Flask init_app") app = Flask(__name__) app.config.from_pyfile('config.py') CORS(app) with app.app_context(): db.init_app(app) flask_admin = Admin(app) with app.test_request_context(): from database.models import Users, Router, Sensor, Warnings flask_admin.add_view(ModelView(Users, db.session)) flask_admin.add_view(ModelView(Router, db.session)) flask_admin.add_view(ModelView(Sensor, db.session)) flask_admin.add_view(ModelView(Warnings, db.session)) register_blueprints(app) db.create_all() from MQTT.mqtt_client import client from handlers.database_handler import DatabaseHandler channels = DatabaseHandler().get_router_channels() for ch in channels[:]: client.subscribe(ch) print("MQTT: Subscribing to: " + str(ch)) client.loop_start() return app
def reset_db(): logger.info('Request to delete the database!') db.drop_all() db.create_all() return 'ok', 200
from flask_sqlalchemy import SQLAlchemy from flask_apscheduler import APScheduler from config import Config from front_end import front_end from model_helper import model_helper from initialize import initialize from database.database import db from test_cases import test_cases app = Flask(__name__) app.config.from_object(Config()) app.config['MYSQL_DATABASE_CHARSET'] = 'utf8mb4' scheduler = APScheduler() app.register_blueprint(front_end) app.register_blueprint(test_cases) app.register_blueprint(model_helper) if __name__ == "__main__": db.app = app db.init_app(app) db.create_all() # initialize() app.run(debug=True) else: db.app = app db.init_app(app) db.create_all() # initialize()
def setup_database(app): # Creating all the tables in database with app.app_context(): db.create_all()
def register_views(self): for x in self.views: self.app.register_blueprint(x.view, url_prefix='/' + x.name) with self.app.app_context(): db.create_all()
def populate(): from database.database import db with app.app_context(): db.create_all() populate_command_with_db(db)