Code factorisation.

This commit is contained in:
Alexis Lahouze 2015-07-15 18:38:50 +02:00
parent 9efade745c
commit 6441644adc
2 changed files with 21 additions and 22 deletions

View File

@ -48,24 +48,11 @@ class ScheduledOperation(db.Model):
self.category = category self.category = category
@classmethod @classmethod
def get_scheduled_operations_for_account(cls, session, account): def query(cls, session):
if isinstance(account, int) or isinstance(account, str): return session.query(
account_id = account
else:
account_id = account.id
query = session.query(
cls cls
).filter(
cls.account_id == account_id
).order_by( ).order_by(
desc(cls.day), desc(cls.day),
cls.value, cls.value,
cls.label, cls.label,
) )
return query
@classmethod
def get(cls, session, id):
return session.query(cls).filter(cls.id == id).one()

View File

@ -66,8 +66,10 @@ class ScheduledOperationListResource(Resource):
""" """
kwargs = get_parser.parse_args() kwargs = get_parser.parse_args()
return ScheduledOperation.get_scheduled_operations_for_account( return ScheduledOperation.query(
session, **kwargs session
).filter(
ScheduledOperation.account_id == kwargs.account
).all() ).all()
@session_aware @session_aware
@ -93,7 +95,11 @@ class ScheduledOperationResource(Resource):
Get scheduled operation. Get scheduled operation.
""" """
try: try:
return ScheduledOperation.get(session, scheduled_operation_id) return ScheduledOperation.query(
session
).filter(
ScheduledOperation.id == scheduled_operation_id
).one()
except NoResultFound: except NoResultFound:
return None, 404 return None, 404
@ -104,8 +110,11 @@ class ScheduledOperationResource(Resource):
Delete a scheduled operation. Delete a scheduled operation.
""" """
try: try:
scheduled_operation = ScheduledOperation.get( scheduled_operation = ScheduledOperation.query(
session, scheduled_operation_id) session
).filter(
ScheduledOperation.id == scheduled_operation_id
).one()
except NoResultFound: except NoResultFound:
return None, 404 return None, 404
@ -125,8 +134,11 @@ class ScheduledOperationResource(Resource):
or kwargs.id == scheduled_operation_id) or kwargs.id == scheduled_operation_id)
try: try:
scheduled_operation = ScheduledOperation.get( scheduled_operation = ScheduledOperation.query(
session, scheduled_operation_id) session
).filter(
ScheduledOperation.id == scheduled_operation_id
).one()
except NoResultFound: except NoResultFound:
return None, 404 return None, 404