Add get with date range for operations.

This commit is contained in:
Alexis Lahouze 2015-06-13 01:24:47 +02:00
parent 16e5a6e91e
commit 7b49cec047

View File

@ -14,12 +14,13 @@
You should have received a copy of the GNU Affero General Public License You should have received a copy of the GNU Affero General Public License
along with Accountant. If not, see <http://www.gnu.org/licenses/>. along with Accountant. If not, see <http://www.gnu.org/licenses/>.
""" """
import arrow
from sqlalchemy import func, case, desc from sqlalchemy import func, case, desc
from accountant import db from accountant import db
class Operation(db.Model): class Operation(db.Model):
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
pointed = db.Column(db.Boolean, nullable=False, default=False) pointed = db.Column(db.Boolean, nullable=False, default=False)
@ -48,6 +49,14 @@ class Operation(db.Model):
@classmethod @classmethod
def get_for_account_and_month(cls, session, account, year, month): 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): if isinstance(account, int) or isinstance(account, str):
account_id = account account_id = account
else: else:
@ -68,8 +77,8 @@ class Operation(db.Model):
).subquery() ).subquery()
query = session.query(base_query).select_from(base_query) query = session.query(base_query).select_from(base_query)
query = query.filter(func.date_trunc( query = query.filter(base_query.c.operation_date >= str(begin),
'month', base_query.c.operation_date) == "%s-%s-01" % (year, month)) base_query.c.operation_date <= str(end))
return query return query