def updatebudget(urlvar_budgetname): """Update a budget""" # User reached route via POST if request.method == "POST": # Get all of the budget info provided from the HTML form formData = list(request.form.items()) # Remove CSRF field from form data before processing formData.pop(0) # Generate data structure to hold budget info from form budgetDict = tendie_budgets.generateBudgetFromForm(formData) # Render error message if budget name or categories contained invalid data if "apology" in budgetDict: return apology(budgetDict["apology"]) else: # Update budget in the DB for user budget = tendie_budgets.updateBudget(urlvar_budgetname, budgetDict, session["user_id"]) # Render error message if budget name is a duplicate of another budget the user has if "apology" in budget: return apology(budget["apology"]) else: return render_template("budgetcreated.html", results=budget) # User reached route via GET else: # Get the budget details from the DB based on the budget name provided via URL. Throw an apology/error if budget can't be found. budgetID = tendie_budgets.getBudgetID(urlvar_budgetname, session["user_id"]) if budgetID is None: return apology("'" + urlvar_budgetname + "' budget does not exist") else: budget = tendie_budgets.getBudgetByID(budgetID, session["user_id"]) # Get the users income income = tendie_account.getIncome(session["user_id"]) # Get the users total budgeted amount budgeted = tendie_budgets.getTotalBudgetedByYear( session["user_id"], budget['year']) # Generate the full, updatable budget data structure (name, amount for budget w/ all categories and their budgeted amounts) budget = tendie_budgets.getUpdatableBudget(budget, session["user_id"]) # Render the budget update page return render_template("updatebudget.html", income=income, budgeted=budgeted, budget=budget)
def createbudget(): """Create a budget""" # User reached route via POST if request.method == "POST": # Make sure user has no more than 20 budgets (note: 20 is an arbitrary value) budgets = tendie_budgets.getBudgets(session["user_id"]) if budgets: budgetCount = 0 for year in budgets: budgetCount += len(budgets[year]) if budgetCount >= 20: return apology("You've reached the max amount of budgets'") # Get all of the budget info provided from the HTML form formData = list(request.form.items()) # Remove CSRF field from form data before processing formData.pop(0) # Generate data structure to hold budget info from form budgetDict = tendie_budgets.generateBudgetFromForm(formData) # Render error message if budget name or categories contained invalid data if "apology" in budgetDict: return apology(budgetDict["apology"]) else: # Add budget to DB for user budget = tendie_budgets.createBudget(budgetDict, session["user_id"]) # Render error message if budget name is a duplicate of another budget the user has if "apology" in budget: return apology(budget["apology"]) else: return render_template("budgetcreated.html", results=budget) else: # Get the users income income = tendie_account.getIncome(session["user_id"]) # Get the users total budgeted amount budgeted = tendie_budgets.getTotalBudgetedByYear(session["user_id"]) # Get the users spend categories categories = tendie_categories.getSpendCategories(session["user_id"]) return render_template("createbudget.html", income=income, budgeted=budgeted, categories=categories)