From 7b49cec047372bf4b6bad35f009721c3bafc4a1e Mon Sep 17 00:00:00 2001 From: Alexis Lahouze Date: Sat, 13 Jun 2015 01:24:47 +0200 Subject: [PATCH] Add get with date range for operations. --- accountant/api/models/operations.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/accountant/api/models/operations.py b/accountant/api/models/operations.py index 143b2a2..d0dda60 100644 --- a/accountant/api/models/operations.py +++ b/accountant/api/models/operations.py @@ -14,12 +14,13 @@ You should have received a copy of the GNU Affero General Public License along with Accountant. If not, see . """ +import arrow + from sqlalchemy import func, case, desc from accountant import db - class Operation(db.Model): id = db.Column(db.Integer, primary_key=True) pointed = db.Column(db.Boolean, nullable=False, default=False) @@ -48,6 +49,14 @@ class Operation(db.Model): @classmethod def get_for_account_and_month(cls, session, account, year, month): + begin = arrow.get(year, month, 1) + end = begin.ceil('month') + + return cls.get_for_account_and_range(session, account, begin.date(), + end.date()) + + @classmethod + def get_for_account_and_range(cls, session, account, begin, end): if isinstance(account, int) or isinstance(account, str): account_id = account else: @@ -68,8 +77,8 @@ class Operation(db.Model): ).subquery() query = session.query(base_query).select_from(base_query) - query = query.filter(func.date_trunc( - 'month', base_query.c.operation_date) == "%s-%s-01" % (year, month)) + query = query.filter(base_query.c.operation_date >= str(begin), + base_query.c.operation_date <= str(end)) return query