fix auto initialize models

This commit is contained in:
hay-kot
2021-08-23 12:25:16 -08:00
parent 24a4b5b810
commit abb0b84e22
5 changed files with 61 additions and 96 deletions

View File

@@ -1,23 +1,8 @@
from mealie.db.models._model_base import BaseMixins, SqlAlchemyBase
from requests import Session
from sqlalchemy import Column, ForeignKey, Integer, String, Table, orm
from sqlalchemy import Column, ForeignKey, Integer, String, orm
from .._model_utils import auto_init
ingredients_to_units = Table(
"ingredients_to_units",
SqlAlchemyBase.metadata,
Column("ingredient_units.id", Integer, ForeignKey("ingredient_units.id")),
Column("recipes_ingredients_id", Integer, ForeignKey("recipes_ingredients.id")),
)
ingredients_to_foods = Table(
"ingredients_to_foods",
SqlAlchemyBase.metadata,
Column("ingredient_foods.id", Integer, ForeignKey("ingredient_foods.id")),
Column("recipes_ingredients_id", Integer, ForeignKey("recipes_ingredients.id")),
)
class IngredientUnitModel(SqlAlchemyBase, BaseMixins):
__tablename__ = "ingredient_units"
@@ -25,7 +10,7 @@ class IngredientUnitModel(SqlAlchemyBase, BaseMixins):
name = Column(String)
description = Column(String)
abbreviation = Column(String)
ingredients = orm.relationship("RecipeIngredient", secondary=ingredients_to_units, back_populates="unit")
ingredients = orm.relationship("RecipeIngredient", back_populates="unit")
@auto_init()
def __init__(self, **_) -> None:
@@ -37,7 +22,7 @@ class IngredientFoodModel(SqlAlchemyBase, BaseMixins):
id = Column(Integer, primary_key=True)
name = Column(String)
description = Column(String)
ingredients = orm.relationship("RecipeIngredient", secondary=ingredients_to_foods, back_populates="food")
ingredients = orm.relationship("RecipeIngredient", back_populates="food")
@auto_init()
def __init__(self, **_) -> None:
@@ -54,19 +39,15 @@ class RecipeIngredient(SqlAlchemyBase, BaseMixins):
note = Column(String) # Force Show Text - Overrides Concat
# Scaling Items
unit = orm.relationship(IngredientUnitModel, secondary=ingredients_to_units, uselist=False)
food = orm.relationship(IngredientFoodModel, secondary=ingredients_to_foods, uselist=False)
unit_id = Column(Integer, ForeignKey("ingredient_units.id"))
unit = orm.relationship(IngredientUnitModel, uselist=False)
food_id = Column(Integer, ForeignKey("ingredient_foods.id"))
food = orm.relationship(IngredientFoodModel, uselist=False)
quantity = Column(Integer)
# Extras
def __init__(self, title: str, note: str, unit: dict, food: dict, quantity: int, session: Session, **_) -> None:
self.title = title
self.note = note
self.quantity = quantity
if unit:
self.unit = IngredientUnitModel.get_ref(unit.get("id"))
if food:
self.food = IngredientFoodModel.get_ref(unit.get("id"))
@auto_init()
def __init__(self, **_) -> None:
pass