move functions to new utils file
This commit is contained in:
parent
f7e19115f8
commit
e3e166e51e
2 changed files with 37 additions and 30 deletions
|
|
@ -1,21 +1,12 @@
|
||||||
from app import app, db
|
from app import app, db
|
||||||
from flask import render_template, redirect, url_for, request
|
from flask import render_template, redirect, url_for, request
|
||||||
from sqlalchemy import select, delete, update
|
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 datetime
|
||||||
import os
|
import os
|
||||||
import openpyxl
|
import openpyxl
|
||||||
import time
|
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')
|
@app.route('/budget_categories')
|
||||||
def budget_categories():
|
def budget_categories():
|
||||||
|
|
@ -33,29 +24,17 @@ def vendors():
|
||||||
|
|
||||||
@app.route('/')
|
@app.route('/')
|
||||||
def home():
|
def home():
|
||||||
# Get the current date
|
last_month_datetime = datetime.datetime(datetime.datetime.now().year,datetime.datetime.now().month, 1) - datetime.timedelta(days=1)
|
||||||
now = datetime.datetime.now()
|
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()
|
||||||
# Get the first day of the current month and subtract one day to get the last day of the previous month
|
for li in last_month_lines:
|
||||||
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:
|
|
||||||
dt_object = datetime.datetime.fromtimestamp(li[0].date)
|
dt_object = datetime.datetime.fromtimestamp(li[0].date)
|
||||||
date_string = dt_object.strftime('%Y-%m-%d')
|
date_string = dt_object.strftime('%Y-%m-%d')
|
||||||
li[0].date = date_string
|
li[0].date = date_string
|
||||||
files = os.listdir(get_uploads_path())
|
files = os.listdir(get_uploads_path())
|
||||||
return render_template('index.html',
|
return render_template('homepage.html',
|
||||||
files=files,
|
files=files,
|
||||||
all_line_items=all_line_items)
|
last_month_lines=last_month_lines)
|
||||||
|
|
||||||
@app.route('/upload_file/<filename>')
|
@app.route('/upload_file/<filename>')
|
||||||
def upload_file(filename:str):
|
def upload_file(filename:str):
|
||||||
|
|
@ -122,7 +101,7 @@ def add_budget_category():
|
||||||
bc = BudgetCategory(name=category_name)
|
bc = BudgetCategory(name=category_name)
|
||||||
db.session.add(bc)
|
db.session.add(bc)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
return redirect(url_for('home'))
|
return redirect(url_for('budget_categories'))
|
||||||
|
|
||||||
@app.route('/delete_budget_category/<id>', methods=['POST'])
|
@app.route('/delete_budget_category/<id>', methods=['POST'])
|
||||||
def delete_budget_category(id):
|
def delete_budget_category(id):
|
||||||
|
|
|
||||||
28
app/utils.py
Normal file
28
app/utils.py
Normal file
|
|
@ -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
|
||||||
Loading…
Add table
Add a link
Reference in a new issue