From 5e0e5edf6f9a7aeac442be52104314bb7e0c6fde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Gr=C3=B6ger?= Date: Tue, 16 Mar 2021 23:27:54 +0100 Subject: [PATCH] [pricelist] new api to add, delete and modify ingredients --- flaschengeist/plugins/pricelist/__init__.py | 23 ++++++ flaschengeist/plugins/pricelist/models.py | 18 ++--- .../plugins/pricelist/pricelist_controller.py | 80 ++++++++++++++++++- 3 files changed, 107 insertions(+), 14 deletions(-) diff --git a/flaschengeist/plugins/pricelist/__init__.py b/flaschengeist/plugins/pricelist/__init__.py index db001e5..3866abf 100644 --- a/flaschengeist/plugins/pricelist/__init__.py +++ b/flaschengeist/plugins/pricelist/__init__.py @@ -161,6 +161,12 @@ def set_volume(identifier): return jsonify(pricelist_controller.set_volume(identifier, data)) +@pricelist_bp.route("/volumes//ingredients", methods=["POST"]) +def set_ingredient(identifier): + data = request.get_json() + return jsonify(pricelist_controller.set_ingredient(data, identifier)) + + @pricelist_bp.route("/volumes/", methods=["PUT"]) def update_volume(identifier): data = request.get_json() @@ -171,3 +177,20 @@ def update_volume(identifier): def delete_volume(identifier): pricelist_controller.delete_volume(identifier) return "", NO_CONTENT + + +@pricelist_bp.route("/ingredients/extraIngredients", methods=["GET"]) +def get_extraIngredients(): + return jsonify(pricelist_controller.get_extra_ingredients()) + + +@pricelist_bp.route("/ingredients/", methods=["PUT"]) +def update_ingredient(identifier): + data = request.get_json() + return jsonify(pricelist_controller.update_ingredient(identifier, data)) + + +@pricelist_bp.route("/ingredients/", methods=["DELETE"]) +def delete_ingredient(identifier): + pricelist_controller.delete_ingredient(identifier) + return "", NO_CONTENT diff --git a/flaschengeist/plugins/pricelist/models.py b/flaschengeist/plugins/pricelist/models.py index c64d6c3..1308f29 100644 --- a/flaschengeist/plugins/pricelist/models.py +++ b/flaschengeist/plugins/pricelist/models.py @@ -75,7 +75,10 @@ class DrinkIngredient(db.Model, ModelSerializeMixin): @property def price(self): - return self.drink_ingredient.cost_price_pro_volume * self.volume + try: + return self.drink_ingredient.cost_price_pro_volume * self.volume + except AttributeError: + pass class Ingredient(db.Model, ModelSerializeMixin): @@ -101,21 +104,10 @@ class DrinkPriceVolume(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)) prices: [DrinkPrice] = db.relationship(DrinkPrice, back_populates="volume", cascade="all,delete,delete-orphan") - ingredients: [DrinkIngredient or ExtraIngredient] = [] - _ingredients: [Ingredient] = db.relationship("Ingredient", foreign_keys=Ingredient.volume_id) + ingredients: [Ingredient] = db.relationship("Ingredient", foreign_keys=Ingredient.volume_id) drink_id = db.Column(db.Integer, db.ForeignKey("drink.id"), nullable=False) - @property - def ingredients(self): - retVal = [] - for ingredient in self._ingredients: - if ingredient.drink_ingredient_id != None: - retVal.append(ingredient.drink_ingredient) - if ingredient.extra_ingredient_id != None: - retVal.append(ingredient.extra_ingredient) - return retVal - class Drink(db.Model, ModelSerializeMixin): """ diff --git a/flaschengeist/plugins/pricelist/pricelist_controller.py b/flaschengeist/plugins/pricelist/pricelist_controller.py index e5725b0..a8eb69c 100644 --- a/flaschengeist/plugins/pricelist/pricelist_controller.py +++ b/flaschengeist/plugins/pricelist/pricelist_controller.py @@ -160,7 +160,7 @@ def update_volume(identifier, data): ingredients = values.pop("ingredients") volume = get_volume(identifier) for key, value in values.items(): - setattr(volume, key, value) + setattr(volume, key, value if value != "" else None) db.session.commit() return volume @@ -214,3 +214,81 @@ def delete_price(identifier): price = get_price(identifier) db.session.delete(price) db.session.commit() + + +def set_drink_ingredient(data): + allowedKeys = DrinkIngredient().serialize().keys() + if "id" in data: + data.pop("id") + if "drink_ingredient" in data: + drink_ingredient_ = data.pop("drink_ingredient") + values = {key: value for key, value in data.items() if key in allowedKeys} + 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) + db.session.commit() + return drink_ingredient + + +def get_ingredient(identifier): + return Ingredient.query.get(identifier) + + +def set_ingredient(data, volume_id): + allowedKeys = Ingredient().serialize().keys() + if "id" in data: + data.pop("id") + values = {key: value for key, value in data.items() if key in allowedKeys} + if "drink_ingredient" in values: + drink_ingredient_values = values.get("drink_ingredient") + if "extra_ingredient" in values: + extra_ingredient_value = values.get("extra_ingredient") + + ingredient = Ingredient(**values) + volume = get_volume(volume_id) + if drink_ingredient_values: + ingredient.drink_ingredient = set_drink_ingredient(drink_ingredient_values) + if extra_ingredient_value: + if "id" in extra_ingredient_value: + ingredient.extra_ingredient = get_extra_ingredient(extra_ingredient_value.get("id")) + + volume.ingredients.append(ingredient) + db.session.add(ingredient) + db.session.commit() + return ingredient + + +def update_ingredient(identifier, data): + ingredient = get_ingredient(identifier) + 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 "volume" in data.get("drink_ingredient"): + ingredient.drink_ingredient.volume = data.get("drink_ingredient").get("volume") + db.session.commit() + return ingredient + + +def delete_ingredient(identifier): + ingredient = get_ingredient(identifier) + if ingredient.drink_ingredient: + db.session.delete(ingredient.drink_ingredient) + db.session.delete(ingredient) + db.session.commit() + + +def get_extra_ingredients(): + return ExtraIngredient.query.all() + + +def get_extra_ingredient(identifier): + return ExtraIngredient.query.get(identifier)