2013-12-10 22:22:57 +00:00
|
|
|
import sys
|
2013-12-09 19:53:20 +00:00
|
|
|
import logging
|
2013-12-10 22:22:57 +00:00
|
|
|
import argparse
|
2013-12-09 19:53:20 +00:00
|
|
|
|
2013-12-10 23:25:16 +00:00
|
|
|
from flask import Flask, jsonify, request
|
2013-12-09 19:53:20 +00:00
|
|
|
|
2013-12-10 23:25:16 +00:00
|
|
|
from accounting import Ledger
|
2013-12-10 22:22:57 +00:00
|
|
|
from accounting.transport import AccountingEncoder, AccountingDecoder
|
2013-12-10 23:25:16 +00:00
|
|
|
from accounting.exceptions import AccountingException
|
|
|
|
from accounting.decorators import jsonify_exceptions
|
2013-12-09 19:53:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
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-10 22:22:57 +00:00
|
|
|
# These will convert output from our internal classes to JSON and back
|
2013-12-09 19:53:20 +00:00
|
|
|
app.json_encoder = AccountingEncoder
|
2013-12-10 22:22:57 +00:00
|
|
|
app.json_decoder = AccountingDecoder
|
2013-12-09 19:53:20 +00:00
|
|
|
|
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():
|
2013-12-10 22:22:57 +00:00
|
|
|
''' Returns the balance report from ledger '''
|
2013-12-09 19:53:20 +00:00
|
|
|
report_data = ledger.bal()
|
|
|
|
|
|
|
|
return jsonify(balance_report=report_data)
|
|
|
|
|
2013-12-09 20:49:38 +00:00
|
|
|
|
2013-12-10 23:25:16 +00:00
|
|
|
@app.route('/transaction', methods=['POST'])
|
|
|
|
@jsonify_exceptions
|
|
|
|
def transaction():
|
|
|
|
'''
|
|
|
|
REST/JSON endpoint for transactions.
|
|
|
|
|
|
|
|
Current state:
|
|
|
|
|
|
|
|
Takes a POST request with a ``transactions`` JSON payload and writes it to
|
|
|
|
the ledger file.
|
|
|
|
|
|
|
|
Requires the ``transactions`` payload to be __type__-annotated:
|
|
|
|
|
|
|
|
.. code-block:: json
|
|
|
|
|
|
|
|
{
|
|
|
|
"transactions": [
|
|
|
|
{
|
|
|
|
"__type__": "Transaction",
|
|
|
|
"date": "2013-01-01",
|
|
|
|
"payee": "Kindly T. Donor",
|
|
|
|
"postings": [
|
|
|
|
{
|
|
|
|
"__type__": "Posting",
|
|
|
|
"account": "Income:Foo:Donation",
|
|
|
|
"amount": {
|
|
|
|
"__type__": "Amount",
|
|
|
|
"amount": "-100",
|
|
|
|
"symbol": "$"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"__type__": "Posting",
|
|
|
|
"account": "Assets:Checking",
|
|
|
|
"amount": {
|
|
|
|
"__type__": "Amount",
|
|
|
|
"amount": "100",
|
|
|
|
"symbol": "$"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
becomes::
|
|
|
|
|
|
|
|
2013-01-01 Kindly T. Donor
|
|
|
|
Income:Foo:Donation $ -100
|
|
|
|
Assets:Checking $ 100
|
|
|
|
'''
|
|
|
|
transactions = request.json.get('transactions')
|
|
|
|
|
|
|
|
if not transactions:
|
|
|
|
raise AccountingException('No transaction data provided')
|
|
|
|
|
|
|
|
for transaction in transactions:
|
|
|
|
ledger.add_transaction(transaction)
|
|
|
|
|
|
|
|
return jsonify(foo='bar')
|
|
|
|
|
|
|
|
|
2013-12-10 22:22:57 +00:00
|
|
|
@app.route('/parse-json', methods=['POST'])
|
|
|
|
def parse_json():
|
|
|
|
r'''
|
|
|
|
Parses a __type__-annotated JSON payload and debug-logs the decoded version
|
|
|
|
of it.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
wget http://127.0.0.1:5000/balance -O balance.json
|
|
|
|
curl -X POST -H 'Content-Type: application/json' -d @balance.json \
|
|
|
|
http://127.0.0.1/parse-json
|
|
|
|
# Logging output (linebreaks added for clarity)
|
|
|
|
DEBUG:accounting:json data: {'balance_report':
|
|
|
|
[<Account "None" [
|
|
|
|
<Amount $ 0>, <Amount KARMA 0>]
|
|
|
|
[<Account "Assets" [
|
|
|
|
<Amount $ 50>, <Amount KARMA 10>]
|
|
|
|
[<Account "Assets:Checking" [
|
|
|
|
<Amount $ 50>] []>,
|
|
|
|
<Account "Assets:Karma Account" [
|
|
|
|
<Amount KARMA 10>] []>]>,
|
|
|
|
<Account "Expenses" [
|
|
|
|
<Amount $ 500>]
|
|
|
|
[<Account "Expenses:Blah" [
|
|
|
|
<Amount $ 250>]
|
|
|
|
[<Account "Expenses:Blah:Hosting" [
|
|
|
|
<Amount $ 250>] []>]>,
|
|
|
|
<Account "Expenses:Foo" [
|
|
|
|
<Amount $ 250>] [
|
|
|
|
<Account "Expenses:Foo:Hosting" [
|
|
|
|
<Amount $ 250>] []>]>]>,
|
|
|
|
<Account "Income" [
|
|
|
|
<Amount $ -550>,
|
|
|
|
<Amount KARMA -10>]
|
|
|
|
[<Account "Income:Donation" [
|
|
|
|
<Amount $ -50>] []>,
|
|
|
|
<Account "Income:Foo" [
|
|
|
|
<Amount $ -500>]
|
|
|
|
[<Account "Income:Foo:Donation" [
|
|
|
|
<Amount $ -500>] []>]>,
|
|
|
|
<Account "Income:Karma" [
|
|
|
|
<Amount KARMA -10>] []>]>]>]}
|
|
|
|
'''
|
|
|
|
app.logger.debug('json data: %s', request.json)
|
|
|
|
return jsonify(foo='bar')
|
|
|
|
|
|
|
|
|
2013-12-09 19:53:20 +00:00
|
|
|
@app.route('/register')
|
|
|
|
def register_report():
|
2013-12-10 22:22:57 +00:00
|
|
|
''' Returns the register report from ledger '''
|
2013-12-09 19:53:20 +00:00
|
|
|
report_data = ledger.reg()
|
|
|
|
|
|
|
|
return jsonify(register_report=report_data)
|
|
|
|
|
|
|
|
|
2013-12-10 22:22:57 +00:00
|
|
|
def main(argv=None):
|
|
|
|
prog = __name__
|
|
|
|
if argv is None:
|
|
|
|
prog = sys.argv[0]
|
|
|
|
argv = sys.argv[1:]
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(prog=prog)
|
|
|
|
parser.add_argument('-v', '--verbosity',
|
|
|
|
default='INFO',
|
|
|
|
help=('Filter logging output. Possible values:' +
|
|
|
|
' CRITICAL, ERROR, WARNING, INFO, DEBUG'))
|
|
|
|
|
|
|
|
args = parser.parse_args(argv)
|
|
|
|
|
|
|
|
logging.basicConfig(level=getattr(logging, args.verbosity, 'INFO'))
|
|
|
|
|
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()
|