Fixed insert bug in testdata
This commit is contained in:
parent
ea3635b8d6
commit
41aa1f8e00
4 changed files with 20 additions and 6 deletions
6
main.py
6
main.py
|
|
@ -103,12 +103,18 @@ async def create_recipe(item: recipes.Recipe, conn: sqlite3.Connection = Depends
|
||||||
if not ingredient.product:
|
if not ingredient.product:
|
||||||
return JSONResponse(status_code=400, content={'message': 'Ingredient must have a 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
|
item.created_by_id = user.id
|
||||||
await recipes.insert_recipe(conn, item)
|
await recipes.insert_recipe(conn, item)
|
||||||
for ingredient in item.ingredients:
|
for ingredient in item.ingredients:
|
||||||
ingredient.recipe_id = item.id
|
ingredient.recipe_id = item.id
|
||||||
ingredient.product_id = ingredient.product.id
|
ingredient.product_id = ingredient.product.id
|
||||||
await ingredients.insert_ingredient(conn, ingredient)
|
await ingredients.insert_ingredient(conn, ingredient)
|
||||||
|
|
||||||
await conn.commit()
|
await conn.commit()
|
||||||
|
|
||||||
return item
|
return item
|
||||||
|
|
|
||||||
|
|
@ -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 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
|
from ingredients import parse_ingredient_from_nlp as _parse_ingredient_from_nlp, match_existing_products as _match_existing_products
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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.name, recipe.link, json.dumps(recipe.image_urls), recipe.based_on_recipe, recipe.created_by_id)) as cursor:
|
||||||
recipe.id = cursor.lastrowid
|
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:
|
def row_to_recipe(col_tuples: List[Tuple[str, ...]]) -> Recipe:
|
||||||
d = {k:v for k,v in col_tuples}
|
d = {k:v for k,v in col_tuples}
|
||||||
d['image_urls'] = json.loads(d['image_urls'])
|
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 def find_recipes_by_name(conn, name: str) -> List[Recipe]:
|
||||||
async with conn.execute(f'''
|
async with conn.execute(f'''
|
||||||
SELECT {','.join(Recipe.KEYS)} FROM Recipe
|
SELECT {','.join(Recipe.KEYS)} FROM Recipe
|
||||||
WHERE name LIKE ?
|
WHERE name LIKE ? AND date_hidden IS NULL
|
||||||
''', (f'%{name}%',)) as cursor:
|
''', (f'%{name}%',)) as cursor:
|
||||||
async for row in cursor:
|
async for row in cursor:
|
||||||
yield row_to_recipe(zip(Recipe.KEYS, row))
|
yield row_to_recipe(zip(Recipe.KEYS, row))
|
||||||
|
|
||||||
async def get_all(conn) -> List[Recipe]:
|
async def get_all(conn) -> List[Recipe]:
|
||||||
async with conn.execute(f'''
|
async with conn.execute(f'''
|
||||||
SELECT {','.join(Recipe.KEYS)} FROM Recipe
|
SELECT {','.join(Recipe.KEYS)} FROM Recipe WHERE date_hidden IS NULL
|
||||||
''') as cursor:
|
''') as cursor:
|
||||||
async for row in cursor:
|
async for row in cursor:
|
||||||
yield row_to_recipe(zip(Recipe.KEYS, row))
|
yield row_to_recipe(zip(Recipe.KEYS, row))
|
||||||
|
|
|
||||||
|
|
@ -211,11 +211,12 @@ if __name__ == '__main__':
|
||||||
await products.insert_product(conn, product, {})
|
await products.insert_product(conn, product, {})
|
||||||
await products.add_missing_tags(conn, product, Products._tags[product.product_id])
|
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():
|
for recipe in class_fields(Recipes).values():
|
||||||
await recipes.insert_recipe(conn, recipe)
|
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():
|
for meal in class_fields(Meals).values():
|
||||||
await meals_db.insert_meal(conn, meal)
|
await meals_db.insert_meal(conn, meal)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue