From 6441644adc88fa41881152fdae70580db8326381 Mon Sep 17 00:00:00 2001 From: Alexis Lahouze Date: Wed, 15 Jul 2015 18:38:50 +0200 Subject: [PATCH] Code factorisation. --- accountant/api/models/scheduled_operations.py | 17 ++---------- accountant/api/views/scheduled_operations.py | 26 ++++++++++++++----- 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/accountant/api/models/scheduled_operations.py b/accountant/api/models/scheduled_operations.py index 6109e16..2dfa700 100644 --- a/accountant/api/models/scheduled_operations.py +++ b/accountant/api/models/scheduled_operations.py @@ -48,24 +48,11 @@ class ScheduledOperation(db.Model): self.category = category @classmethod - def get_scheduled_operations_for_account(cls, session, account): - if isinstance(account, int) or isinstance(account, str): - account_id = account - else: - account_id = account.id - - query = session.query( + def query(cls, session): + return session.query( cls - ).filter( - cls.account_id == account_id ).order_by( desc(cls.day), cls.value, cls.label, ) - - return query - - @classmethod - def get(cls, session, id): - return session.query(cls).filter(cls.id == id).one() diff --git a/accountant/api/views/scheduled_operations.py b/accountant/api/views/scheduled_operations.py index 4c288a1..28f3990 100644 --- a/accountant/api/views/scheduled_operations.py +++ b/accountant/api/views/scheduled_operations.py @@ -66,8 +66,10 @@ class ScheduledOperationListResource(Resource): """ kwargs = get_parser.parse_args() - return ScheduledOperation.get_scheduled_operations_for_account( - session, **kwargs + return ScheduledOperation.query( + session + ).filter( + ScheduledOperation.account_id == kwargs.account ).all() @session_aware @@ -93,7 +95,11 @@ class ScheduledOperationResource(Resource): Get scheduled operation. """ try: - return ScheduledOperation.get(session, scheduled_operation_id) + return ScheduledOperation.query( + session + ).filter( + ScheduledOperation.id == scheduled_operation_id + ).one() except NoResultFound: return None, 404 @@ -104,8 +110,11 @@ class ScheduledOperationResource(Resource): Delete a scheduled operation. """ try: - scheduled_operation = ScheduledOperation.get( - session, scheduled_operation_id) + scheduled_operation = ScheduledOperation.query( + session + ).filter( + ScheduledOperation.id == scheduled_operation_id + ).one() except NoResultFound: return None, 404 @@ -125,8 +134,11 @@ class ScheduledOperationResource(Resource): or kwargs.id == scheduled_operation_id) try: - scheduled_operation = ScheduledOperation.get( - session, scheduled_operation_id) + scheduled_operation = ScheduledOperation.query( + session + ).filter( + ScheduledOperation.id == scheduled_operation_id + ).one() except NoResultFound: return None, 404