Beispiel #1
0
def get_ingredient_id(ing_name):
    session = Session()
    ing_id = (session.query(
        Ingredient.id).filter(Ingredient.ingredient == ing_name).first())
    session.close()
    ing_id = ing_id[0]
    return ing_id
Beispiel #2
0
def insert_new_recipe(
    rec_title,
    rec_instructions,
    rec_url,
    rec_img,
    calories,
    carbs,
    fibre,
    sugar,
    protein,
    fats,
    sat_fats,
    serving_size,
):
    session = Session()
    new_recipe = Recipe(
        rec_title,
        rec_instructions,
        rec_url,
        rec_img,
        calories,
        carbs,
        fibre,
        sugar,
        protein,
        fats,
        sat_fats,
        serving_size,
    )
    session.add(new_recipe)
    session.commit()
    session.close()
Beispiel #3
0
def connect_shoplist_with_ingredients_and_user(ingredients, user_id,
                                               shoplist_name):
    session = Session()

    now = datetime.datetime.now()
    date = now.strftime("%Y-%m-%d %H:%M:%S")

    newshopList = ShopList(shoplist_name, str(date))

    new_user = session.query(User).filter(User.id == user_id).first()
    new_ingredients = (session.query(Ingredient).filter(
        Ingredient.ingredient.in_(ingredients)).all())

    old_ings = []
    for ing in new_ingredients:
        old_ings.append(ing.ingredient)

    for new_ing in ingredients:
        if new_ing not in old_ings:
            insert_new_ingredient(new_ing)

    new_ingredients = (session.query(Ingredient).filter(
        Ingredient.ingredient.in_(ingredients)).all())

    newshopList.users.append(new_user)

    newshopList.ingredients.extend(new_ingredients)

    session.add(newshopList)
    session.commit()
    session.close()
Beispiel #4
0
def get_user_recipes_all(session_id):
    session = Session()

    # get current user object
    user = session.query(User).filter(User.id == session_id).first()
    recipe_list = user.recipes
    session.close()
    # returns a list of recipe objects
    return recipe_list
Beispiel #5
0
def connect_recipe_with_ingredients_and_user(
    rec_title,
    rec_instructions,
    rec_ingredients,
    scrape_url,
    rec_image,
    rec_calories,
    rec_carbs,
    rec_fibre,
    rec_sugar,
    rec_protein,
    rec_fats,
    rec_sat_fats,
    rec_serving,
    user_id,
):
    session = Session()

    newRecipe = Recipe(
        rec_title,
        rec_instructions,
        scrape_url,
        rec_image,
        rec_calories,
        rec_carbs,
        rec_fibre,
        rec_sugar,
        rec_protein,
        rec_fats,
        rec_sat_fats,
        rec_serving,
    )

    new_user = session.query(User).filter(User.id == user_id).first()
    new_ingredients = (session.query(Ingredient).filter(
        Ingredient.ingredient.in_(rec_ingredients)).all())

    old_ings = []
    for ing in new_ingredients:
        old_ings.append(ing.ingredient)

    for new_ing in rec_ingredients:
        if new_ing not in old_ings:
            insert_new_ingredient(new_ing)

    new_ingredients = (session.query(Ingredient).filter(
        Ingredient.ingredient.in_(rec_ingredients)).all())

    newRecipe.users.append(new_user)

    newRecipe.ingredients.extend(new_ingredients)

    session.add(newRecipe)
    session.commit()
    session.close()
Beispiel #6
0
def connect_recipe_with_ingredients(rec_id, ing_ids):
    session = Session()

    recipe = session.query(Recipe).filter(Recipe.id == rec_id).first()
    # get ingredients for recipe
    ingredients = session.query(Ingredient).filter(
        Ingredient.id.in_(ing_ids)).all()

    recipe.ingredients = ingredients
    session.add(recipe)
    session.commit()
    session.close()
Beispiel #7
0
def connect_user_to_shoplist(user_id):
    session = Session()
    user = session.query(User).filter(User.id == user_id).first()

    ingredients_list = []
    for recipe in user.recipes:
        for ingredient in recipe.ingredients:
            ingredients_list.append(ingredient)

    user.ingredients = ingredients_list
    session.add(user)
    session.commit()
    session.close()
Beispiel #8
0
def connect_user_with_recipe(user_id, rec_id):
    session = Session()
    user = session.query(User).filter(User.id == user_id).first()
    new_recipe = session.query(Recipe).filter(Recipe.id == rec_id).first()

    # get all current recipes
    all_recipes = user.recipes

    # append new recipe and add as new user.recipes
    all_recipes.append(new_recipe)
    user.recipes = all_recipes

    session.add(user)
    session.commit()
    session.close()
    def post_many(self, tickers):
        DBSession = sessionmaker(bind=self.engine)
        session = DBSession()

        try:
            with session.no_autoflush:
                for ticker in tickers:
                    if ticker.get('ticker', None):
                        ticker_instance = models.Ticker(
                            symbol=ticker['ticker'], )
                        session.add(ticker_instance)
            session.commit()
            print("Post successful.")
        except Exception as e:
            print("Post many failed. Roll Back ! ", e)
            session.rollback()
        finally:
            session.close()
Beispiel #10
0
def get_ingredients_for_recipeList(recipe_titles):
    session = Session()

    # ingredientList = []
    # for rec_title in recipe_titles:
    #     recipe = session.query(Recipe).filter(Recipe.title == rec_title).first()
    #     for ing in recipe.ingredients:
    #         ingredientList.append(ing.ingredient)

    ingredientList = []

    recipe = session.query(Recipe).filter(
        Recipe.title == recipe_titles).first()
    for ing in recipe.ingredients:
        ingredientList.append(ing.ingredient)

    session.close()
    return ingredientList
Beispiel #11
0
def disconnect_shoplist_from_user(rec_id, user_id):
    session = Session()
    user = session.query(User).filter(User.id == user_id).first()
    shoplist_todelete = session.query(ShopList).filter(
        ShopList.id == rec_id).first()

    new_shoplists = []
    for shoplist in user.shoplists:
        if shoplist != shoplist_todelete:
            new_shoplists.append(shoplist)

    user.shoplists = new_shoplists

    if len(shoplist_todelete.users) == 0:
        for ingredient in shoplist_todelete.ingredients:
            if len(ingredient.recipes) == 0 and len(ingredient.shoplists) == 1:
                session.delete(ingredient)
        session.delete(shoplist_todelete)

    session.add(user)
    session.commit()
    session.close()
Beispiel #12
0
def disconnect_recipe_from_user(rec_title, user_id):
    session = Session()
    user = session.query(User).filter(User.id == user_id).first()
    recipe_todelete = session.query(Recipe).filter(
        Recipe.title == rec_title).first()

    new_recipes = []
    for recipe in user.recipes:
        if recipe != recipe_todelete:
            new_recipes.append(recipe)

    user.recipes = new_recipes

    if len(recipe_todelete.users) == 0:

        for ingredient in recipe_todelete.ingredients:
            if len(ingredient.recipes) == 1:
                session.delete(ingredient)

        session.delete(recipe_todelete)

    session.add(user)
    session.commit()
    session.close()
Beispiel #13
0
def get_user_shoplists(user_id):
    session = Session()

    user = session.query(User).filter(User.id == user_id).first()

    ingList = []

    for shoplist in user.shoplists:
        # shop_ingList = []
        # # for ing in shoplist.ingredients:
        # #     shop_ingList.append(ing.ingredient)
        # # ingList.append(shop_ingList)
        list_dict = {}
        list_dict["id"] = shoplist.id
        list_dict["name"] = shoplist.name
        list_dict["date"] = shoplist.date
        shop_ingList = []
        for ing in shoplist.ingredients:
            shop_ingList.append(ing.ingredient)
        list_dict["ingredients"] = shop_ingList
        ingList.append(list_dict)

    session.close()
    return ingList
Beispiel #14
0
def get_all_ingredients():
    session = Session()
    all_ingredients = session.query(Ingredient.ingredient).all()
    session.close()
    return all_ingredients
Beispiel #15
0
def get_ingredients_for_user(user_id):
    session = Session()
    user = session.query(User).filter(User.id == user_id).first()
    shoplist = user.ingredients
    session.close()
    return shoplist
Beispiel #16
0
def insert_new_ingredient(new_ing):
    session = Session()
    new_ingredient = Ingredient(new_ing, "measure", "Other info")
    session.add(new_ingredient)
    session.commit()
    session.close()
Beispiel #17
0
def get_all_recipes():
    session = Session()
    allRecipes = session.query(Recipe.title).all()
    session.close()
    return allRecipes
Beispiel #18
0
def get_id_of_existing_or_inserted_recipe(rec_title):
    session = Session()
    rec_id = session.query(Recipe.id).filter(Recipe.title == rec_title).first()
    session.close()
    rec_id = rec_id[0]
    return rec_id
Beispiel #19
0
    # Temporary supplement information to create csv from largest dict items
    #   and smallest dict items to investigate any parsing errors
    maxtuple, mintuple = tmp_info(maxtuple, mintuple)

    # clen = len(champlist)
    # if len(cdir) > maxtuple[2]:
    #     maxtuple = clen - 1, d, len(cdir)
    # if len(cdir) < mintuple[2]:
    #     mintuple = clen - 1, d, len(cdir)
    d += 1
    # End of Temporary info

    rem -= 1
    index += 1
session.close()

# Temporary min/max info
log.info(
    "First most keys: %s (%s) = %s",
    champlist[maxtuple[0]]["Name"],
    maxtuple[1],
    maxtuple[2],
)

log.info(
    "First least keys: %s (%s) = %s",
    champlist[mintuple[0]]["Name"],
    mintuple[1],
    mintuple[2],
)
Beispiel #20
0
def get_ingredients_for_recipe(rec_id):
    session = Session()
    recipe = session.query(Recipe).filter(Recipe.id == rec_id).first()
    ingredients = recipe.ingredients
    session.close()
    return ingredients