From 26e82f02d688f32ef09f42705866dd254cf82a45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Gr=C3=B6ger?= Date: Wed, 17 Mar 2021 22:49:54 +0100 Subject: [PATCH] [pricelist] add, modify and delete for extra ingredients --- flaschengeist/plugins/pricelist/__init__.py | 18 +++++++++++ .../plugins/pricelist/pricelist_controller.py | 32 ++++++++++++++++++- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/flaschengeist/plugins/pricelist/__init__.py b/flaschengeist/plugins/pricelist/__init__.py index 89967b9..e5a15ac 100644 --- a/flaschengeist/plugins/pricelist/__init__.py +++ b/flaschengeist/plugins/pricelist/__init__.py @@ -202,3 +202,21 @@ def update_ingredient(identifier): def delete_ingredient(identifier): pricelist_controller.delete_ingredient(identifier) return "", NO_CONTENT + + +@pricelist_bp.route("/ingredients/extraIngredients", methods=["POST"]) +def set_extra_ingredient(): + data = request.get_json() + return jsonify(pricelist_controller.set_extra_ingredient(data)) + + +@pricelist_bp.route("/ingredients/extraIngredients/", methods=["PUT"]) +def update_extra_ingredient(identifier): + data = request.get_json() + return jsonify(pricelist_controller.update_extra_ingredient(identifier, data)) + + +@pricelist_bp.route("/ingredients/extraIngredients/", methods=["DELETE"]) +def delete_extra_ingredient(identifier): + pricelist_controller.delete_extra_ingredient(identifier) + return "", NO_CONTENT diff --git a/flaschengeist/plugins/pricelist/pricelist_controller.py b/flaschengeist/plugins/pricelist/pricelist_controller.py index 03bf389..d688178 100644 --- a/flaschengeist/plugins/pricelist/pricelist_controller.py +++ b/flaschengeist/plugins/pricelist/pricelist_controller.py @@ -124,7 +124,7 @@ def set_drink(data): allowedKeys = Drink().serialize().keys() if "id" in data: data.pop("id") - values = {key: value for key, value in data.items() if key in allowedKeys} + values = {key: value if value != "" else None for key, value in data.items() if key in allowedKeys} if "volumes" in values: values.pop("volumes") if "tags" in values: @@ -341,3 +341,33 @@ def get_extra_ingredients(): def get_extra_ingredient(identifier): return ExtraIngredient.query.get(identifier) + + +def set_extra_ingredient(data): + allowedKeys = ExtraIngredient().serialize().keys() + if "id" in data: + data.pop("id") + values = {key: value for key, value in data.items() if key in allowedKeys} + extra_ingredient = ExtraIngredient(**values) + db.session.add(extra_ingredient) + db.session.commit() + return extra_ingredient + + +def update_extra_ingredient(identifier, data): + allowedKeys = ExtraIngredient().serialize().keys() + if "id" in data: + data.pop("id") + values = {key: value for key, value in data.items() if key in allowedKeys} + extra_ingredient = get_extra_ingredient(identifier) + if extra_ingredient: + for key, value in data.items: + setattr(extra_ingredient, key, value) + db.session.commit() + return extra_ingredient + + +def delete_extra_ingredient(identifier): + extra_ingredient = get_extra_ingredient(identifier) + db.session.delete(extra_ingredient) + db.session.commit()