moved funcitons to api calls, add vendor add/delete function routes, added api route to get line items by month

This commit is contained in:
Yisroel Baum 2024-10-09 14:39:21 +03:00
parent 0c0b7a9503
commit 2b7057c862

View file

@ -1,8 +1,8 @@
from app import app, db
from flask import render_template, redirect, url_for, request
from flask import render_template, redirect, url_for, request, jsonify
from sqlalchemy import select, delete, update
from .models import LineItem, BudgetCategory, Vendor
from .utils import get_uploads_path, get_month_timestamps
from .utils import get_uploads_path, get_month_timestamps, get_all_months
import datetime
import os
import openpyxl
@ -24,17 +24,11 @@ def vendors():
@app.route('/')
def home():
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
all_months = get_all_months()
files = os.listdir(get_uploads_path())
return render_template('homepage.html',
files=files,
last_month_lines=last_month_lines)
all_months=all_months)
@app.route('/upload_file/<filename>')
def upload_file(filename:str):
@ -93,6 +87,13 @@ def view_month(year, month):
month=month,
year=year)
###################
# API calls below #
###################
@app.route('/add_budget_category', methods=['POST'])
def add_budget_category():
if request.method == 'POST':
@ -102,10 +103,55 @@ def add_budget_category():
db.session.add(bc)
db.session.commit()
return redirect(url_for('budget_categories'))
@app.route('/add_vendor', methods=['POST'])
def add_vendor():
if request.method == 'POST':
vendor_name = request.form['vendor_name']
vendor = Vendor(name=vendor_name)
db.session.add(vendor)
db.session.commit()
return redirect(url_for('vendors'))
@app.route('/delete_budget_category/<id>', methods=['POST'])
def delete_budget_category(id):
db.session.execute(update(Vendor).where(Vendor.bc_id==id).values(bc_id=None))
db.session.execute(delete(BudgetCategory).where(BudgetCategory.id==id))
db.session.commit()
return {"status":'success'}
return {"status":'success'}
@app.route('/delete_vendor/<id>', methods=['POST'])
def delete_vendor(id):
db.session.execute(delete(Vendor).where(Vendor.id==id))
db.session.commit()
return {"status":'success'}
@app.route('/get_month_line_items/<month>/<year>', methods=['POST'])
def get_month_line_items(month:str, year:str):
year = int(year)
month_int = datetime.datetime.strptime(month, "%b").month
first_day_timestamp, last_day_timestamp = get_month_timestamps(month_int, year)
month_line_items = db.session.execute(select(LineItem).where(LineItem.date > first_day_timestamp, LineItem.date < last_day_timestamp)).all()
line_item_list = []
for li in month_line_items:
li = li[0]
dt_object = datetime.datetime.fromtimestamp(li.date)
date_string = dt_object.strftime('%Y-%m-%d')
line_item_data = {
'id':li.id,
'parent_line_item_id':li.parent_line_item_id,
'amount':li.amount,
'currency_type':li.currency_type,
'vendor':li.get_vendor()[0].name,
'date':date_string,
'confirmation_code':li.confirmation_code,
'note':li.note
}
line_item_list.append(line_item_data)
return jsonify(line_item_list)