diff --git a/accounting/web.py b/accounting/web.py index 8140ad1f0a7ca0ece51f4bd385a4a359d8b90e23..a6f17abe50b3e21ede843028bc5b0849648bddeb 100644 --- a/accounting/web.py +++ b/accounting/web.py @@ -21,14 +21,15 @@ from accounting.decorators import jsonify_exceptions app = Flask('accounting') app.config.from_pyfile('config.py') -storage = SQLStorage(app) +storage = Ledger(app=app) -# TODO: Move migration stuff into SQLStorage -db = storage.db -migrate = Migrate(app, db) +if isinstance(storage, SQLStorage): + # TODO: Move migration stuff into SQLStorage + db = storage.db + migrate = Migrate(app, db) -manager = Manager(app) -manager.add_command('db', MigrateCommand) + manager = Manager(app) + manager.add_command('db', MigrateCommand) @app.before_request @@ -59,6 +60,24 @@ def transaction_get(): ''' return jsonify(transactions=storage.get_transactions()) +@app.route('/transaction/', methods=['POST']) +@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 + + storage.update_transaction(transaction) + + return jsonify(status='OK') + @app.route('/transaction', methods=['POST']) @jsonify_exceptions @@ -118,56 +137,7 @@ def transaction_post(): for transaction in transactions: storage.add_transaction(transaction) - return jsonify(foo='bar') - - -@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: - - .. code-block:: bash - - 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': - # [, ] - # [, ] - # [] []>, - # ] []>]>, - # ] - # [] - # [] []>]>, - # ] [ - # ] []>]>]>, - # , - # ] - # [] []>, - # ] - # [] []>]>, - # ] []>]>]>]} - ''' - app.logger.debug('json data: %s', request.json) - return jsonify(foo='bar') + return jsonify(status='OK') def main(argv=None):