From e7b978ae3c6fc57288e21204b7fb1a1f8b042401 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Gr=C3=B6ger?= Date: Wed, 30 Jun 2021 10:45:41 +0200 Subject: [PATCH] better drink dependency drink_ingredient has name and cost_per_volume https://flaschengeist.dev/Flaschengeist/flaschengeist-pricelist/issues/2 --- flaschengeist/plugins/pricelist/models.py | 17 +++++++++-------- .../plugins/pricelist/pricelist_controller.py | 4 ++++ 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/flaschengeist/plugins/pricelist/models.py b/flaschengeist/plugins/pricelist/models.py index ffde797..f6d68f9 100644 --- a/flaschengeist/plugins/pricelist/models.py +++ b/flaschengeist/plugins/pricelist/models.py @@ -76,16 +76,17 @@ class DrinkIngredient(db.Model, ModelSerializeMixin): id: int = db.Column("id", db.Integer, primary_key=True) volume: float = db.Column(db.Numeric(precision=5, scale=2, asdecimal=False), nullable=False) ingredient_id: int = db.Column(db.Integer, db.ForeignKey("drink.id")) - # drink_ingredient: Drink = db.relationship("Drink") - # price: float = 0 + cost_per_volume: float + name: str + _drink_ingredient: Drink = db.relationship("Drink") + @property + def cost_per_volume(self): + return self._drink_ingredient.cost_per_volume if self._drink_ingredient else None -# @property -# def price(self): -# try: -# return self.drink_ingredient.cost_price_pro_volume * self.volume -# except AttributeError: -# pass + @property + def name(self): + return self._drink_ingredient.name if self._drink_ingredient else None class Ingredient(db.Model, ModelSerializeMixin): diff --git a/flaschengeist/plugins/pricelist/pricelist_controller.py b/flaschengeist/plugins/pricelist/pricelist_controller.py index 1c1491e..c89dc2d 100644 --- a/flaschengeist/plugins/pricelist/pricelist_controller.py +++ b/flaschengeist/plugins/pricelist/pricelist_controller.py @@ -315,6 +315,10 @@ def delete_price(identifier): def set_drink_ingredient(data): allowed_keys = DrinkIngredient().serialize().keys() values = {key: value for key, value in data.items() if key in allowed_keys} + if "cost_per_volume" in values: + values.pop("cost_per_volume") + if "name" in values: + values.pop("name") ingredient_id = values.pop("id", -1) if ingredient_id < 0: drink_ingredient = DrinkIngredient(**values)