From a27f33aabf2d4a5819df86c1089f27b2d99de4f2 Mon Sep 17 00:00:00 2001 From: Alexis Lahouze Date: Fri, 19 May 2017 11:49:54 +0200 Subject: [PATCH] Rename id parameters, improve doc for some resources. --- accountant/views/accounts.py | 120 +++++++++++++---------- accountant/views/operations.py | 24 ++--- accountant/views/scheduled_operations.py | 29 +++--- 3 files changed, 95 insertions(+), 78 deletions(-) diff --git a/accountant/views/accounts.py b/accountant/views/accounts.py index 7896191..09980cb 100644 --- a/accountant/views/accounts.py +++ b/accountant/views/accounts.py @@ -165,11 +165,11 @@ class AccountListResource(Resource): return account, 201 -@ns.route('/') +@ns.route('/') @ns.doc( security='apikey', params={ - 'id': 'Id of the account to manage' + 'account_id': 'Id of the account to manage' }, responses={ 401: 'Unauthorized', @@ -181,15 +181,15 @@ class AccountResource(Resource): @ns.response(200, 'OK', account_model) @ns.marshal_with(account_model) @jwt_required - def get(self, id): + def get(self, account_id): """Get an account.""" - account = Account.query().get(id) + account = Account.query().get(account_id) if not account: ns.abort( 404, - error_message='Account with id %d not found.' % id + error_message='Account with id %d not found.' % account_id ) # Note: if we don't pass the code, the result is seen as a tuple and @@ -201,25 +201,25 @@ class AccountResource(Resource): @ns.response(406, 'Invalid account data') @ns.marshal_with(account_model) @jwt_required - def post(self, id): + def post(self, account_id): """Update an account.""" data = self.api.payload # Check ID consistency. - if 'id' in data and data['id'] and data['id'] != id: + if 'id' in data and data['id'] and data['id'] != account_id: ns.abort( 406, error_message='Id must not be provided or changed on update.' ) # Need to get the object to update it. - account = Account.query().get(id) + account = Account.query().get(account_id) if not account: ns.abort( 404, - error_message='Account with id %d not found.' % id + error_message='Account with id %d not found.' % account_id ) # SQLAlchemy objects ignore __dict__.update() with merge. @@ -234,16 +234,16 @@ class AccountResource(Resource): @ns.response(204, 'Account deleted', account_model) @ns.marshal_with(account_model) @jwt_required - def delete(self, id): + def delete(self, account_id): """Delete an account.""" # Need to get the object to update it. - account = Account.query().get(id) + account = Account.query().get(account_id) if not account: ns.abort( 404, - error_message='Account with id %d not found.' % id + error_message='Account with id %d not found.' % account_id ) db.session.delete(account) @@ -251,28 +251,31 @@ class AccountResource(Resource): return None, 204 -@ns.route('//solds') +@ns.route('//solds') +@ns.doc( + security='apikey', + params={ + 'account_id': 'Id of the account to manage' + }, + responses={ + 200: ('OK', solds_model), + 401: 'Unauthorized', + 404: 'Account not found' + }) class SoldsResource(Resource): """Resource to expose solds.""" - @ns.doc( - security='apikey', - responses={ - 200: ('OK', solds_model), - 401: 'Unauthorized', - 404: 'Account not found' - }) @ns.marshal_with(solds_model) @jwt_required - def get(self, id): + def get(self, account_id): """Get solds for a specific account and date range.""" - account = Account.query().get(id) + account = Account.query().get(account_id) if not account: ns.abort( 404, - error_message='Account with id %d not found.' % id + error_message='Account with id %d not found.' % account_id ) # Note: if we don't pass the code, the result is seen as a tuple and @@ -280,29 +283,32 @@ class SoldsResource(Resource): return account.solds(), 200 -@ns.route('//balance') +@ns.route('//balance') +@ns.doc( + security='apikey', + params={ + 'account_id': 'Id of the account to manage' + }, + responses={ + 200: ('OK', balance_model), + 401: 'Unauthorized', + 404: 'Account not found' + }) class BalanceResource(Resource): """Resource to expose balances.""" - @ns.doc( - security='apikey', - responses={ - 200: ('OK', balance_model), - 401: 'Unauthorized', - 404: 'Account not found' - }) @ns.expect(range_parser) @ns.marshal_with(balance_model) @jwt_required - def get(self, id): + def get(self, account_id): """Get account balance for a specific date range.""" - account = Account.query().get(id) + account = Account.query().get(account_id) if not account: ns.abort( 404, - error_message='Account with id %d not found.' % id + error_message='Account with id %d not found.' % account_id ) data = range_parser.parse_args() @@ -312,45 +318,51 @@ class BalanceResource(Resource): return account.balance(**data), 200 -@ns.route("//category") +@ns.route("//category") +@ns.doc( + security='apikey', + params={ + 'account_id': 'Id of the account to manage' + }, + responses={ + 200: ('OK', [category_model]), + 401: 'Unauthorized', + 404: 'Account not found' + }) class CategoryResource(Resource): """Resource to expose categories.""" - @ns.doc( - security='apikey', - responses={ - 200: ('OK', [category_model]), - 401: 'Unauthorized', - 404: 'Account not found' - }) @ns.expect(range_parser) @ns.marshal_list_with(category_model) @jwt_required - def get(self, id): + def get(self, account_id): """Get account category balances for a specific date range.""" data = range_parser.parse_args() - return Operation.get_categories_for_range(id, **data).all() + return Operation.get_categories_for_range(account_id, **data).all() -@ns.route('//ohlc') +@ns.route('//ohlc') +@ns.doc( + security='apikey', + params={ + 'account_id': 'Id of the account to manage' + }, + responses={ + 200: ('OK', [ohlc_model]), + 401: 'Unauthorized', + 404: 'Account not found' + }) class OHLCResource(Resource): """Resource to expose OHLC.""" - @ns.doc( - security='apikey', - responses={ - 200: ('OK', [ohlc_model]), - 401: 'Unauthorized', - 404: 'Account not found' - }) @ns.expect(range_parser) @ns.marshal_list_with(ohlc_model) @jwt_required - def get(self, id): + def get(self, account_id): """Get OHLC data for a specific date range and account.""" data = range_parser.parse_args() - return Operation.get_ohlc_per_day_for_range(id, **data).all() + return Operation.get_ohlc_per_day_for_range(account_id, **data).all() diff --git a/accountant/views/operations.py b/accountant/views/operations.py index faaf91b..08cf746 100644 --- a/accountant/views/operations.py +++ b/accountant/views/operations.py @@ -146,11 +146,11 @@ class OperationListResource(Resource): return operation, 201 -@ns.route('/') +@ns.route('/') @ns.doc( security='apikey', params={ - 'id': 'Id of the operation to manage' + 'operation_id': 'Id of the operation to manage' }, responses={ 401: 'Unauthorized', @@ -162,15 +162,15 @@ class OperationResource(Resource): @ns.response(200, 'OK', operation_model) @ns.marshal_with(operation_model) @jwt_required - def get(self, id): + def get(self, operation_id): """Get operation.""" - operation = db.session.query(Operation).get(id) + operation = db.session.query(Operation).get(operation_id) if not operation: ns.abort( 404, - error_message='Operation with id %d not found.' % id + error_message='Operation with id %d not found.' % operation_id ) return operation, 200 @@ -180,24 +180,24 @@ class OperationResource(Resource): @ns.response(406, 'Invalid operation data') @ns.marshal_with(operation_model) @jwt_required - def post(self, id): + def post(self, operation_id): """Update an operation.""" data = self.api.payload # Check ID consistency. - if 'id' in data and data['id'] and data['id'] != id: + if 'id' in data and data['id'] and data['id'] != operation_id: ns.abort( 406, error_message='Id must not be provided or changed on update.' ) - operation = db.session.query(Operation).get(id) + operation = db.session.query(Operation).get(operation_id) if not operation: ns.abort( 404, - error_message='Operation with id %d not found.' % id + error_message='Operation with id %d not found.' % operation_id ) # FIXME check account_id consistency. @@ -213,15 +213,15 @@ class OperationResource(Resource): @ns.response(204, 'Operation deleted', operation_model) @ns.marshal_with(operation_model) @jwt_required - def delete(self, id): + def delete(self, operation_id): """Delete an operation.""" - operation = db.session.query(Operation).get(id) + operation = db.session.query(Operation).get(operation_id) if not operation: ns.abort( 404, - error_message='Operation with id %d not found.' % id + error_message='Operation with id %d not found.' % operation_id ) db.session.delete(operation) diff --git a/accountant/views/scheduled_operations.py b/accountant/views/scheduled_operations.py index 70b74d5..7aa2c12 100644 --- a/accountant/views/scheduled_operations.py +++ b/accountant/views/scheduled_operations.py @@ -124,11 +124,11 @@ class ScheduledOperationListResource(Resource): return scheduled_operation, 201 -@ns.route('/') +@ns.route('/') @ns.doc( security='apikey', params={ - 'id': 'Id of the scheduled operation to manage' + 'scheduled_operation_id': 'Id of the scheduled operation to manage' }, responses={ 401: 'Unauthorized', @@ -140,15 +140,17 @@ class ScheduledOperationResource(Resource): @ns.response(200, 'OK', scheduled_operation_model) @ns.marshal_with(scheduled_operation_model) @jwt_required - def get(self, id): + def get(self, scheduled_operation_id): """Get scheduled operation.""" - scheduled_operation = ScheduledOperation.query().get(id) + so_id = scheduled_operation_id + + scheduled_operation = ScheduledOperation.query().get(so_id) if not scheduled_operation: ns.abort( 404, - error_message='Scheduled operation with id %d not found.' % id + error_message='Scheduled operation with id %d not found.' % so_id ) return scheduled_operation, 200 @@ -158,24 +160,25 @@ class ScheduledOperationResource(Resource): @ns.expect(scheduled_operation_model) @ns.marshal_with(scheduled_operation_model) @jwt_required - def post(self, id): + def post(self, scheduled_operation_id): """Update a scheduled operation.""" data = self.api.payload + so_id = scheduled_operation_id # Check ID consistency. - if 'id' in data and data['id'] and data['id'] != id: + if 'id' in data and data['id'] and data['id'] != so_id: ns.abort( 406, error_message='Id must not be provided or changed on update.' ) - scheduled_operation = ScheduledOperation.query().get(id) + scheduled_operation = ScheduledOperation.query().get(so_id) if not scheduled_operation: ns.abort( 404, - error_message='Scheduled operation with id %d not found.' % id + error_message='Scheduled operation with id %d not found.' % so_id ) # FIXME check account_id consistency. @@ -196,15 +199,17 @@ class ScheduledOperationResource(Resource): @ns.response(409, 'Cannot be deleted') @ns.marshal_with(scheduled_operation_model) @jwt_required - def delete(self, id): + def delete(self, scheduled_operation_id): """Delete a scheduled operation.""" - scheduled_operation = ScheduledOperation.query().get(id) + so_id = scheduled_operation_id + + scheduled_operation = ScheduledOperation.query().get(so_id) if not scheduled_operation: ns.abort( 404, - error_message='Scheduled operation with id %d not found.' % id + error_message='Scheduled operation with id %d not found.' % so_id ) operations = scheduled_operation.operations.filter(