Fixed typehints

This commit is contained in:
jableader 2024-05-13 13:59:46 +10:00
parent c241b8dd8d
commit 720bae6c22
6 changed files with 17 additions and 19 deletions

View file

@ -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]

View file

@ -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)

View file

@ -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'''

View file

@ -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

View file

@ -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 = ?

View file

@ -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: