Code factorisation.

This commit is contained in:
Alexis Lahouze 2015-07-15 18:28:28 +02:00
parent 53cdeabc90
commit 0b0ca57042
2 changed files with 19 additions and 46 deletions

View File

@ -32,36 +32,7 @@ class Account(db.Model):
self.authorized_overdraft = authorized_overdraft self.authorized_overdraft = authorized_overdraft
@classmethod @classmethod
def get_accounts(cls, session): def query(cls, session, begin=None, end=None):
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
status_query = session.query( status_query = session.query(
Operation.account_id, Operation.account_id,
func.sum(Operation.value).label("future"), func.sum(Operation.value).label("future"),
@ -120,8 +91,6 @@ class Account(db.Model):
status_query, status_query.c.account_id == cls.id status_query, status_query.c.account_id == cls.id
).outerjoin( ).outerjoin(
balance_query, balance_query.c.account_id == cls.id balance_query, balance_query.c.account_id == cls.id
).filter(
cls.id == account_id
) )
else: else:
query = session.query( query = session.query(
@ -136,11 +105,9 @@ class Account(db.Model):
literal_column("0").label('balance'), literal_column("0").label('balance'),
).outerjoin( ).outerjoin(
status_query, status_query.c.account_id == cls.id status_query, status_query.c.account_id == cls.id
).filter(
cls.id == account_id
) )
return query.one() return query
@validates('authorized_overdraft') @validates('authorized_overdraft')
def validate_authorized_overdraft(self, key, authorized_overdraft): def validate_authorized_overdraft(self, key, authorized_overdraft):

View File

@ -60,13 +60,7 @@ class AccountListResource(Resource):
""" """
Returns accounts with their balances. Returns accounts with their balances.
""" """
return Account.get_accounts(session).all(), 200 return Account.query(session).all(), 200
def put(self):
"""
Batch update, not implemented.
"""
raise NotImplementedError()
@session_aware @session_aware
@marshal_with_field(Object(resource_fields)) @marshal_with_field(Object(resource_fields))
@ -99,7 +93,11 @@ class AccountResource(Resource):
kwargs = date_parser.parse_args() kwargs = date_parser.parse_args()
try: try:
return Account.get(session, account_id, **kwargs) return Account.query(
session, **kwargs
).filter(
Account.id == account_id
).one()
except NoResultFound: except NoResultFound:
return None, 404 return None, 404
@ -107,7 +105,11 @@ class AccountResource(Resource):
@marshal_with_field(Object(resource_fields)) @marshal_with_field(Object(resource_fields))
def delete(self, account_id, session): def delete(self, account_id, session):
try: try:
account = Account.get(session, account_id) account = Account.query(
session
).filter(
Account.id == account_id
).one()
except NoResultFound: except NoResultFound:
return None, 404 return None, 404
@ -120,14 +122,18 @@ class AccountResource(Resource):
@session_aware @session_aware
@marshal_with_field(Object(resource_fields)) @marshal_with_field(Object(resource_fields))
def put(self, account_id, session): def post(self, account_id, session):
kwargs = parser.parse_args() kwargs = parser.parse_args()
assert (id not in kwargs or kwargs.id is None assert (id not in kwargs or kwargs.id is None
or kwargs.id == account_id) or kwargs.id == account_id)
try: try:
account = Account.get(session, account_id) account = Account.query(
session
).filter(
Account.id == account_id
).one()
except NoResultFound: except NoResultFound:
return None, 404 return None, 404