def addexpenses(): """Add new expense(s)""" # User reached route via POST if request.method == "POST": # Get all of the expenses provided from the HTML form formData = list(request.form.items()) # Add expenses to the DB for user expenses = tendie_expenses.addExpenses(formData, session["user_id"]) # Redirect to results page and render a summary of the submitted expenses return render_template("expensed.html", results=expenses) # User reached route via GET else: # Get the users spend categories categories = tendie_categories.getSpendCategories(session["user_id"]) # Get the users payers payers = tendie_account.getPayers(session["user_id"]) # Render expense page date = datetime.today().strftime('%Y-%m-%d') return render_template("addexpenses.html", categories=categories, date=date, payers=payers)
def expensehistory(): """Show history of expenses or let the user update existing expense""" # User reached route via GET if request.method == "GET": # Get all of the users expense history ordered by submission time history = tendie_expenses.getHistory(session["user_id"]) # Get the users spend categories categories = tendie_categories.getSpendCategories(session["user_id"]) # Get the users payers (for modal) payers = tendie_account.getPayers(session["user_id"]) return render_template("expensehistory.html", history=history, categories=categories, payers=payers, isDeleteAlert=False) # User reached route via POST else: # Initialize users action userHasSelected_deleteExpense = False # Determine what action was selected by the user (button/form trick from: https://stackoverflow.com/questions/26217779/how-to-get-the-name-of-a-submitted-form-in-flask) if "btnDeleteConfirm" in request.form: userHasSelected_deleteExpense = True elif "btnSave" in request.form: userHasSelected_deleteExpense = False else: return apology("Doh! Spend Categories is drunk. Try again!") # Get the existing expense record ID from the DB and build a data structure to store old expense details oldExpense = tendie_expenses.getExpense(request.form, session["user_id"]) # Make sure an existing record was found otherwise render an error message if oldExpense["id"] == None: return apology( "The expense record you're trying to update doesn't exist") # Delete the existing expense record if userHasSelected_deleteExpense == True: # Delete the old record from the DB deleted = tendie_expenses.deleteExpense(oldExpense, session["user_id"]) if not deleted: return apology("The expense was unable to be deleted") # Get the users expense history, spend categories, payers, and then render the history page w/ delete alert history = tendie_expenses.getHistory(session["user_id"]) categories = tendie_categories.getSpendCategories( session["user_id"]) payers = tendie_account.getPayers(session["user_id"]) return render_template("expensehistory.html", history=history, categories=categories, payers=payers, isDeleteAlert=True) # Update the existing expense record else: # Update the old record with new details from the form expensed = tendie_expenses.updateExpense(oldExpense, request.form, session["user_id"]) if not expensed: return apology("The expense was unable to be updated") # Redirect to results page and render a summary of the updated expense return render_template("expensed.html", results=expensed)
def index(): """Show dashboard of budget/expenses""" # User reached route via GET if request.method == "GET": # TODO reduce or completely remove the redundant use of javascript code in dashboard.js and reports.js # Initialize metrics to None to render the appropriate UX if data does not exist yet for the user expenses_year = None expenses_month = None expenses_week = None expenses_last5 = None spending_week = [] spending_month = [] # Get the users spend categories (for quick expense modal) categories = tendie_categories.getSpendCategories(session["user_id"]) # Get the users payers (for quick expense modal) payers = tendie_account.getPayers(session["user_id"]) # Get todays date (for quick expense modal) date = datetime.today().strftime('%Y-%m-%d') # Get the users income income = tendie_account.getIncome(session["user_id"]) # Get current years total expenses for the user expenses_year = tendie_dashboard.getTotalSpend_Year(session["user_id"]) # Get current months total expenses for the user expenses_month = tendie_dashboard.getTotalSpend_Month( session["user_id"]) # Get current week total expenses for the user expenses_week = tendie_dashboard.getTotalSpend_Week(session["user_id"]) # Get last 5 expenses for the user expenses_last5 = tendie_dashboard.getLastFiveExpenses( session["user_id"]) # Get every budgets spent/remaining for the user budgets = tendie_dashboard.getBudgets(session["user_id"]) # Get weekly spending for the user weeks = tendie_dashboard.getLastFourWeekNames() spending_week = tendie_dashboard.getWeeklySpending( weeks, session["user_id"]) # Get monthly spending for the user (for the current year) spending_month = tendie_dashboard.getMonthlySpending( session["user_id"]) # Get spending trends for the user spending_trends = tendie_dashboard.getSpendingTrends( session["user_id"]) # Get payer spending for the user payersChart = tendie_reports.generatePayersReport(session["user_id"]) return render_template("index.html", categories=categories, payers=payers, date=date, income=income, expenses_year=expenses_year, expenses_month=expenses_month, expenses_week=expenses_week, expenses_last5=expenses_last5, budgets=budgets, spending_week=spending_week, spending_month=spending_month, spending_trends=spending_trends, payersChart=payersChart) # User reached route via POST else: # Get all of the expenses provided from the HTML form formData = list(request.form.items()) # Add expenses to the DB for user expenses = tendie_expenses.addExpenses(formData, session["user_id"]) # Redirect to results page and render a summary of the submitted expenses return render_template("expensed.html", results=expenses)