converted over to using a class in disp.py that defines data about each variable in the form, and use an array of these objects to format the form in templates/index.html -- provides consistency of bootstrap classes and makes it far easier to move items around columns/rows in the future, e.g. when I resign. Also, now retrieve actual values form comparison set data (still have hardcoded cset_id for now), but also stopped using separate vars for items like CBA/TLS, and buried them into the finance_data

This commit is contained in:
2025-02-12 17:25:23 +11:00
parent af38b45034
commit 65ed02812a
5 changed files with 171 additions and 173 deletions

51
db.py
View File

@@ -1,13 +1,15 @@
# db.py
import sqlite3
import pprint
def connect_db():
def connect_db(as_object):
conn = sqlite3.connect('finance.db')
conn.row_factory = sqlite3.Row # This allows us to access columns by name
if as_object:
conn.row_factory = sqlite3.Row # This allows us to access columns by name
return conn
def init_db():
conn = connect_db()
conn = connect_db(True)
cur = conn.cursor()
cur.execute('''CREATE TABLE IF NOT EXISTS finance (
id INTEGER PRIMARY KEY AUTOINCREMENT,
@@ -92,19 +94,19 @@ def init_db():
conn.close()
def get_finance_data():
conn = connect_db()
conn = connect_db(True)
cur = conn.cursor()
cur.execute('SELECT * FROM finance WHERE id = 1') # Assuming a single record for simplicity
finance = cur.fetchone()
conn.close()
return dict(finance)
def get_budget_data(finance_data, CBA, TLS):
def get_budget_data(finance_data):
# annual bills - health ins (5k), rates (2.4), electricity (1.5), gas (2), internet (1.6), car insurance (.7), rego (.8), house insurance (2.4), GFC (2.2), phones (.5), melb. pollen (.03), nabu casa (.1), eweka (.1) --- noting phone is elevated presuming I also go onto Aldi plan, but that there is no family discount
bills = 19330
BUDGET=[]
BUDGET.append( ('Bills', f"${bills:,.2f}") )
BUDGET.append( ('Buffer', f"${CBA*finance_data['CBA_price']+TLS*finance_data['TLS_price']:,.2f}") )
BUDGET.append( ('Buffer', f"${finance_data['CBA']*finance_data['CBA_price']+finance_data['TLS']*finance_data['TLS_price']:,.2f}") )
BUDGET.append( ('Monthly budget', f"${((finance_data['Living_Expenses']-12000) / 12):,.2f}" ) )
BUDGET.append( ('Weekly budget', f"${((finance_data['Living_Expenses']-12000) / 52):,.2f}" ) )
return BUDGET
@@ -166,11 +168,36 @@ def insert_cset( data ):
conn.close()
return cset_id
def last_cset_savings_data(cset_id):
conn = connect_db()
################################################################################
#
# gets comparison set data from the DB based on cset_id passed in (via drop-down in GUI).
# returns a DICT vars pointing to all the var values used for the ARRAY savings_data of saved fortnightly $'s
# also sets date / amount for the last set of $'s for easy display in the GUI
#
################################################################################
def get_comp_set_data(cset_id):
COMP={}
# get comp data from DB (as object so dict conversion works below)
conn = connect_db(True)
cur = conn.cursor()
cur.execute('''select name, value from comparison_savings_data
where name = ( select max(name) from comparison_savings_data ) ''' )
name, value = cur.fetchone() # fetchone() fetches a single row
# HARDCODED FOR NOW
cset_id = 1
# get saved finance data for this comparison set
cur.execute( f"select * from comparison_set where id = {cset_id}" )
COMP['vars']= dict(cur.fetchone())
conn.close()
return name, value
# open new connection so we get rows back as basic array
conn = connect_db(False)
cur = conn.cursor()
# get data for this comparison set
cur.execute( f"select name, value from comparison_savings_data where comparison_set_id = {cset_id}" )
COMP['savings_data']=cur.fetchall()
# do this for convenience in printing single last cset data point
COMP['date'], COMP['amount'] = COMP['savings_data'][-1]
return COMP