From d5ba1f023e0f726aee72917b01e2c36e74ffc70e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Gr=C3=B6ger?= Date: Thu, 18 Mar 2021 17:26:02 +0100 Subject: [PATCH] [pricelist] fixed circula issue --- flaschengeist/plugins/pricelist/models.py | 42 ++++++++++++------- .../plugins/pricelist/pricelist_controller.py | 27 ++++++------ 2 files changed, 40 insertions(+), 29 deletions(-) diff --git a/flaschengeist/plugins/pricelist/models.py b/flaschengeist/plugins/pricelist/models.py index 2a7e182..703a792 100644 --- a/flaschengeist/plugins/pricelist/models.py +++ b/flaschengeist/plugins/pricelist/models.py @@ -72,15 +72,16 @@ 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) drink_ingredient_id: int = db.Column("drink_ingredient_id", db.Integer, db.ForeignKey("drink.id")) - drink_ingredient = db.relationship("Drink") - price: float = 0 + # drink_ingredient: Drink = db.relationship("Drink") + # price: float = 0 - @property - def price(self): - try: - return self.drink_ingredient.cost_price_pro_volume * self.volume - except AttributeError: - pass + +# @property +# def price(self): +# try: +# return self.drink_ingredient.cost_price_pro_volume * self.volume +# except AttributeError: +# pass class Ingredient(db.Model, ModelSerializeMixin): @@ -90,11 +91,20 @@ class Ingredient(db.Model, ModelSerializeMixin): __tablename__ = "ingredient_association" id: int = db.Column("id", db.Integer, primary_key=True) - volume_id: int = db.Column(db.Integer, db.ForeignKey("drink_price_volume.id")) + volume_id = db.Column(db.Integer, db.ForeignKey("drink_price_volume.id")) drink_ingredient_id = db.Column(db.Integer, db.ForeignKey("drink_ingredient.id")) - drink_ingredient: DrinkIngredient = db.relationship(DrinkIngredient) + drink_ingredient: Optional[DrinkIngredient] = db.relationship(DrinkIngredient) extra_ingredient_id = db.Column(db.Integer, db.ForeignKey("extra_ingredient.id")) - extra_ingredient: ExtraIngredient = db.relationship(ExtraIngredient) + extra_ingredient: Optional[ExtraIngredient] = db.relationship(ExtraIngredient) + + +class MinPrices(ModelSerializeMixin): + """ + MinPrices + """ + + percentage: float + price: float class DrinkPriceVolume(db.Model, ModelSerializeMixin): @@ -106,11 +116,11 @@ class DrinkPriceVolume(db.Model, ModelSerializeMixin): id: int = db.Column("id", db.Integer, primary_key=True) drink_id = db.Column(db.Integer, db.ForeignKey("drink.id"), nullable=False) volume: float = db.Column(db.Numeric(precision=5, scale=2, asdecimal=False)) - ingredients: list[DrinkIngredient, ExtraIngredient] = [] + min_prices: list[MinPrices] = [] + # ingredients: list[Ingredient] = [] prices: list[DrinkPrice] = db.relationship(DrinkPrice, back_populates="volume", cascade="all,delete,delete-orphan") - # TODO: Really protected or just not exported (e.g. name_)? - _ingredients: list[Ingredient] = db.relationship("Ingredient", foreign_keys=Ingredient.volume_id) + ingredients: list[Ingredient] = db.relationship("Ingredient", foreign_keys=Ingredient.volume_id) class Drink(db.Model, ModelSerializeMixin): @@ -129,6 +139,6 @@ class Drink(db.Model, ModelSerializeMixin): _type_id = db.Column("type_id", db.Integer, db.ForeignKey("drink_type.id")) - tags: list[Tag] = db.relationship("Tag", secondary=drink_tag_association, cascade="save-update, merge") - type: DrinkType = db.relationship("DrinkType", foreign_keys=[_type_id]) + tags: Optional[list[Tag]] = db.relationship("Tag", secondary=drink_tag_association, cascade="save-update, merge") + type: Optional[DrinkType] = db.relationship("DrinkType", foreign_keys=[_type_id]) volumes: list[DrinkPriceVolume] = db.relationship(DrinkPriceVolume) diff --git a/flaschengeist/plugins/pricelist/pricelist_controller.py b/flaschengeist/plugins/pricelist/pricelist_controller.py index d688178..8eceec9 100644 --- a/flaschengeist/plugins/pricelist/pricelist_controller.py +++ b/flaschengeist/plugins/pricelist/pricelist_controller.py @@ -269,12 +269,15 @@ def set_drink_ingredient(data): allowedKeys = DrinkIngredient().serialize().keys() if "id" in data: data.pop("id") + drink = None if "drink_ingredient" in data: drink_ingredient_ = data.pop("drink_ingredient") + if "id" in drink_ingredient_: + drink = get_drink(drink_ingredient_.get("id")) values = {key: value for key, value in data.items() if key in allowedKeys} + if "price" in values: + values.pop("price") drink_ingredient = DrinkIngredient(**values) - if "id" in drink_ingredient_: - drink = get_drink(drink_ingredient_.get("id")) if drink: drink_ingredient.drink_ingredient = drink db.session.add(drink_ingredient) @@ -291,15 +294,16 @@ def set_ingredient(data, volume_id): if "id" in data: data.pop("id") values = {key: value for key, value in data.items() if key in allowedKeys} + drink_ingredient_value = None + extra_ingredient_value = None if "drink_ingredient" in values: - drink_ingredient_values = values.get("drink_ingredient") + drink_ingredient_value = values.pop("drink_ingredient") if "extra_ingredient" in values: - extra_ingredient_value = values.get("extra_ingredient") - + extra_ingredient_value = values.pop("extra_ingredient") ingredient = Ingredient(**values) volume = get_volume(volume_id) - if drink_ingredient_values: - ingredient.drink_ingredient = set_drink_ingredient(drink_ingredient_values) + if drink_ingredient_value: + ingredient.drink_ingredient = set_drink_ingredient(drink_ingredient_value) if extra_ingredient_value: if "id" in extra_ingredient_value: ingredient.extra_ingredient = get_extra_ingredient(extra_ingredient_value.get("id")) @@ -315,12 +319,9 @@ def update_ingredient(identifier, data): if "extra_ingredient" in data and isinstance(data.get("extra_ingredient"), dict): if "id" in data.get("extra_ingredient"): ingredient.extra_ingredient = get_extra_ingredient(data.get("extra_ingredient").get("id")) - if "drink_ingredient" in data and ingredient.drink_ingredient and isinstance(data.get("drink_ingredient"), dict): - if "drink_ingredient" in data.get("drink_ingredient"): - if "id" in data.get("drink_ingredient").get("drink_ingredient"): - ingredient.drink_ingredient.drink_ingredient = get_drink( - data.get("drink_ingredient").get("drink_ingredient").get("id") - ) + if "drink_ingredient" in data and ingredient.drink_ingredient: + if data.get("drink_ingredient").get("drink_ingredient_id") > -1: + ingredient.drink_ingredient.drink_ingredient_id = data.get("drink_ingredient").get("drink_ingredient_id") if "volume" in data.get("drink_ingredient"): ingredient.drink_ingredient.volume = data.get("drink_ingredient").get("volume") db.session.commit()