update foods and units for multitenant support

This commit is contained in:
Hayden
2022-02-08 14:55:18 -09:00
parent fbc17b670d
commit 9a82a172cb
11 changed files with 194 additions and 15 deletions

View File

@@ -55,6 +55,10 @@ class Group(SqlAlchemyBase, BaseMixins):
group_reports = orm.relationship("ReportModel", **common_args)
group_event_notifiers = orm.relationship("GroupEventNotifierModel", **common_args)
# Owned Models
ingredient_units = orm.relationship("IngredientUnitModel", **common_args)
ingredient_foods = orm.relationship("IngredientFoodModel", **common_args)
class Config:
exclude = {
"users",

View File

@@ -9,6 +9,11 @@ from .._model_utils.guid import GUID
class IngredientUnitModel(SqlAlchemyBase, BaseMixins):
__tablename__ = "ingredient_units"
# ID Relationships
group_id = Column(GUID, ForeignKey("groups.id"), nullable=False)
group = orm.relationship("Group", back_populates="ingredient_units", foreign_keys=[group_id])
id = Column(Integer, primary_key=True)
name = Column(String)
description = Column(String)
@@ -23,6 +28,11 @@ class IngredientUnitModel(SqlAlchemyBase, BaseMixins):
class IngredientFoodModel(SqlAlchemyBase, BaseMixins):
__tablename__ = "ingredient_foods"
# ID Relationships
group_id = Column(GUID, ForeignKey("groups.id"), nullable=False)
group = orm.relationship("Group", back_populates="ingredient_foods", foreign_keys=[group_id])
id = Column(Integer, primary_key=True)
name = Column(String)
description = Column(String)

View File

@@ -2,7 +2,7 @@ import json
from typing import Generator
from mealie.schema.labels import MultiPurposeLabelSave
from mealie.schema.recipe.recipe_ingredient import CreateIngredientFood, CreateIngredientUnit
from mealie.schema.recipe.recipe_ingredient import SaveIngredientFood, SaveIngredientUnit
from ._abstract_seeder import AbstractSeeder
@@ -27,10 +27,11 @@ class MultiPurposeLabelSeeder(AbstractSeeder):
class IngredientUnitsSeeder(AbstractSeeder):
def load_data(self) -> Generator[CreateIngredientUnit, None, None]:
def load_data(self) -> Generator[SaveIngredientUnit, None, None]:
file = self.resources / "units" / "en-us.json"
for unit in json.loads(file.read_text()).values():
yield CreateIngredientUnit(
yield SaveIngredientUnit(
group_id=self.group_id,
name=unit["name"],
description=unit["description"],
abbreviation=unit["abbreviation"],
@@ -46,10 +47,14 @@ class IngredientUnitsSeeder(AbstractSeeder):
class IngredientFoodsSeeder(AbstractSeeder):
def load_data(self) -> Generator[CreateIngredientFood, None, None]:
def load_data(self) -> Generator[SaveIngredientFood, None, None]:
file = self.resources / "foods" / "en-us.json"
for food in json.loads(file.read_text()):
yield CreateIngredientFood(name=food, description="")
yield SaveIngredientFood(
group_id=self.group_id,
name=food,
description="",
)
def seed(self) -> None:
self.logger.info("Seeding Ingredient Foods")

View File

@@ -5,8 +5,9 @@ from fastapi import APIRouter, Depends
from mealie.routes._base.abc_controller import BaseUserController
from mealie.routes._base.controller import controller
from mealie.routes._base.mixins import CrudMixins
from mealie.schema import mapper
from mealie.schema.query import GetAll
from mealie.schema.recipe.recipe_ingredient import CreateIngredientFood, IngredientFood
from mealie.schema.recipe.recipe_ingredient import CreateIngredientFood, IngredientFood, SaveIngredientFood
router = APIRouter(prefix="/foods", tags=["Recipes: Foods"])
@@ -15,11 +16,11 @@ router = APIRouter(prefix="/foods", tags=["Recipes: Foods"])
class IngredientFoodsController(BaseUserController):
@cached_property
def repo(self):
return self.deps.repos.ingredient_foods
return self.deps.repos.ingredient_foods.by_group(self.group_id)
@cached_property
def mixins(self):
return CrudMixins[CreateIngredientFood, IngredientFood, CreateIngredientFood](
return CrudMixins[SaveIngredientFood, IngredientFood, CreateIngredientFood](
self.repo,
self.deps.logger,
self.registered_exceptions,
@@ -31,7 +32,8 @@ class IngredientFoodsController(BaseUserController):
@router.post("", response_model=IngredientFood, status_code=201)
def create_one(self, data: CreateIngredientFood):
return self.mixins.create_one(data)
save_data = mapper.cast(data, SaveIngredientFood, group_id=self.group_id)
return self.mixins.create_one(save_data)
@router.get("/{item_id}", response_model=IngredientFood)
def get_one(self, item_id: int):

View File

@@ -5,8 +5,9 @@ from fastapi import APIRouter, Depends
from mealie.routes._base.abc_controller import BaseUserController
from mealie.routes._base.controller import controller
from mealie.routes._base.mixins import CrudMixins
from mealie.schema import mapper
from mealie.schema.query import GetAll
from mealie.schema.recipe.recipe_ingredient import CreateIngredientUnit, IngredientUnit
from mealie.schema.recipe.recipe_ingredient import CreateIngredientUnit, IngredientUnit, SaveIngredientUnit
router = APIRouter(prefix="/units", tags=["Recipes: Units"])
@@ -15,7 +16,7 @@ router = APIRouter(prefix="/units", tags=["Recipes: Units"])
class IngredientUnitsController(BaseUserController):
@cached_property
def repo(self):
return self.deps.repos.ingredient_units
return self.deps.repos.ingredient_units.by_group(self.group_id)
@cached_property
def mixins(self):
@@ -31,7 +32,8 @@ class IngredientUnitsController(BaseUserController):
@router.post("", response_model=IngredientUnit, status_code=201)
def create_one(self, data: CreateIngredientUnit):
return self.mixins.create_one(data)
save_data = mapper.cast(data, SaveIngredientUnit, group_id=self.group_id)
return self.mixins.create_one(save_data)
@router.get("/{item_id}", response_model=IngredientUnit)
def get_one(self, item_id: int):

View File

@@ -17,6 +17,10 @@ class CreateIngredientFood(UnitFoodBase):
label_id: UUID4 = None
class SaveIngredientFood(CreateIngredientFood):
group_id: UUID4
class IngredientFood(CreateIngredientFood):
id: int
label: MultiPurposeLabelSummary = None
@@ -30,6 +34,10 @@ class CreateIngredientUnit(UnitFoodBase):
abbreviation: str = ""
class SaveIngredientUnit(CreateIngredientUnit):
group_id: UUID4
class IngredientUnit(CreateIngredientUnit):
id: int