63 lines
1.5 KiB
Python
63 lines
1.5 KiB
Python
"""The accountant package."""
|
|
|
|
# vim: set tw=80 ts=4 sw=4 sts=4:
|
|
|
|
import click
|
|
|
|
from flask import Flask
|
|
|
|
from flask_alembic import Alembic
|
|
from flask_alembic import alembic_click
|
|
|
|
from .models import db
|
|
from .views import api, cors, jwt
|
|
|
|
# pylint: disable=invalid-name
|
|
alembic = Alembic()
|
|
|
|
|
|
def create_app(config_path):
|
|
"""Create the app using specific config file."""
|
|
# The app
|
|
app = Flask(__name__, static_folder=None, template_folder=None)
|
|
|
|
# Configure it from configuration file.
|
|
app.config.from_pyfile(config_path)
|
|
|
|
# Database related stuff.
|
|
app.config['SQLALCHEMY_ECHO'] = app.debug
|
|
app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True
|
|
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
|
|
|
|
db.init_app(app)
|
|
|
|
alembic.init_app(app)
|
|
|
|
# API views related stuff.
|
|
cors.init_app(app)
|
|
api.init_app(app)
|
|
jwt.init_app(app)
|
|
# Needed to handle correctly JWT exceptions by restplus.
|
|
jwt._set_error_handler_callbacks(api) # pylint: disable=protected-access
|
|
|
|
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.")
|
|
|
|
|
|
@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.")
|