Files
@ 4fce6b4feaa7
Branch filter:
Location: NPO-Accounting/experimental-accounting-api/accounting/web.py - annotation
4fce6b4feaa7
5.4 KiB
text/x-python
[tests] Added bin/runtests
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | fc2c3a6b4339 fc2c3a6b4339 fc2c3a6b4339 fc2c3a6b4339 c0b791b30f57 c0b791b30f57 c0b791b30f57 c0b791b30f57 63c7b70000da f8f51f74897d 63c7b70000da f8f51f74897d 281d6fed47b7 124bd1706d79 124bd1706d79 f8f51f74897d 2eecd641dda8 873cff4229e3 124bd1706d79 124bd1706d79 63c7b70000da 281d6fed47b7 2eecd641dda8 f8f51f74897d f8f51f74897d f8f51f74897d f8f51f74897d f8f51f74897d 281d6fed47b7 124bd1706d79 124bd1706d79 c0b791b30f57 281d6fed47b7 f8f51f74897d af6e28d1680a 63c7b70000da f8f51f74897d 63c7b70000da f8f51f74897d af6e28d1680a f8f51f74897d f8f51f74897d c0b791b30f57 f8f51f74897d f8f51f74897d 2eecd641dda8 2eecd641dda8 2eecd641dda8 2eecd641dda8 2eecd641dda8 2eecd641dda8 2eecd641dda8 2eecd641dda8 2eecd641dda8 2eecd641dda8 2eecd641dda8 2eecd641dda8 2eecd641dda8 2eecd641dda8 2eecd641dda8 2eecd641dda8 2eecd641dda8 2eecd641dda8 af6e28d1680a f0a5db5a2907 2eecd641dda8 2eecd641dda8 2eecd641dda8 2eecd641dda8 c0b791b30f57 c0b791b30f57 c0b791b30f57 2eecd641dda8 281d6fed47b7 2eecd641dda8 281d6fed47b7 281d6fed47b7 281d6fed47b7 281d6fed47b7 124bd1706d79 02fc05aebdde f2b9decf2752 2eecd641dda8 f2b9decf2752 f2b9decf2752 f2b9decf2752 f2b9decf2752 f2b9decf2752 f2b9decf2752 f2b9decf2752 f2b9decf2752 02fc05aebdde 02fc05aebdde f2b9decf2752 f2b9decf2752 f2b9decf2752 281d6fed47b7 f2b9decf2752 f2b9decf2752 f2b9decf2752 af6e28d1680a 02fc05aebdde 2eecd641dda8 02fc05aebdde 02fc05aebdde 02fc05aebdde 02fc05aebdde 02fc05aebdde 281d6fed47b7 02fc05aebdde 02fc05aebdde 02fc05aebdde 02fc05aebdde 6f2c875c7b76 2eecd641dda8 6f2c875c7b76 f0a5db5a2907 6f2c875c7b76 6f2c875c7b76 6f2c875c7b76 6f2c875c7b76 6f2c875c7b76 6f2c875c7b76 6f2c875c7b76 6f2c875c7b76 6f2c875c7b76 6f2c875c7b76 6f2c875c7b76 6f2c875c7b76 6f2c875c7b76 6f2c875c7b76 6f2c875c7b76 6f2c875c7b76 6f2c875c7b76 6f2c875c7b76 6f2c875c7b76 6f2c875c7b76 6f2c875c7b76 6f2c875c7b76 6f2c875c7b76 6f2c875c7b76 6f2c875c7b76 6f2c875c7b76 6f2c875c7b76 6f2c875c7b76 6f2c875c7b76 6f2c875c7b76 6f2c875c7b76 6f2c875c7b76 6f2c875c7b76 6f2c875c7b76 6f2c875c7b76 6f2c875c7b76 6f2c875c7b76 6f2c875c7b76 6f2c875c7b76 6f2c875c7b76 6f2c875c7b76 6f2c875c7b76 6f2c875c7b76 6f2c875c7b76 6f2c875c7b76 6f2c875c7b76 6f2c875c7b76 2eecd641dda8 2eecd641dda8 2eecd641dda8 2eecd641dda8 6f2c875c7b76 6f2c875c7b76 6f2c875c7b76 6f2c875c7b76 02fc05aebdde 02fc05aebdde 6f2c875c7b76 281d6fed47b7 6f2c875c7b76 02fc05aebdde 63c7b70000da 63c7b70000da 63c7b70000da 63c7b70000da 63c7b70000da 63c7b70000da 63c7b70000da 63c7b70000da 63c7b70000da 63c7b70000da 63c7b70000da 63c7b70000da 124bd1706d79 63c7b70000da 281d6fed47b7 873cff4229e3 63c7b70000da 63c7b70000da 63c7b70000da 63c7b70000da dd260aa02127 f8f51f74897d f8f51f74897d f8f51f74897d | # Part of accounting-api project:
# https://gitorious.org/conservancy/accounting-api
# License: AGPLv3-or-later
'''
This module contains the high-level webservice logic such as the Flask setup
and the Flask endpoints.
'''
import sys
import logging
import argparse
from flask import Flask, jsonify, request, render_template, abort
from flask.ext.script import Manager
from flask.ext.migrate import Migrate, MigrateCommand
from accounting.models import Transaction
from accounting.storage import Storage
from accounting.storage.ledgercli import Ledger
from accounting.storage.sql import SQLStorage
from accounting.transport import AccountingEncoder, AccountingDecoder
from accounting.exceptions import AccountingException, TransactionNotFound
from accounting.decorators import jsonify_exceptions, cors
app = Flask('accounting')
app.config.from_pyfile('config.py')
app.ledger = Storage()
def init_ledger():
app.ledger = Ledger(app)
# These will convert output from our internal classes to JSON and back
app.json_encoder = AccountingEncoder
app.json_decoder = AccountingDecoder
@app.route('/')
def index():
''' Hello World! '''
return 'Hello World!'
@app.route('/client')
def client():
return render_template('client.html')
@app.route('/transaction', methods=['OPTIONS'])
@cors()
@jsonify_exceptions
def transaction_options():
return jsonify(status='OPTIONS')
@app.route('/transaction/<string:transaction_id>', methods=['OPTIONS'])
@cors()
@jsonify_exceptions
def transaction_by_id_options(transaction_id=None):
return jsonify(status='OPTIONS')
@app.route('/transaction', methods=['GET'])
@app.route('/transaction/<string:transaction_id>', methods=['GET'])
@cors()
@jsonify_exceptions
def transaction_get(transaction_id=None):
'''
Returns the JSON-serialized output of :meth:`accounting.Ledger.reg`
'''
if transaction_id is None:
return jsonify(transactions=app.ledger.get_transactions())
try:
return jsonify(transaction=app.ledger.get_transaction(transaction_id))
except TransactionNotFound:
abort(404)
@app.route('/transaction/<string:transaction_id>', methods=['POST'])
@cors()
@jsonify_exceptions
def transaction_update(transaction_id=None):
if transaction_id is None:
raise AccountingException('The transaction ID cannot be None.')
transaction = request.json['transaction']
if transaction.id is not None and not transaction.id == transaction_id:
raise AccountingException('The transaction data has an ID attribute'
' and it is not the same ID as in the path')
elif transaction.id is None:
transaction.id = transaction_id
app.ledger.update_transaction(transaction)
return jsonify(status='OK')
@app.route('/transaction/<string:transaction_id>', methods=['DELETE'])
@cors()
@jsonify_exceptions
def transaction_delete(transaction_id=None):
if transaction_id is None:
raise AccountingException('Transaction ID cannot be None')
app.ledger.delete_transaction(transaction_id)
return jsonify(status='OK')
@app.route('/transaction', methods=['POST'])
@cors()
@jsonify_exceptions
def transaction_post():
'''
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
'''
if not isinstance(request.json, Transaction):
transactions = request.json.get('transactions')
else:
transactions = [request.json]
if not transactions:
raise AccountingException('No transaction data provided')
transaction_ids = []
for transaction in transactions:
transaction_ids.append(app.ledger.add_transaction(transaction))
return jsonify(status='OK', transaction_ids=transaction_ids)
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'))
init_ledger()
args = parser.parse_args(argv)
logging.basicConfig(level=getattr(logging, args.verbosity, 'INFO'))
app.run(host=app.config['HOST'], port=app.config['PORT'])
if __name__ == '__main__':
main()
|