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