Rewrite abort messages.

This commit is contained in:
Alexis Lahouze 2017-05-19 14:16:05 +02:00
parent de5e0482cd
commit af05a3d6ba
4 changed files with 22 additions and 81 deletions

View File

@ -149,10 +149,7 @@ class AccountListResource(Resource):
# A new account MUST NOT have an id;
if 'id' in data and data['id']:
ns.abort(
406,
error_message='Id must not be provided on creation.'
)
ns.abort(406, 'Id must not be provided on creation.')
# Instantiate account with data.
account = Account(**data)
@ -189,10 +186,7 @@ class AccountResource(Resource):
account = Account.query().get(account_id)
if not account:
ns.abort(
404,
error_message='Account with id %d not found.' % account_id
)
ns.abort(404, 'Account with id %d not found.' % account_id)
# Note: if we don't pass the code, the result is seen as a tuple and
# causes error on marshalling.
@ -210,19 +204,13 @@ class AccountResource(Resource):
# Check ID consistency.
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.'
)
ns.abort(406, 'Id must not be provided or changed on update.')
# Need to get the object to update it.
account = Account.query().get(account_id)
if not account:
ns.abort(
404,
error_message='Account with id %d not found.' % account_id
)
ns.abort(404, 'Account with id %d not found.' % account_id)
# SQLAlchemy objects ignore __dict__.update() with merge.
for k, v in data.items():
@ -243,10 +231,7 @@ class AccountResource(Resource):
account = Account.query().get(account_id)
if not account:
ns.abort(
404,
error_message='Account with id %d not found.' % account_id
)
ns.abort(404, 'Account with id %d not found.' % account_id)
db.session.delete(account) # pylint: disable=no-member
@ -275,10 +260,7 @@ class SoldsResource(Resource):
account = Account.query().get(account_id)
if not account:
ns.abort(
404,
error_message='Account with id %d not found.' % account_id
)
ns.abort(404, 'Account with id %d not found.' % account_id)
# Note: if we don't pass the code, the result is seen as a tuple and
# causes error on marshalling.
@ -308,10 +290,7 @@ class BalanceResource(Resource):
account = Account.query().get(account_id)
if not account:
ns.abort(
404,
error_message='Account with id %d not found.' % account_id
)
ns.abort(404, 'Account with id %d not found.' % account_id)
data = range_parser.parse_args()

View File

@ -129,17 +129,11 @@ class OperationListResource(Resource):
account = Account.query().get(account_id)
if not account:
ns.abort(
404,
error_message='Account with id %d not found.' % account_id
)
ns.abort(404, 'Account with id %d not found.' % account_id)
# A new operation MUST NOT have an id;
if 'id' in data and data['id']:
ns.abort(
406,
error_message='Id must not be provided on creation.'
)
ns.abort(406, 'Id must not be provided on creation.')
operation = Operation(**data)
@ -172,10 +166,7 @@ class OperationResource(Resource):
# pylint: enable=no-member
if not operation:
ns.abort(
404,
error_message='Operation with id %d not found.' % operation_id
)
ns.abort(404, 'Operation with id %d not found.' % operation_id)
return operation, 200
@ -191,20 +182,14 @@ class OperationResource(Resource):
# Check ID consistency.
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.'
)
ns.abort(406, 'Id must not be provided or changed on update.')
# pylint: disable=no-member
operation = db.session.query(Operation).get(operation_id)
# pylint: enable=no-member
if not operation:
ns.abort(
404,
error_message='Operation with id %d not found.' % operation_id
)
ns.abort(404, 'Operation with id %d not found.' % operation_id)
# FIXME check account_id consistency.
@ -227,10 +212,7 @@ class OperationResource(Resource):
# pylint: enable=no-member
if not operation:
ns.abort(
404,
error_message='Operation with id %d not found.' % operation_id
)
ns.abort(404, 'Operation with id %d not found.' % operation_id)
db.session.delete(operation) # pylint: disable=no-member

View File

@ -103,17 +103,11 @@ class ScheduledOperationListResource(Resource):
account = Account.query().get(account_id)
if not account:
ns.abort(
404,
error_message='Account with id %d not found.' % account_id
)
ns.abort(404, 'Account with id %d not found.' % account_id)
# A new scheduled operation MUST NOT have an id;
if 'id' in data and data['id']:
ns.abort(
406,
error_message='Id must not be provided on creation.'
)
ns.abort(406, 'Id must not be provided on creation.')
scheduled_operation = ScheduledOperation(**data)
@ -150,10 +144,7 @@ class ScheduledOperationResource(Resource):
scheduled_operation = ScheduledOperation.query().get(so_id)
if not scheduled_operation:
ns.abort(
404,
error_message='Scheduled operation with id %d not found.' % so_id
)
ns.abort(404, 'Scheduled operation with id %d not found.' % so_id)
return scheduled_operation, 200
@ -170,18 +161,12 @@ class ScheduledOperationResource(Resource):
# Check ID consistency.
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.'
)
ns.abort(406, 'Id must not be provided or changed on update.')
scheduled_operation = ScheduledOperation.query().get(so_id)
if not scheduled_operation:
ns.abort(
404,
error_message='Scheduled operation with id %d not found.' % so_id
)
ns.abort(404, 'Scheduled operation with id %d not found.' % so_id)
# FIXME check account_id consistency.
@ -209,20 +194,15 @@ class ScheduledOperationResource(Resource):
scheduled_operation = ScheduledOperation.query().get(so_id)
if not scheduled_operation:
ns.abort(
404,
error_message='Scheduled operation with id %d not found.' % so_id
)
ns.abort(404, 'Scheduled operation with id %d not found.' % so_id)
operations = scheduled_operation.operations.filter(
Operation.confirmed == true()
).count()
if operations:
ns.abort(
409,
error_message='There are still confirmed operations \
associated to this scheduled operation.')
ns.abort(409, 'There are still confirmed operations '
'associated to this scheduled operation.')
# Delete unconfirmed operations
scheduled_operation.operations.delete()

View File

@ -72,7 +72,7 @@ class LoginResource(Resource):
).one_or_none()
if not user or not user.verify_password(password):
ns.abort(401, error_message="Bad user or password.")
ns.abort(401, "Bad user or password.")
return {
'access_token': create_access_token(identity=user),