From 0b0ca5704241225e938799a5e2850c3624654d1a Mon Sep 17 00:00:00 2001 From: Alexis Lahouze Date: Wed, 15 Jul 2015 18:28:28 +0200 Subject: [PATCH] Code factorisation. --- accountant/api/models/accounts.py | 37 ++----------------------------- accountant/api/views/accounts.py | 28 ++++++++++++++--------- 2 files changed, 19 insertions(+), 46 deletions(-) diff --git a/accountant/api/models/accounts.py b/accountant/api/models/accounts.py index ae5806c..00a7780 100644 --- a/accountant/api/models/accounts.py +++ b/accountant/api/models/accounts.py @@ -32,36 +32,7 @@ class Account(db.Model): self.authorized_overdraft = authorized_overdraft @classmethod - def get_accounts(cls, session): - query = session.query( - cls.id.label("id"), - cls.name.label("name"), - cls.authorized_overdraft.label("authorized_overdraft"), - func.sum(Operation.value).label("future"), - func.sum( - case( - [(Operation.pointed, Operation.value)], - else_=0 - ) - ).label("pointed"), - func.sum( - case( - [(Operation.operation_date <= func.current_date(), - Operation.value)], - else_=0 - ) - ).label("current") - ).outerjoin(Operation).group_by(cls.id).order_by(cls.id) - - return query - - @classmethod - def get(cls, session, account, begin=None, end=None): - if isinstance(account, int) or isinstance(account, str): - account_id = account - else: - account_id = account.id - + def query(cls, session, begin=None, end=None): status_query = session.query( Operation.account_id, func.sum(Operation.value).label("future"), @@ -120,8 +91,6 @@ class Account(db.Model): status_query, status_query.c.account_id == cls.id ).outerjoin( balance_query, balance_query.c.account_id == cls.id - ).filter( - cls.id == account_id ) else: query = session.query( @@ -136,11 +105,9 @@ class Account(db.Model): literal_column("0").label('balance'), ).outerjoin( status_query, status_query.c.account_id == cls.id - ).filter( - cls.id == account_id ) - return query.one() + return query @validates('authorized_overdraft') def validate_authorized_overdraft(self, key, authorized_overdraft): diff --git a/accountant/api/views/accounts.py b/accountant/api/views/accounts.py index 70e2fb5..2646201 100644 --- a/accountant/api/views/accounts.py +++ b/accountant/api/views/accounts.py @@ -60,13 +60,7 @@ class AccountListResource(Resource): """ Returns accounts with their balances. """ - return Account.get_accounts(session).all(), 200 - - def put(self): - """ - Batch update, not implemented. - """ - raise NotImplementedError() + return Account.query(session).all(), 200 @session_aware @marshal_with_field(Object(resource_fields)) @@ -99,7 +93,11 @@ class AccountResource(Resource): kwargs = date_parser.parse_args() try: - return Account.get(session, account_id, **kwargs) + return Account.query( + session, **kwargs + ).filter( + Account.id == account_id + ).one() except NoResultFound: return None, 404 @@ -107,7 +105,11 @@ class AccountResource(Resource): @marshal_with_field(Object(resource_fields)) def delete(self, account_id, session): try: - account = Account.get(session, account_id) + account = Account.query( + session + ).filter( + Account.id == account_id + ).one() except NoResultFound: return None, 404 @@ -120,14 +122,18 @@ class AccountResource(Resource): @session_aware @marshal_with_field(Object(resource_fields)) - def put(self, account_id, session): + def post(self, account_id, session): kwargs = parser.parse_args() assert (id not in kwargs or kwargs.id is None or kwargs.id == account_id) try: - account = Account.get(session, account_id) + account = Account.query( + session + ).filter( + Account.id == account_id + ).one() except NoResultFound: return None, 404