From e3e166e51ee74571cd17b0cdc72d7e85c0269907 Mon Sep 17 00:00:00 2001 From: ydb5755 Date: Mon, 7 Oct 2024 15:25:30 +0300 Subject: [PATCH] move functions to new utils file --- app/routes.py | 39 +++++++++------------------------------ app/utils.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 30 deletions(-) create mode 100644 app/utils.py diff --git a/app/routes.py b/app/routes.py index f352ac2..184ec49 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,21 +1,12 @@ from app import app, db from flask import render_template, redirect, url_for, request from sqlalchemy import select, delete, update -from app.models import LineItem, BudgetCategory, Vendor +from .models import LineItem, BudgetCategory, Vendor +from .utils import get_uploads_path, get_month_timestamps import datetime import os import openpyxl import time -import calendar - -def get_uploads_path() -> str: - path1 = 'C:/Users/Lenovo/Desktop/BudgetingApp/app/static/uploadable/' - path2 = '/home/yisroel2/Desktop/budgetingApp/app/static/uploadable/' - for p in [path1, path2]: - if os.path.exists(p): - path = p - break - return path @app.route('/budget_categories') def budget_categories(): @@ -33,29 +24,17 @@ def vendors(): @app.route('/') def home(): - # Get the current date - now = datetime.datetime.now() - - # Get the first day of the current month and subtract one day to get the last day of the previous month - first_day_of_current_month = datetime.datetime(now.year, now.month, 1) - last_day_of_previous_month = first_day_of_current_month - datetime.timedelta(days=1) - - # Get the first day of the previous month - first_day_of_previous_month = datetime.datetime(last_day_of_previous_month.year, last_day_of_previous_month.month, 1) - - # Convert both datetime objects to timestamps - first_day_timestamp = first_day_of_previous_month.timestamp() - last_day_timestamp = datetime.datetime(last_day_of_previous_month.year, last_day_of_previous_month.month, last_day_of_previous_month.day, 23, 59, 59).timestamp() - - all_line_items = db.session.execute(select(LineItem).where(LineItem.date > first_day_timestamp, LineItem.date < last_day_timestamp)).all() - for li in all_line_items: + last_month_datetime = datetime.datetime(datetime.datetime.now().year,datetime.datetime.now().month, 1) - datetime.timedelta(days=1) + first_day_timestamp, last_day_timestamp = get_month_timestamps(last_month_datetime.month, last_month_datetime.year) + last_month_lines = db.session.execute(select(LineItem).where(LineItem.date > first_day_timestamp, LineItem.date < last_day_timestamp)).all() + for li in last_month_lines: dt_object = datetime.datetime.fromtimestamp(li[0].date) date_string = dt_object.strftime('%Y-%m-%d') li[0].date = date_string files = os.listdir(get_uploads_path()) - return render_template('index.html', + return render_template('homepage.html', files=files, - all_line_items=all_line_items) + last_month_lines=last_month_lines) @app.route('/upload_file/') def upload_file(filename:str): @@ -122,7 +101,7 @@ def add_budget_category(): bc = BudgetCategory(name=category_name) db.session.add(bc) db.session.commit() - return redirect(url_for('home')) + return redirect(url_for('budget_categories')) @app.route('/delete_budget_category/', methods=['POST']) def delete_budget_category(id): diff --git a/app/utils.py b/app/utils.py new file mode 100644 index 0000000..2bf114d --- /dev/null +++ b/app/utils.py @@ -0,0 +1,28 @@ +import os +import datetime +import calendar + +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) + print(calendar.monthrange(year, month)[1]) + + + # # Get the first day of the previous month + # first_day_of_previous_month = datetime.datetime(last_day_of_requested_month.year, last_day_of_requested_month.month, 1) + + # Convert both datetime objects to timestamps + first_day_timestamp = first_day_of_requested_month.timestamp() + last_day_timestamp = datetime.datetime(last_day_of_requested_month.year, last_day_of_requested_month.month, last_day_of_requested_month.day, 23, 59, 59).timestamp() + return first_day_timestamp, last_day_timestamp + +def get_uploads_path() -> str: + path1 = 'C:/Users/Lenovo/Desktop/BudgetingApp/app/static/uploadable/' + path2 = '/home/yisroel2/Desktop/budgetingApp/app/static/uploadable/' + for p in [path1, path2]: + if os.path.exists(p): + path = p + break + return path \ No newline at end of file