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
@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()

View File

@ -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