From 0c0b7a95032554c7a045de0ebf38cef5f05a34dd Mon Sep 17 00:00:00 2001 From: ydb5755 Date: Wed, 9 Oct 2024 14:37:51 +0300 Subject: [PATCH] create function to get all months of line items in string form --- app/utils.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/app/utils.py b/app/utils.py index 056efce..a2ed061 100644 --- a/app/utils.py +++ b/app/utils.py @@ -1,9 +1,12 @@ import os import datetime import calendar +from app import db +from sqlalchemy import select, delete, update, distinct +from .models import LineItem, BudgetCategory, Vendor + def get_month_timestamps(month:int, year:int) -> tuple[float, float]: - print(month, year) # Get the first day of the requested month and subtract one day to get the last day of the previous month first_day_of_requested_month = datetime.datetime(year, month, 1) last_day_of_requested_month = first_day_of_requested_month + datetime.timedelta(days=calendar.monthrange(year, month)[1] - 1) @@ -20,4 +23,13 @@ def get_uploads_path() -> str: if os.path.exists(p): path = p break - return path \ No newline at end of file + return path + +def get_all_months(): + lis = [datetime.datetime.fromtimestamp(int(s)) for s in db.session.execute(select(distinct(LineItem.date))).scalars().all()] + month_year = {(s.month, s.year) for s in lis} + + month_year = [datetime.datetime(year=x[1], month=x[0], day=1) for x in month_year] + month_year.sort(reverse=True) + return [x.strftime("%b %Y") for x in month_year] + \ No newline at end of file