def manually_add_food(): """Add food to foods, ingredient to ingredients, and food log event to DB""" # add food to foods table new_food = Food.add_or_return_food( food_name=request.form.get('food_name'), brand_name=request.form.get('brand_name')) if not new_food: # add_or_return returns false if food exists flash('Food already exists within DB') return redirect('/manual_add') # add ingredients and link them to the food new_food.add_ingredients_and_links(request.form.get('ingredients')) # add meal to food log time_eaten = request.form.get('time_eaten') new_food_log = FoodLog(meal_id=request.form.get('meal_to_add'), food_id=new_food.id, user_id=session['user_id'], ts=time_eaten) db.session.add(new_food_log) db.session.commit() # redirect to the day for which the food log was added return redirect(f'/{time_eaten[:10]}')
def nutrionix_confirm(nix_id): """Confirms the addition of a nutrionix food search result to the DB""" print(debug) print('NIX_ID:', nix_id) print(debug) result = search_branded_item(nix_id) if not result['nf_ingredient_statement']: # flash('Unfortunately this record has no ingredient info 😢 please try another') return redirect('/add_food') # food_name = result['foods'] new_food = Food.add_or_return_food(food_name=result['food_name'], brand_name=result['brand_name']) if not new_food: # add_or_return returns false if food exists existing_food = Food.query.filter(Food.name == result['food_name'], Food.brand_name == result['brand_name']) \ .first() flash('Food already exists within DB') return redirect(f'/add_food/{existing_food.id}') # add ingredients and link them to the food new_food.add_ingredients_and_links(result['nf_ingredient_statement']) return redirect(f'/add_food/{new_food.id}')