accountant/accountant/__init__.py

75 lines
2.0 KiB
Python

"""
This file is part of Accountant.
Accountant is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Accountant 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with Accountant. If not, see <http://www.gnu.org/licenses/>.
"""
import click
from flask import Flask, redirect, url_for
from flask_alembic import Alembic
from flask_alembic.cli.click import cli as alembic_cli
from flask_restplus import Api
from flask_cors import CORS
from flask_sqlalchemy import SQLAlchemy
# The app
app = Flask(__name__, static_folder=None, template_folder=None)
# Configure it from config.cfg.
app.config.from_pyfile('config.cfg')
app.config['SQLALCHEMY_ECHO'] = app.debug
app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
# Database initialization.
db = SQLAlchemy(app)
# Database migrations.
alembic = Alembic(app)
app.cli.add_command(alembic_cli, 'db')
# Database initialization.
@app.cli.command()
@click.pass_context
def initdb(ctx):
""" Create the database ans stamp it. """
click.echo("Get table list.")
tables = db.engine.table_names()
if 'alembic_version' in tables:
ctx.fail("Database already initialized.")
#db.metadata.create_all(bind=db.engine)
#alembic.stamp()
click.echo("Database created.")
# API initialization.
authorizations = {
'apikey': {
'type': 'apiKey',
'in': 'header',
'name': 'Authorization'
}
}
api = Api(app, authorizations=authorizations, prefix='/api')
CORS(app)
# Load all views.
from .views import * # flake8: noqa