def add_ingredient_from_API(recipe_id): """Shows data from API and allows users to select their ingredients and post it to the database""" if request.method == 'POST': name_string = request.form.get('name') name = name_string.replace("/", "") calories_string = request.form.get('calories') calories = int(float(calories_string.replace("/", ""))) recipe = crud.get_recipe_by_id(recipe_id) measurement = request.form.get('measurement') photo = request.form.get('photo') ingredient = crud.create_ingredient(name, calories, measurement, photo) crud.create_recipe_ingredient(recipe, ingredient) return redirect(f'/recipe/{recipe_id}') else: if 'user_id' not in session: return redirect('/login') search = request.args.get( 'name') #must use request.args.get for GET requests app_key = os.environ['app_key'] app_id = os.environ['app_id'] payload = {'ingr': search, 'app_id': app_id, 'app_key': app_key} url = 'https://api.edamam.com/api/food-database/v2/parser' res = requests.get(url, params=payload) #interacting directly with API data = res.json() ingredient = data['hints'] return render_template('all_ingredients.html', ingredients=ingredient, recipe_id=recipe_id)
def add_recipe_empty(user_cleanse_id): """Add a smoothie/recipe to a cleanse that does not have any smoothies/recipes""" if request.method == 'POST': timestamp = datetime.now() date = datetime.now() user_cleanse = crud.get_user_cleanse(user_cleanse_id) recipe_name = request.form.get('recipe_name') ingredient_name = request.form.get('ingredient_name') calories = request.form.get('calories') measurement = request.form.get('measurement') user = crud.get_user_by_email(session['email']) photo = "/static/img/smoothie.jpg" recipe = crud.create_recipe(recipe_name, user) ingredient = crud.create_ingredient(ingredient_name, calories, measurement, photo) crud.create_recipe_ingredient(recipe, ingredient) crud.create_user_cleanse_recipe(timestamp, date, user_cleanse, recipe) return redirect(f'/cleanse/{user_cleanse_id}') else: if 'user_id' not in session: return redirect('/login') return render_template('add_recipe.html')
def user_cleanse_recipes(user_cleanse_id): """Shows recipes/smoothies that belong to a specific cleanse and their ingredients Users can also submit an entry to their cleanse log for a specific cleanse""" if request.method == 'POST': if "form-submit" in request.form: timestamp = datetime.now() date = datetime.now() user_cleanse = crud.get_user_cleanse(user_cleanse_id) recipe_name = request.form.get('recipe_name') ingredient_name = request.form.get('ingredient_name') calories = request.form.get('calories') measurement = request.form.get('measurement') user = crud.get_user_by_email(session['email']) photo = "/static/img/smoothie.jpg" recipe = crud.create_recipe(recipe_name, user) ingredient = crud.create_ingredient(ingredient_name, calories, measurement, photo) crud.create_recipe_ingredient(recipe, ingredient) crud.create_user_cleanse_recipe(timestamp, date, user_cleanse, recipe) return redirect(f'/cleanse/{user_cleanse_id}') if "form2-submit" in request.form: timestamp = datetime.now() comment = request.form.get('comment') private = request.form.get('private') if private == 'on': private = False else: private = True user_cleanse = crud.get_user_cleanse(user_cleanse_id) crud.create_cleanse_log(timestamp, comment, private, user_cleanse) return redirect(f'/cleanse/{user_cleanse_id}') else: if 'user_id' not in session: return redirect('/login') user_cleanse = crud.get_user_cleanse(user_cleanse_id) user_cleanse_recipes = crud.get_user_cleanse_recipes(user_cleanse_id) session['user_cleanse_id'] = user_cleanse_id cleanse_logs = crud.get_cleanse_logs(user_cleanse_id) return render_template('cleanse_details.html', user_cleanse_recipes=user_cleanse_recipes, user_cleanse=user_cleanse, cleanse_logs=cleanse_logs)
def create_test_ingredients(): """Create test ingredients.""" test_ingredients_in_db = [] # Capture test ingredients from get_test_ingredients() function test_ingredients = get_test_ingredients() for test_ingredient in test_ingredients: # Use .title() to make the first letter of all ingredients capitalized ingredient = crud.create_ingredient(test_ingredient.title()) test_ingredients_in_db.append(ingredient) return test_ingredients_in_db
def create_recipe(): """Show create recipe page.""" if request.method == "GET": cuisines = crud.get_cuisines() categories = crud.get_categories() ingredients = crud.get_ingredients() if session.get("user_id") == None: flash("Please log in to create a new recipe.") return redirect('/') return render_template("create_recipe.html", cuisines=cuisines, categories=categories, ingredients=ingredients) elif request.method == "POST": kwargs = { "title": request.form.get("title"), "description": request.form.get("description"), "prep_time": request.form.get("prep_time"), "cook_time": request.form.get("cook_time"), "total_time": request.form.get("total_time"), "serving_qty": request.form.get("serving_qty"), "source": request.form.get("source"), "user_id": session.get("user_id"), "cuisine_id": request.form.get("cuisine_id") } recipe = crud.create_recipe(**kwargs) category_id = request.form.get("category_id") crud.create_recipe_category(recipe.recipe_id, category_id) # Get ingredient, measurement, and instruction items in a dictionary # If flat is False, returns all items as a list request_dict = request.form.to_dict(flat=False) for ingredient, measurement in zip(request_dict["ingredients"], request_dict["measurements"]): ingredient_object = crud.create_ingredient(ingredient) crud.create_recipe_ingredient(recipe.recipe_id, ingredient_object.ingredient_id, measurement) for step_num, instruction in enumerate(request_dict["instructions"]): crud.create_recipe_step(recipe.recipe_id, step_num + 1, instruction) # Get images from create recipe form for image in request_dict["images"]: image_object = crud.create_image(image) crud.create_recipe_image(recipe.recipe_id, image_object.image_id) if recipe: flash('Congrats! A new recipe was created.') else: flash('Error.') return redirect('/create_recipe')
i_name_2 = SEED[i]['ingredients'][2]['name'] calories_2 = SEED[i]['ingredients'][2]['calories'] measurement_2 = SEED[i]['ingredients'][2]['measurement'] photo_2 = SEED[i]['ingredients'][2]['photo'] i_name_3 = SEED[i]['ingredients'][3]['name'] calories_3 = SEED[i]['ingredients'][3]['calories'] measurement_3 = SEED[i]['ingredients'][3]['measurement'] photo_3 = SEED[i]['ingredients'][3]['photo'] global_comment_dict = SEED[i].get('global_comments', {'comment': 'test'}) global_comment = global_comment_dict['comment'] comment_timestamp = global_comment_dict['timestamp'] db_user = crud.create_user(username, email, hashed, name, location, about, member_since) db_ingredient = crud.create_ingredient(i_name, calories, measurement_1, photo_1) db_ingredient_2 = crud.create_ingredient(i_name_2, calories_2, measurement_2, photo_2) db_ingredient_3 = crud.create_ingredient(i_name_3, calories_3, measurement_3, photo_3) db_recipe = crud.create_recipe(r_name, db_user) db_cleanse = crud.create_cleanse(start_date, end_date, public, description, db_user) crud.create_recipe_ingredient(db_recipe, db_ingredient) crud.create_recipe_ingredient(db_recipe, db_ingredient_2) crud.create_recipe_ingredient(db_recipe, db_ingredient_3) db_user_cleanse = crud.create_user_cleanse(active, completed, db_cleanse, db_user) crud.create_user_cleanse_recipe(timestamp, start_date, db_user_cleanse, db_recipe) crud.create_cleanse_log(clog_timestamp, comment, private, db_user_cleanse) db_comment = crud.create_global_comment(global_comment, timestamp, db_user)
clog_timestamp = SEED[i]['cleanse_log']['timestamp'] comment = SEED[i]['cleanse_log']['comment'] private = SEED[i]['cleanse_log']['private'] r_name = SEED[i]['recipe']['name'] timestamp = SEED[i]['recipe']['timestamp'] i_name = SEED[i]['ingredients'][1]['name'] calories = SEED[i]['ingredients'][1]['calories'] i_name_2 = SEED[i]['ingredients'][2]['name'] calories_2 = SEED[i]['ingredients'][2]['calories'] i_name_3 = SEED[i]['ingredients'][3]['name'] calories_3 = SEED[i]['ingredients'][3]['calories'] db_user = crud.create_user(username, email, password) db_ingredient = crud.create_ingredient(i_name, calories) db_ingredient_2 = crud.create_ingredient(i_name_2, calories_2) db_ingredient_3 = crud.create_ingredient(i_name_3, calories_3) db_recipe = crud.create_recipe(r_name, db_user) db_cleanse = crud.create_cleanse(start_date, end_date, public, description, db_user) crud.create_recipe_ingredient(db_recipe, db_ingredient) crud.create_recipe_ingredient(db_recipe, db_ingredient_2) crud.create_recipe_ingredient(db_recipe, db_ingredient_3) db_user_cleanse = crud.create_user_cleanse(active, completed, db_cleanse, db_user) crud.create_user_cleanse_recipe(timestamp, start_date, db_user_cleanse, db_recipe) crud.create_cleanse_log(clog_timestamp, comment, private, db_user_cleanse)
import json import csv # Import dabase model, crud, and server import crud import model import server # Drop and create db os.system('dropdb recipes') os.system('createdb recipes') # Connect db to Flask app model.connect_to_db(server.app) # Create tables from classes inherited from db.model model.db.create_all() # read and open ingredients csv file = open('data/top-1k-ingredients.csv', newline='') data = csv.reader(file, delimiter=';') # each row is a list of 2, name and id ingredients_data = {row[1]: row[0] for row in data} # make id as key and name as value # loop over ingredients dictionary for ingredient in ingredients_data: # create an ingredient and save to db crud.create_ingredient(ingredient_id=ingredient, name=ingredients_data[ingredient])
brand=brand, product_categorization=product_categorization) print('*******************************') print(product) print('*******************************') # product = crud.get_product_id(product_id) # if product == None: # product = crud.create_product(product_id) # print('*******************************') # print(product) # print('*******************************') ingredient = crud.get_ingredient_by_name(ingredient_name) if ingredient == None: ingredient = crud.create_ingredient(ingredient_name) print('*******************************') print(ingredient) print('*******************************') product_ingredient = crud.get_prod_ing_by_prod_and_ing( product, ingredient) if product_ingredient == None: product_ingredient = crud.create_product_ingredient( product, ingredient) print('*******************************') print(product_ingredient) print('*******************************') count += 1