From e26b7b8c962ff26948c230584aa80e6a5deeaa52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Gr=C3=B6ger?= Date: Mon, 15 Mar 2021 23:53:21 +0100 Subject: [PATCH] [pricelist] first step to add, modify and delete volumes --- flaschengeist/plugins/pricelist/__init__.py | 22 ++++++++- .../plugins/pricelist/pricelist_controller.py | 48 ++++++++++++++++++- 2 files changed, 67 insertions(+), 3 deletions(-) diff --git a/flaschengeist/plugins/pricelist/__init__.py b/flaschengeist/plugins/pricelist/__init__.py index 5df895b..db001e5 100644 --- a/flaschengeist/plugins/pricelist/__init__.py +++ b/flaschengeist/plugins/pricelist/__init__.py @@ -137,7 +137,7 @@ def get_prices(identifier=None): return jsonify(result) -@pricelist_bp.route("/prices/volumes/", methods=["POST"]) +@pricelist_bp.route("/volumes//prices", methods=["POST"]) def create_price(identifier): data = request.get_json() return jsonify(pricelist_controller.set_price(identifier, data)) @@ -152,4 +152,22 @@ def modify_price(identifier): @pricelist_bp.route("/prices/", methods=["DELETE"]) def delete_price(identifier): pricelist_controller.delete_price(identifier) - return NO_CONTENT + return "", NO_CONTENT + + +@pricelist_bp.route("/drinks//volumes", methods=["POST"]) +def set_volume(identifier): + data = request.get_json() + return jsonify(pricelist_controller.set_volume(identifier, data)) + + +@pricelist_bp.route("/volumes/", methods=["PUT"]) +def update_volume(identifier): + data = request.get_json() + return jsonify(pricelist_controller.update_volume(identifier, data)) + + +@pricelist_bp.route("/volumes/", methods=["DELETE"]) +def delete_volume(identifier): + pricelist_controller.delete_volume(identifier) + return "", NO_CONTENT diff --git a/flaschengeist/plugins/pricelist/pricelist_controller.py b/flaschengeist/plugins/pricelist/pricelist_controller.py index d308504..2e1b69f 100644 --- a/flaschengeist/plugins/pricelist/pricelist_controller.py +++ b/flaschengeist/plugins/pricelist/pricelist_controller.py @@ -124,6 +124,50 @@ def get_volume(identifier): return DrinkPriceVolume.query.get(identifier) +def get_volumes(drink_id=None): + if drink_id: + return DrinkPriceVolume.query.filter(DrinkPriceVolume.drink_id == drink_id).all() + return DrinkPriceVolume.query.all() + + +def set_volume(identifier, data): + allowed_keys = DrinkPriceVolume().serialize().keys() + if "id" in data: + data.pop("id") + values = {key: value for key, value in data.items() if key in allowed_keys} + + volume = DrinkPriceVolume(**values) + drink = get_drink(identifier) + if not drink: + raise BadRequest + drink.volumes.append(volume) + db.session.add(volume) + db.session.commit() + return volume + + +def update_volume(identifier, data): + allowed_keys = DrinkPriceVolume().serialize().keys() + if "id" in data: + data.pop("id") + values = {key: value for key, value in data.items() if key in allowed_keys} + if "prices" in values: + prices = values.pop("prices") + if "ingredients" in values: + ingredients = values.pop("ingredients") + volume = get_volume(identifier) + for key, value in values.items(): + setattr(volume, key, value) + db.session.commit() + return volume + + +def delete_volume(identifier): + volume = get_volume(identifier) + db.session.delete(volume) + db.session.commit() + + def get_price(identifier): if isinstance(identifier, int): return DrinkPrice.query.get(identifier) @@ -138,6 +182,8 @@ def get_prices(volume_id=None): def set_price(identifier, data): allowed_keys = DrinkPrice().serialize().keys() + if "id" in data: + data.pop("id") values = {key: value for key, value in data.items() if key in allowed_keys} price = DrinkPrice(**values) volume = get_volume(identifier) @@ -155,7 +201,7 @@ def update_price(identifier, data): data.pop("id") values = {key: value for key, value in data.items() if key in allowed_keys} price = get_price(identifier) - for key, value in values: + for key, value in values.items(): setattr(price, key, value) db.session.commit() return price