accountant/accountant/__init__.py
2015-08-16 00:33:35 +02:00

95 lines
2.4 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/>.
"""
from contextlib import contextmanager
import logging
from flask import Flask, redirect, url_for
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.bower import Bower
from flask.ext.assets import Environment, Bundle
from . import config
logging.basicConfig()
if config.debug:
logging.getLogger().setLevel(logging.DEBUG)
logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
else:
logging.getLogger().setLevel(logging.INFO)
# The app
app = Flask(__name__, static_folder=None)
app.config['SQLALCHEMY_DATABASE_URI'] = config.db_uri
app.config['SQLALCHEMY_RECORD_QUERIES'] = config.debug
app.config['WTF_CSRF_ENABLED'] = False
app.config['SECRET_KEY'] = 'my_secret_key'
app.debug = config.debug
db = SQLAlchemy(app)
@contextmanager
def session_scope():
from accountant import db
session = db.session
try:
yield session
session.commit()
except:
session.rollback()
raise
finally:
session.close()
def session_aware(f):
def wrapper(*args, **kwargs):
with session_scope() as session:
kwargs['session'] = session
return f(*args, **kwargs)
return wrapper
# Must be after db declaration because the blueprints may need it.
from .api import api
from .frontend import frontend, frontend_js, frontend_css
app.register_blueprint(frontend, url_prefix='/app')
app.register_blueprint(api, url_prefix='/api')
app.config['BOWER_COMPONENTS_ROOT'] = "../bower_components"
app.config['BOWER_TRY_MINIFIED'] = not config.debug
bower = Bower(app)
assets = Environment(app)
assets.register('frontend_js', frontend_js)
assets.register('frontend_css', frontend_css)
@app.route('/')
def index():
return redirect(url_for('frontend.index'))