database.py 2.13 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#
#  engine
#
#  Playout Daemon for autoradio project
#
#
#  Copyright (C) 2017-2018 Gottfried Gaisbauer <gottfried.gaisbauer@servus.at>
#
#  This file is part of engine.
#
#  engine is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  any later version.
#
#  engine is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with engine. If not, see <http://www.gnu.org/licenses/>.
#
24

25
from sqlalchemy.ext.declarative import declarative_base
26
from flask_sqlalchemy import SQLAlchemy
27
# from flask_babel import Babel
28
29
from flask import Flask

30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from modules.base.config import ConfigReader # pylint: disable=import-error

def create_app(install_dir, uri):
    """
    creates flask app context
    :param install_dir: Installdir of Aura
    :param uri: Database connection uri
    :return: Flask object
    """
    app = Flask(__name__, template_folder=install_dir + '/modules/web/templates')
    app.config["SQLALCHEMY_DATABASE_URI"] = uri
    app.config['BABEL_DEFAULT_LOCALE'] = 'de'
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

    return app

def create_database():
    """
    creates sqlalchemy database connection
    :return: SQLAlchemy object
    """
    #### load config ####
    config = ConfigReader()
    config.load_config()
54

55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
    #### read config ####
    install_dir = config.get(str("install_dir"))
    db_name = config.get(str("db_name"))
    db_user = config.get(str("db_user"))
    db_pass = config.get(str("db_pass"))
    db_host = config.get(str("db_host"))

    #### create database conn ####
    uri = "mysql://"+db_user+":"+db_pass+"@"+db_host+"/"+db_name+"?charset=utf8"

    app = create_app(install_dir, uri)

    database = SQLAlchemy(app)
#    babel = Babel(app)

    return app, database


Base = declarative_base()
APP, DB = create_database()