From 720bae6c221a681c266cbfb3868b835c9a13f324 Mon Sep 17 00:00:00 2001 From: jableader Date: Mon, 13 May 2024 13:59:46 +1000 Subject: [PATCH] Fixed typehints --- ingredients/db.py | 6 +++--- main.py | 4 ++-- meals/db.py | 8 +++----- persons/db.py | 6 +++--- products/db.py | 6 +++--- recipes/db.py | 6 +++--- 6 files changed, 17 insertions(+), 19 deletions(-) diff --git a/ingredients/db.py b/ingredients/db.py index 6e8b14e..9c748a5 100644 --- a/ingredients/db.py +++ b/ingredients/db.py @@ -1,7 +1,7 @@ from products import Product from pydantic import BaseModel -from typing import List, ClassVar, Optional +from typing import AsyncIterator, List, ClassVar, Optional class Ingredient(BaseModel): KEYS: ClassVar[List[str]] = ['id', 'name', 'line', 'preparation', 'unit', 'quantity', 'product_id', 'recipe_id', 'meal_id'] @@ -47,7 +47,7 @@ async def insert_ingredient(conn, ingredient: Ingredient): ''', (ingredient.name, ingredient.line, ingredient.preparation, ingredient.unit, ingredient.quantity, ingredient.product_id, ingredient.recipe_id, ingredient.meal_id)) as cursor: ingredient.id = cursor.lastrowid -async def find_ingredients_by_recipe_id(conn, recipe_id: int) -> List[Ingredient]: +async def find_ingredients_by_recipe_id(conn, recipe_id: int) -> AsyncIterator[Ingredient]: ingredient_keys = [f'ingredient.{key}' for key in Ingredient.KEYS] product_keys = [f'product.{key}' for key in Product.KEYS] @@ -61,7 +61,7 @@ async def find_ingredients_by_recipe_id(conn, recipe_id: int) -> List[Ingredient product = Product(**product_keys) if product_keys['id'] else None yield Ingredient(**{k:v for k,v in zip(Ingredient.KEYS, row[:len(Ingredient.KEYS)])}, product=product) -async def find_ingredients_by_meal_id(conn, meal_id: int) -> List[Ingredient]: +async def find_ingredients_by_meal_id(conn, meal_id: int) -> AsyncIterator[Ingredient]: ingredient_keys = [f'ingredient.{key}' for key in Ingredient.KEYS] product_keys = [f'product.{key}' for key in Product.KEYS] diff --git a/main.py b/main.py index dc83e32..3c9e54b 100644 --- a/main.py +++ b/main.py @@ -14,7 +14,7 @@ app = FastAPI() # Add CORS middleware app.add_middleware( CORSMiddleware, - allow_origins=["*", "http://localhost:8080", "https://localhost:8080"], + allow_origins=["*", "http://localhost:8080", "https://localhost:8080", "http://192.168.68.183:8080"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"] @@ -132,7 +132,7 @@ async def delete_recipe(recipe_id: int, conn: sqlite3.Connection = Depends(get_d @app.get("/meals/") async def get_meals(date_from: Annotated[datetime.datetime, Query(alias='from')], to: datetime.datetime, conn: sqlite3.Connection = Depends(get_db)) -> List[meals.Meal]: result = [] - for meal in await meals.find_meals_by_date_range(conn, date_from, to): + async for meal in meals.find_meals_by_date_range(conn, date_from, to): await meals.load_recipes(conn, meal) await meals.load_extra_ingredients(conn, meal) await meals.load_participants(conn, meal) diff --git a/meals/db.py b/meals/db.py index 3fe039d..4cf2c02 100644 --- a/meals/db.py +++ b/meals/db.py @@ -1,4 +1,4 @@ -from typing import List, ClassVar, Optional +from typing import AsyncIterator, List, ClassVar, Optional from pydantic import BaseModel from ingredients import Ingredient, insert_ingredient, find_ingredients_by_meal_id, delete_ingredients_by_meal_id @@ -102,15 +102,13 @@ async def find_meal_by_date(conn, date: datetime) -> Meal: async for row in cursor: return Meal(**{k:v for k,v in zip(Meal.KEYS, row)}) -async def find_meals_by_date_range(conn, start: datetime, end: datetime) -> List[Meal]: +async def find_meals_by_date_range(conn, start: datetime, end: datetime) -> AsyncIterator[Meal]: async with conn.execute(f''' SELECT {','.join(Meal.KEYS)} FROM Meal WHERE meal_date >= ? AND meal_date <= ? ''', (start, end)) as cursor: - result = [] async for row in cursor: - result.append(Meal(**{k:v for k,v in zip(Meal.KEYS, row)})) - return result + yield Meal(**{k:v for k,v in zip(Meal.KEYS, row)}) async def load_participants(conn, meal: Meal) -> None: async with conn.execute(f''' diff --git a/persons/db.py b/persons/db.py index f69fa06..46d1fbb 100644 --- a/persons/db.py +++ b/persons/db.py @@ -1,5 +1,5 @@ from pydantic import BaseModel -from typing import List +from typing import AsyncIterator class Person(BaseModel): id: int @@ -12,7 +12,7 @@ async def create(conn): name TEXT UNIQUE );''') -async def search_by_name(conn, name: str) -> List[Person]: +async def search_by_name(conn, name: str) -> AsyncIterator[Person]: async with conn.execute(''' SELECT id, name FROM Person @@ -44,7 +44,7 @@ async def get_by_id(conn, id: int) -> Person: return None return Person(id=row[0], name=row[1]) -async def get_all(conn) -> List[Person]: +async def get_all(conn) -> AsyncIterator[Person]: async with conn.execute(''' SELECT id, name FROM Person diff --git a/products/db.py b/products/db.py index e2d4fad..295b02a 100644 --- a/products/db.py +++ b/products/db.py @@ -1,4 +1,4 @@ -from typing import List, ClassVar +from typing import AsyncIterator, List, ClassVar from pydantic import BaseModel import json @@ -32,7 +32,7 @@ async def create(conn): FOREIGN KEY (food_item_id) REFERENCES Product(id) );''') -async def find_product_by_tag(conn, tag: str) -> List[Product]: +async def find_product_by_tag(conn, tag: str) -> AsyncIterator[Product]: async with conn.execute(f''' SELECT {','.join(Product.KEYS)} FROM Product WHERE id IN ( @@ -78,7 +78,7 @@ async def add_tag(conn, product: Product, tag: str): await conn.commit() -async def get_tags(conn, product: Product) -> List[str]: +async def get_tags(conn, product: Product) -> AsyncIterator[str]: async with conn.execute(''' SELECT tag FROM ProductTag WHERE food_item_id = ? diff --git a/recipes/db.py b/recipes/db.py index 08e0ec2..94a8d64 100644 --- a/recipes/db.py +++ b/recipes/db.py @@ -4,7 +4,7 @@ from persons import Person from ingredients import Ingredient, find_ingredients_by_recipe_id from pydantic import BaseModel -from typing import List, ClassVar, Tuple, Optional +from typing import AsyncIterator, List, ClassVar, Tuple, Optional class Recipe(BaseModel): KEYS: ClassVar[List[str]] = ['id', 'name', 'link', 'image_urls', 'based_on_recipe', 'created_by_id', 'date_created', 'hidden_by_id', 'date_hidden'] @@ -71,7 +71,7 @@ async def find_recipe_by_id(conn, recipe_id: int) -> Recipe: async for row in cursor: return row_to_recipe(zip(Recipe.KEYS, row)) -async def find_recipes_by_name(conn, name: str) -> List[Recipe]: +async def find_recipes_by_name(conn, name: str) -> AsyncIterator[Recipe]: async with conn.execute(f''' SELECT {','.join(Recipe.KEYS)} FROM Recipe WHERE name LIKE ? AND date_hidden IS NULL @@ -79,7 +79,7 @@ async def find_recipes_by_name(conn, name: str) -> List[Recipe]: async for row in cursor: yield row_to_recipe(zip(Recipe.KEYS, row)) -async def get_all(conn) -> List[Recipe]: +async def get_all(conn) -> AsyncIterator[Recipe]: async with conn.execute(f''' SELECT {','.join(Recipe.KEYS)} FROM Recipe WHERE date_hidden IS NULL ''') as cursor: