Code factorisation.
This commit is contained in:
parent
9efade745c
commit
6441644adc
@ -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()
|
||||
|
@ -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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user