From 41aa1f8e001591e7641ba88ce23d8c1915cef4da Mon Sep 17 00:00:00 2001 From: jableader Date: Sun, 28 Apr 2024 13:57:02 +1000 Subject: [PATCH] Fixed insert bug in testdata --- main.py | 6 ++++++ recipes/__init__.py | 2 +- recipes/db.py | 11 +++++++++-- test_data.py | 7 ++++--- 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/main.py b/main.py index eefb541..981522d 100644 --- a/main.py +++ b/main.py @@ -103,12 +103,18 @@ async def create_recipe(item: recipes.Recipe, conn: sqlite3.Connection = Depends if not ingredient.product: return JSONResponse(status_code=400, content={'message': 'Ingredient must have a product'}) + if item.id: + await recipes.hide_recipe(conn, item.id, user) + item.based_on_recipe = item.id + item.id = 0 + item.created_by_id = user.id await recipes.insert_recipe(conn, item) for ingredient in item.ingredients: ingredient.recipe_id = item.id ingredient.product_id = ingredient.product.id await ingredients.insert_ingredient(conn, ingredient) + await conn.commit() return item diff --git a/recipes/__init__.py b/recipes/__init__.py index 9d1ef14..adb2cf5 100644 --- a/recipes/__init__.py +++ b/recipes/__init__.py @@ -1,4 +1,4 @@ -from recipes.db import Recipe, insert_recipe, find_recipe_by_id, get_all, find_recipes_by_name, row_to_recipe, load_recipe_ingredients +from recipes.db import Recipe, insert_recipe, find_recipe_by_id, get_all, find_recipes_by_name, row_to_recipe, load_recipe_ingredients, hide_recipe from recipes.scraping import scrape_recipe_ldata as _scrape_recipe_ldata from ingredients import parse_ingredient_from_nlp as _parse_ingredient_from_nlp, match_existing_products as _match_existing_products diff --git a/recipes/db.py b/recipes/db.py index 2165bba..08e0ec2 100644 --- a/recipes/db.py +++ b/recipes/db.py @@ -50,6 +50,13 @@ async def insert_recipe(conn, recipe: Recipe): ''', (recipe.name, recipe.link, json.dumps(recipe.image_urls), recipe.based_on_recipe, recipe.created_by_id)) as cursor: recipe.id = cursor.lastrowid +async def hide_recipe(conn, recipe_id: int, person: Person): + await conn.execute(''' + UPDATE Recipe + SET date_hidden = CURRENT_TIMESTAMP, hidden_by_id = ? + WHERE id = ? + ''', (person.id, recipe_id)) + def row_to_recipe(col_tuples: List[Tuple[str, ...]]) -> Recipe: d = {k:v for k,v in col_tuples} d['image_urls'] = json.loads(d['image_urls']) @@ -67,14 +74,14 @@ async def find_recipe_by_id(conn, recipe_id: int) -> Recipe: async def find_recipes_by_name(conn, name: str) -> List[Recipe]: async with conn.execute(f''' SELECT {','.join(Recipe.KEYS)} FROM Recipe - WHERE name LIKE ? + WHERE name LIKE ? AND date_hidden IS NULL ''', (f'%{name}%',)) as cursor: async for row in cursor: yield row_to_recipe(zip(Recipe.KEYS, row)) async def get_all(conn) -> List[Recipe]: async with conn.execute(f''' - SELECT {','.join(Recipe.KEYS)} FROM Recipe + SELECT {','.join(Recipe.KEYS)} FROM Recipe WHERE date_hidden IS NULL ''') as cursor: async for row in cursor: yield row_to_recipe(zip(Recipe.KEYS, row)) diff --git a/test_data.py b/test_data.py index 42a18ab..7dd6789 100644 --- a/test_data.py +++ b/test_data.py @@ -211,11 +211,12 @@ if __name__ == '__main__': await products.insert_product(conn, product, {}) await products.add_missing_tags(conn, product, Products._tags[product.product_id]) - for ingredient in class_fields(Ingredients).values(): - await ingredients.insert_ingredient(conn, ingredient) - for recipe in class_fields(Recipes).values(): await recipes.insert_recipe(conn, recipe) + for ingredient in recipe.ingredients: + ingredient.recipe_id = recipe.id + ingredient.product_id = ingredient.product.id + await ingredients.insert_ingredient(conn, ingredient) for meal in class_fields(Meals).values(): await meals_db.insert_meal(conn, meal)