accountant/accountant/__init__.py

63 lines
1.5 KiB
Python
Raw Normal View History

"""The accountant package."""
# vim: set tw=80 ts=4 sw=4 sts=4:
import click
2017-05-04 14:42:09 +02:00
from flask import Flask
2016-01-13 00:10:51 +01:00
from flask_alembic import Alembic
from flask_alembic import alembic_click
2017-05-04 14:39:17 +02:00
from .models import db
2017-05-19 00:07:30 +02:00
from .views import api, cors, jwt
2015-07-10 14:59:37 +02:00
2017-05-13 10:31:17 +02:00
# pylint: disable=invalid-name
alembic = Alembic()
2017-05-13 10:31:17 +02:00
def create_app(config_path):
"""Create the app using specific config file."""
# The app
app = Flask(__name__, static_folder=None, template_folder=None)
2015-07-10 14:59:37 +02:00
2017-05-13 10:31:17 +02:00
# Configure it from configuration file.
app.config.from_pyfile(config_path)
2017-05-13 10:31:17 +02:00
# Database related stuff.
app.config['SQLALCHEMY_ECHO'] = app.debug
app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
2017-05-13 10:31:17 +02:00
db.init_app(app)
2017-05-13 10:31:17 +02:00
alembic.init_app(app)
2017-05-13 10:31:17 +02:00
# API views related stuff.
cors.init_app(app)
api.init_app(app)
2017-05-19 00:07:30 +02:00
jwt.init_app(app)
# Needed to handle correctly JWT exceptions by restplus.
jwt._set_error_handler_callbacks(api) # pylint: disable=protected-access
2017-05-13 10:31:17 +02:00
return app
# Database initialization.
@alembic_click.command()
def initdb():
""" Create the database ans stamp it. """
click.echo("Creating database.")
db.metadata.create_all(bind=db.engine)
click.echo("Stamping database.")
alembic.stamp()
click.echo("Database created.")
2017-05-04 14:42:38 +02:00
@alembic_click.command()
def dropdb():
"""Clear database schema. USE CAREFULLY!!"""
click.echo("Clearing database.")
db.metadata.drop_all(bind=db.engine)
click.echo("Database cleared.")