DrCr/drcr/webapp.py

67 lines
2.2 KiB
Python

# DrCr: Web-based double-entry bookkeeping framework
# Copyright (C) 2022 Lee Yingtong Li (RunasSudo)
#
# This program 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.
#
# This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
from flask import Flask, g
from flask_sqlalchemy.record_queries import get_recorded_queries
from .database import db
from .general_journal.models import GeneralJournalTransaction
from .statements.models import StatementLine, StatementLineTransaction
import time
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///drcr.db'
app.config['SQLALCHEMY_RECORD_QUERIES'] = app.debug
db.init_app(app)
def all_transactions():
return (
GeneralJournalTransaction.query.all() +
StatementLineTransaction.query.all() +
[line.into_transaction() for line in StatementLine.query.all() if len(line.postings) == 0] # TODO: Filter in SQL
)
from . import views
from .general_journal import views
from .reports import views
from .statements import views
@app.cli.command('initdb')
def initdb():
db.create_all()
if app.debug:
@app.before_request
def before_request():
g.start = time.time()
@app.after_request
def after_request(response):
diff = time.time() - g.start
if response.response and response.status_code == 200 and response.content_type.startswith('text/html'):
response.set_data(response.get_data().replace(b'__EXECUTION_TIME__', bytes(format(diff * 1000, '.1f'), 'utf-8')))
return response
@app.context_processor
def add_dbtime():
def dbtime():
queries = get_recorded_queries()
total_duration = sum(q.duration for q in queries)
return format(total_duration * 1000, '.1f')
return dict(dbtime=dbtime)