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:
parent
0c0b7a9503
commit
2b7057c862
1 changed files with 57 additions and 11 deletions
|
|
@ -1,8 +1,8 @@
|
||||||
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, jsonify
|
||||||
from sqlalchemy import select, delete, update
|
from sqlalchemy import select, delete, update
|
||||||
from .models import LineItem, BudgetCategory, Vendor
|
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 datetime
|
||||||
import os
|
import os
|
||||||
import openpyxl
|
import openpyxl
|
||||||
|
|
@ -24,17 +24,11 @@ def vendors():
|
||||||
|
|
||||||
@app.route('/')
|
@app.route('/')
|
||||||
def home():
|
def home():
|
||||||
last_month_datetime = datetime.datetime(datetime.datetime.now().year,datetime.datetime.now().month, 1) - datetime.timedelta(days=1)
|
all_months = get_all_months()
|
||||||
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())
|
files = os.listdir(get_uploads_path())
|
||||||
return render_template('homepage.html',
|
return render_template('homepage.html',
|
||||||
files=files,
|
files=files,
|
||||||
last_month_lines=last_month_lines)
|
all_months=all_months)
|
||||||
|
|
||||||
@app.route('/upload_file/<filename>')
|
@app.route('/upload_file/<filename>')
|
||||||
def upload_file(filename:str):
|
def upload_file(filename:str):
|
||||||
|
|
@ -93,6 +87,13 @@ def view_month(year, month):
|
||||||
month=month,
|
month=month,
|
||||||
year=year)
|
year=year)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
###################
|
||||||
|
# API calls below #
|
||||||
|
###################
|
||||||
|
|
||||||
|
|
||||||
@app.route('/add_budget_category', methods=['POST'])
|
@app.route('/add_budget_category', methods=['POST'])
|
||||||
def add_budget_category():
|
def add_budget_category():
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
|
|
@ -102,10 +103,55 @@ def add_budget_category():
|
||||||
db.session.add(bc)
|
db.session.add(bc)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
return redirect(url_for('budget_categories'))
|
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'])
|
@app.route('/delete_budget_category/<id>', methods=['POST'])
|
||||||
def delete_budget_category(id):
|
def delete_budget_category(id):
|
||||||
db.session.execute(update(Vendor).where(Vendor.bc_id==id).values(bc_id=None))
|
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.execute(delete(BudgetCategory).where(BudgetCategory.id==id))
|
||||||
db.session.commit()
|
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)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue