예제 #1
0
파일: server.py 프로젝트: leemoh/Spent
def add_expenditure():
    """ Add new expenditure to the database """

    # Set the value of the user id of the user in the session
    id = session.get('id')

    # Get values from the form
    category_id = int(request.form.get("category"))
    price = request.form.get("price")
    date_of_expenditure = request.form.get("date")
    where_bought = request.form.get("wherebought")
    description = request.form.get("description")
    tracking_num = request.form.get("tracking-num")
    tracking_num_carrier = request.form.get("tracking-num-carrier")

    start_date, end_date = get_dates_for_budget(category_id, id)

    # Create a new expenditure object to insert into the expenditures table
    new_expenditure = Expenditure(category_id=category_id,
                                  price=price,
                                  date_of_expenditure=date_of_expenditure,
                                  where_bought=where_bought,
                                  description=description,
                                  expenditure_userid=id,
                                  tracking_num=tracking_num,
                                  tracking_num_carrier=tracking_num_carrier)

    # Insert the new expenditure into the expenditures table and commit the insert
    db.session.add(new_expenditure)
    db.session.commit()

    # Unpacking the function call
    total_cat_price, avg_cat_expenditures = expenditure_function(category_id, id, start_date, end_date)

    budget_minus_expenses = budget_totals(category_id, id, total_cat_price)
    cat_budget = get_budget_per_category(category_id, id)
    category_progress = get_progress(budget_minus_expenses, cat_budget)

    expenditure_info = {
        'total_cat_price': total_cat_price,
        'avg_cat_expenditures': avg_cat_expenditures,
        'category_id': category_id,
        'expenditure_id': new_expenditure.id,
        'date_of_expenditure': new_expenditure.date_of_expenditure.strftime('%Y-%m-%d'),
        'where_bought': new_expenditure.where_bought,
        'description': new_expenditure.description,
        'price': str(new_expenditure.price),
        'category': new_expenditure.category.category,
        'tracking_num': new_expenditure.tracking_num,
        'tracking_num_carrier': new_expenditure.tracking_num_carrier,
        'cat_budget_minus_expenses': budget_minus_expenses,
        'category_progress': category_progress
    }

    # Return jsonified info to submit-expenditure.js
    return jsonify(expenditure_info)
예제 #2
0
파일: server.py 프로젝트: ysjin94/Spent
def add_budget():
    """ Add a budget """

    # Set the value of the user id of the user in the session
    id = session.get('id')

    # Get values from the form
    budget = request.form.get("budget")
    category_id = int(request.form.get("category"))
    start_date = request.form.get("start-date")
    end_date = request.form.get("end-date")

    user_budget_query = Budget.query.filter_by(budget_userid=id).all()

    # Check for budgets in the database under the user ID in particular categories;
    # delete budgets that exist to override them
    # Check to see if you can modify it instead
    for query in user_budget_query:
        if query.category_id == category_id:
            db.session.delete(query)
            db.session.commit()

    # Add the budget to the database. It will be the only budget for that
    # category in the database for the user
    new_budget = Budget(budget=budget,
                        category_id=category_id,
                        budget_userid=id,
                        budget_start_date=start_date,
                        budget_end_date=end_date)

    # Insert the new budget into the budget table and commit the insert
    db.session.add(new_budget)
    db.session.commit()

    # Call functions in tools.py
    total_cat_price, avg_cat_expenditures = expenditure_function(
        category_id, id, start_date, end_date)
    cat_budget_minus_expenses = budget_totals(category_id, id, total_cat_price)

    # Call get_progress in tools.py to calculate the progress bar totals
    category_progress = get_progress(cat_budget_minus_expenses, budget)

    budget_info = {
        'id': new_budget.id,
        'category': new_budget.category.category,
        'category_id': category_id,
        'budget': budget,
        'cat_budget_minus_expenses': cat_budget_minus_expenses,
        'category_progress': category_progress
    }

    # Return jsonified budget info to submit-budget.js
    return jsonify(budget_info)
예제 #3
0
파일: server.py 프로젝트: leemoh/Spent
def dashboard(id):
    """ This is the user dashboard """

    # If the user id is in the session, this will render the dashboard
    # template, which will display their information and expenditure information
    if 'id' in session:

        # This is the user object
        user = User.query.filter_by(id=id).first()

        ### GENERATE THE USER HASH ###

        APP_ID = os.getenv('APP_ID')
        KEY = os.getenv('SECURE_MODE_KEY')
        MESSAGE = str(user.id)
        hash_result = hmac.new(KEY, MESSAGE, hashlib.sha256).hexdigest() 

        ####### GET THE USER'S BUDGETS FOR EACH CATEGORY

        cat_1_budget = get_budget_per_category(1, id)
        cat_2_budget = get_budget_per_category(2, id)
        cat_3_budget = get_budget_per_category(3, id)
        cat_4_budget = get_budget_per_category(4, id)
        cat_5_budget = get_budget_per_category(5, id)
        cat_6_budget = get_budget_per_category(6, id)

        # This is the expenditure object, which contains information about
        # expenditures specific to the user from the expenditure table in the
        # database
        expenditures = Expenditure.query.filter_by(expenditure_userid=id).all()

        ########### GET BUDGET START AND END DATES ###########

        # Calls the get_dates_for_budget function in tools.py
        cat_3_start, cat_3_end = get_dates_for_budget(3, id)
        cat_1_start, cat_1_end = get_dates_for_budget(1, id)
        cat_2_start, cat_2_end = get_dates_for_budget(2, id)
        cat_4_start, cat_4_end = get_dates_for_budget(4, id)
        cat_5_start, cat_5_end = get_dates_for_budget(5, id)
        cat_6_start, cat_6_end = get_dates_for_budget(6, id)

        # Strips datetime objects to year, month, day
        cat_3_start_date = cat_3_start.strftime('%m-%d-%Y')
        cat_1_start_date = cat_1_start.strftime('%m-%d-%Y')
        cat_2_start_date = cat_2_start.strftime('%m-%d-%Y')
        cat_4_start_date = cat_4_start.strftime('%m-%d-%Y')
        cat_5_start_date = cat_5_start.strftime('%m-%d-%Y')
        cat_6_start_date = cat_6_start.strftime('%m-%d-%Y')

        cat_3_end_date = cat_3_end.strftime('%m-%d-%Y')
        cat_1_end_date = cat_1_end.strftime('%m-%d-%Y')
        cat_2_end_date = cat_2_end.strftime('%m-%d-%Y')
        cat_4_end_date = cat_4_end.strftime('%m-%d-%Y')
        cat_5_end_date = cat_5_end.strftime('%m-%d-%Y')
        cat_6_end_date = cat_6_end.strftime('%m-%d-%Y')

        ########### TOTAL PRICE AND AVERAGE SPENT ###########

        # Unpacking the total price and average spent
        total_food_price, avg_food_expenditures = expenditure_function(3, id, cat_3_start_date, cat_3_end_date)
        total_groceries_price, avg_groceries_expenditures = expenditure_function(4, id, cat_4_start_date, cat_4_end_date)
        total_clothing_price, avg_clothing_expenditures = expenditure_function(5, id, cat_5_start_date, cat_5_end_date)
        total_entertainment_price, avg_entertainment_expenditures = expenditure_function(6, id, cat_6_start_date, cat_6_end_date)
        total_travel_price, avg_travel_expenditures = expenditure_function(2, id, cat_2_start_date, cat_2_end_date)
        total_online_purchase_price, avg_online_expenditures = expenditure_function(1, id, cat_1_start_date, cat_1_end_date)

        total_price = (total_food_price + total_groceries_price + total_clothing_price +
                       total_entertainment_price + total_travel_price +
                       total_online_purchase_price)

        ########### BUDGET ###########

        # Calling the function for each of the expenditure categories
        food_budget_minus_expenses = budget_totals(3, id, total_food_price)
        online_budget_minus_expenses = budget_totals(1, id, total_online_purchase_price)
        groceries_budget_minus_expenses = budget_totals(4, id, total_groceries_price)
        clothing_budget_minus_expenses = budget_totals(5, id, total_clothing_price)
        travel_budget_minus_expenses = budget_totals(2, id, total_travel_price)
        entertainment_budget_minus_expenses = budget_totals(6, id, total_entertainment_price)

        ############# PROGRESS BAR ##############

        # Call get_progress in tools.py to calculate the progress bar totals
        clothing_progress = get_progress(clothing_budget_minus_expenses, cat_5_budget)
        online_progress = get_progress(online_budget_minus_expenses, cat_1_budget)
        food_progress = get_progress(food_budget_minus_expenses, cat_3_budget)
        groceries_progress = get_progress(groceries_budget_minus_expenses, cat_4_budget)
        entertainment_progress = get_progress(entertainment_budget_minus_expenses, cat_6_budget)
        travel_progress = get_progress(travel_budget_minus_expenses, cat_2_budget)

        # Renders the dashboard, which displays the following info
        return render_template("dashboard.html",
                                                name=user.name,
                                                password=user.password,
                                                email=user.email,
                                                expenditures=expenditures,
                                                id=id,
                                                total_food_price=total_food_price,
                                                total_travel_price=total_travel_price,
                                                total_clothing_price=total_clothing_price,
                                                total_entertainment_price=total_entertainment_price,
                                                total_online_purchase_price=total_online_purchase_price,
                                                total_groceries_price=total_groceries_price,
                                                avg_online_expenditures=avg_online_expenditures,
                                                avg_entertainment_expenditures=avg_entertainment_expenditures,
                                                avg_clothing_expenditures=avg_clothing_expenditures,
                                                avg_travel_expenditures=avg_travel_expenditures,
                                                avg_groceries_expenditures=avg_groceries_expenditures,
                                                avg_food_expenditures=avg_food_expenditures,
                                                clothing_budget_minus_expenses=clothing_budget_minus_expenses,
                                                travel_budget_minus_expenses=travel_budget_minus_expenses,
                                                groceries_budget_minus_expenses=groceries_budget_minus_expenses,
                                                food_budget_minus_expenses=food_budget_minus_expenses,
                                                online_budget_minus_expenses=online_budget_minus_expenses,
                                                entertainment_budget_minus_expenses=entertainment_budget_minus_expenses,
                                                cat_1_budget=cat_1_budget,
                                                cat_2_budget=cat_2_budget,
                                                cat_3_budget=cat_3_budget,
                                                cat_4_budget=cat_4_budget,
                                                cat_5_budget=cat_5_budget,
                                                cat_6_budget=cat_6_budget,
                                                cat_1_start_date=cat_1_start_date,
                                                cat_2_start_date=cat_2_start_date,
                                                cat_3_start_date=cat_3_start_date,
                                                cat_4_start_date=cat_4_start_date,
                                                cat_5_start_date=cat_5_start_date,
                                                cat_6_start_date=cat_6_start_date,
                                                cat_1_end_date=cat_1_end_date,
                                                cat_2_end_date=cat_2_end_date,
                                                cat_3_end_date=cat_3_end_date,
                                                cat_4_end_date=cat_4_end_date,
                                                cat_5_end_date=cat_5_end_date,
                                                cat_6_end_date=cat_6_end_date,
                                                clothing_progress=clothing_progress,
                                                entertainment_progress=entertainment_progress,
                                                online_progress=online_progress,
                                                food_progress=food_progress,
                                                groceries_progress=groceries_progress,
                                                travel_progress=travel_progress,
                                                total_price=total_price,
                                                user_hash=hash_result,
                                                app_id=APP_ID)