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
along with Accountant. If not, see <http://www.gnu.org/licenses/>.
"""
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