create function to get all months of line items in string form

This commit is contained in:
Yisroel Baum 2024-10-09 14:37:51 +03:00
parent 4afe06f67c
commit 0c0b7a9503

View file

@ -1,9 +1,12 @@
import os import os
import datetime import datetime
import calendar 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]: 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 # 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) 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) 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): if os.path.exists(p):
path = p path = p
break break
return path 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]