accountant/accountant/__init__.py

95 lines
2.4 KiB
Python
Raw Normal View History

2013-02-08 13:07:27 +01:00
"""
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.
2015-06-05 18:08:06 +02:00
Accountant is distributed in the hope that it will be useful,
2013-02-08 13:07:27 +01:00
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/>.
"""
2015-06-06 14:04:48 +02:00
from contextlib import contextmanager
2013-02-08 13:07:27 +01:00
2015-07-10 14:59:37 +02:00
import logging
2015-08-16 00:33:35 +02:00
from flask import Flask, redirect, url_for
from flask.ext.sqlalchemy import SQLAlchemy
2015-07-29 15:14:44 +02:00
from flask.ext.bower import Bower
2015-08-16 00:33:35 +02:00
from flask.ext.assets import Environment, Bundle
2015-06-06 14:04:48 +02:00
2015-06-05 23:34:04 +02:00
from . import config
2015-07-10 14:59:37 +02:00
logging.basicConfig()
if config.debug:
logging.getLogger().setLevel(logging.DEBUG)
logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
else:
logging.getLogger().setLevel(logging.INFO)
2013-01-24 01:59:42 +01:00
# The app
2015-06-06 14:04:48 +02:00
app = Flask(__name__, static_folder=None)
2013-11-10 20:20:01 +01:00
app.config['SQLALCHEMY_DATABASE_URI'] = config.db_uri
app.config['SQLALCHEMY_RECORD_QUERIES'] = config.debug
2015-06-05 18:15:50 +02:00
app.config['WTF_CSRF_ENABLED'] = False
app.config['SECRET_KEY'] = 'my_secret_key'
2015-07-10 14:59:37 +02:00
app.debug = config.debug
2015-06-06 14:04:48 +02:00
db = SQLAlchemy(app)
2015-06-06 14:04:48 +02:00
@contextmanager
def session_scope():
from accountant import db
session = db.session
try:
yield session
session.commit()
except:
session.rollback()
raise
finally:
session.close()
2015-06-11 23:27:55 +02:00
def session_aware(f):
def wrapper(*args, **kwargs):
with session_scope() as session:
kwargs['session'] = session
return f(*args, **kwargs)
return wrapper
2015-06-06 14:04:48 +02:00
# Must be after db declaration because the blueprints may need it.
from .api import api
2015-08-16 00:33:35 +02:00
from .frontend import frontend, frontend_js, frontend_css
2013-12-03 22:22:25 +01:00
2015-07-29 15:14:44 +02:00
app.register_blueprint(frontend, url_prefix='/app')
2013-12-03 22:22:25 +01:00
app.register_blueprint(api, url_prefix='/api')
2015-07-29 15:14:44 +02:00
app.config['BOWER_COMPONENTS_ROOT'] = "../bower_components"
app.config['BOWER_TRY_MINIFIED'] = not config.debug
bower = Bower(app)
2015-08-16 00:33:35 +02:00
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'))