def load_expenditures(): """ Load expenditures from expenditures.csv into database """ Expenditure.query.delete() with open(expenditure_file) as f: for _ in range(1): next(f) for row in f: row = row.rstrip() expenditure_data = row.split(",") print(expenditure_data) id = expenditure_data[0] category_id = expenditure_data[1] price = expenditure_data[2] date_of_expenditure = expenditure_data[3] expenditure_userid = expenditure_data[4] where_bought = expenditure_data[5] description = expenditure_data[6] expenditure = Expenditure( id=id, category_id=category_id, price=price, date_of_expenditure=get_datetime(date_of_expenditure), expenditure_userid=expenditure_userid, where_bought=where_bought, description=description) db.session.add(expenditure) db.session.commit()
def add_expenditure(jwt): try: new_expense = Expenditure(**request.json) except TypeError: abort(422) new_expense.insert() return jsonify({ "success": True, "expenditure": new_expense.dict_form(), })
def add_expenditure(): """ Add new expenditure to the database """ from models import Budget, Expenditure, User, Category from utils import expenditure_total_amount_and_avg, budget_totals, get_dates_for_budget, get_progress, get_budget_per_category, connect_to_db # 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") 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 ) # 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_total_amount_and_avg(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, 'cat_budget_minus_expenses': budget_minus_expenses, 'category_progress': category_progress } return jsonify(expenditure_info)