experimental-accounting-api/accounting/web.py
2013-12-09 21:49:38 +01:00

69 lines
1.5 KiB
Python

import logging
from flask import Flask, g, jsonify, json
from accounting import Ledger, Account, Posting, Transaction, Amount
logging.basicConfig(level=logging.DEBUG)
app = Flask('accounting')
app.config.from_pyfile('config.py')
ledger = Ledger(ledger_file=app.config['LEDGER_FILE'])
class AccountingEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, Account):
return dict(
name=o.name,
amounts=o.amounts,
accounts=o.accounts
)
elif isinstance(o, Transaction):
return dict(
date=o.date.strftime('%Y-%m-%d'),
payee=o.payee,
postings=o.postings
)
elif isinstance(o, Posting):
return dict(
account=o.account,
amount=o.amount,
)
elif isinstance(o, Amount):
return dict(
amount=o.amount,
symbol=o.symbol
)
return json.JSONEncoder.default(self, o)
app.json_encoder = AccountingEncoder
@app.route('/')
def index():
return 'Hello World!'
@app.route('/balance')
def balance_report():
report_data = ledger.bal()
return jsonify(balance_report=report_data)
@app.route('/register')
def register_report():
report_data = ledger.reg()
return jsonify(register_report=report_data)
def main():
app.run(host=app.config['HOST'], port=app.config['PORT'])
if __name__ == '__main__':
main()