From 51763e0ddf520e9c19aba3ab501a9cbd46ea91df Mon Sep 17 00:00:00 2001 From: Alexis Lahouze Date: Sun, 16 Aug 2015 00:35:20 +0200 Subject: [PATCH] Use get method to retrieve account. --- accountant/api/views/accounts.py | 42 +++++++++++++++++--------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/accountant/api/views/accounts.py b/accountant/api/views/accounts.py index 2646201..12d7a0f 100644 --- a/accountant/api/views/accounts.py +++ b/accountant/api/views/accounts.py @@ -74,7 +74,15 @@ class AccountListResource(Resource): session.add(account) - return account, 201 + # Flush session to have id in account. + session.flush() + + # Return account with solds. + return Account.query( + session + ).filter( + Account.id == account.id + ).one(), 201 def delete(self): """ @@ -104,22 +112,16 @@ class AccountResource(Resource): @session_aware @marshal_with_field(Object(resource_fields)) def delete(self, account_id, session): - try: - account = Account.query( - session - ).filter( - Account.id == account_id - ).one() - except NoResultFound: + # Need to get the object to update it. + account = session.query(Account).get(account_id) + + if not account: return None, 404 session.delete(account) return account - def patch(self, id): - pass - @session_aware @marshal_with_field(Object(resource_fields)) def post(self, account_id, session): @@ -128,13 +130,10 @@ class AccountResource(Resource): assert (id not in kwargs or kwargs.id is None or kwargs.id == account_id) - try: - account = Account.query( - session - ).filter( - Account.id == account_id - ).one() - except NoResultFound: + # Need to get the object to update it. + account = session.query(Account).get(account_id) + + if not account: return None, 404 # SQLAlchemy objects ignore __dict__.update() with merge. @@ -143,7 +142,12 @@ class AccountResource(Resource): session.merge(account) - return account + # Return account with solds. + return Account.query( + session + ).filter( + Account.id == account_id + ).one() api_api.add_resource(AccountListResource, '/accounts')