Esempio n. 1
0
def budgets():
    """Manage budgets"""

    # User reached route via GET
    if request.method == "GET":
        # Get the users income
        income = tendie_account.getIncome(session["user_id"])

        # Get the users current budgets
        budgets = tendie_budgets.getBudgets(session["user_id"])

        # Get the users total budgeted amount
        budgeted = tendie_budgets.getTotalBudgeted(session["user_id"])

        return render_template("budgets.html",
                               income=income,
                               budgets=budgets,
                               budgeted=budgeted,
                               deletedBudgetName=None)

    # User reached route via POST
    else:
        # Get the name of the budget the user wants to delete
        budgetName = request.form.get("delete").strip()

        # Delete the budget
        deletedBudgetName = tendie_budgets.deleteBudget(
            budgetName, session["user_id"])

        # Render the budgets page with a success message, otherwise throw an error/apology
        if deletedBudgetName:
            # Get the users income, current budgets, and sum their budgeted amount unless they don't have any budgets (same steps as a GET for this route)
            income = tendie_account.getIncome(session["user_id"])
            budgets = tendie_budgets.getBudgets(session["user_id"])
            budgeted = tendie_budgets.getTotalBudgeted(session["user_id"])

            return render_template("budgets.html",
                                   income=income,
                                   budgets=budgets,
                                   budgeted=budgeted,
                                   deletedBudgetName=deletedBudgetName)
        else:
            return apology("Uh oh! Your budget could not be deleted.")
Esempio n. 2
0
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())

        # 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(budget["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.getTotalBudgeted(session["user_id"])

        # 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)
Esempio n. 3
0
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)
        if tendie_budgets.getBudgets(session["user_id"]) is not None:
            budgetCount = len(tendie_budgets.getBudgets(session["user_id"]))
            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())

        # 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(budget["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.getTotalBudgeted(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)