From 642d95b2a5c6fbeac19d185d2679f94caccf9ae1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Gr=C3=B6ger?= Date: Wed, 17 Mar 2021 21:36:51 +0100 Subject: [PATCH] [pricelist] finish drinks, can add, modify and delete --- flaschengeist/plugins/pricelist/__init__.py | 18 +++++-- .../plugins/pricelist/pricelist_controller.py | 49 +++++++++++++++++++ 2 files changed, 62 insertions(+), 5 deletions(-) diff --git a/flaschengeist/plugins/pricelist/__init__.py b/flaschengeist/plugins/pricelist/__init__.py index 3866abf..89967b9 100644 --- a/flaschengeist/plugins/pricelist/__init__.py +++ b/flaschengeist/plugins/pricelist/__init__.py @@ -120,11 +120,19 @@ def search_drinks(name): @login_required(permission=permissions.CREATE) def create_drink(current_session): data = request.get_json() - if not all(item in data for item in ["name", "volume", "cost_price"]) or not all( - item in data for item in ["name", "ingredients"] - ): - raise BadRequest("No correct Keys to create drink") - return "jsonify(pricelist_controller.create_drink(data))" + return jsonify(pricelist_controller.set_drink(data)) + + +@pricelist_bp.route("/drinks/", methods=["PUT"]) +def update_drink(identifier): + data = request.get_json() + return jsonify(pricelist_controller.update_drink(identifier, data)) + + +@pricelist_bp.route("/drinks/", methods=["DELETE"]) +def delete_drink(identifier): + pricelist_controller.delete_drink(identifier) + return "", NO_CONTENT @pricelist_bp.route("/prices", methods=["GET"]) diff --git a/flaschengeist/plugins/pricelist/pricelist_controller.py b/flaschengeist/plugins/pricelist/pricelist_controller.py index a8eb69c..03bf389 100644 --- a/flaschengeist/plugins/pricelist/pricelist_controller.py +++ b/flaschengeist/plugins/pricelist/pricelist_controller.py @@ -120,6 +120,55 @@ def get_drink(identifier): raise NotFound +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} + if "volumes" in values: + values.pop("volumes") + if "tags" in values: + values.pop("tags") + if "type" in values: + _type = values.pop("type") + if isinstance(_type, dict) and "id" in _type: + type = get_drink_type(_type.get("id")) + drink = Drink(**values) + if type: + drink.type = type + db.session.add(drink) + db.session.commit() + return drink + + +def update_drink(identifier, 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} + if "volumes" in values: + values.pop("volumes") + if "tags" in values: + values.pop("tags") + if "type" in values: + _type = values.pop("type") + if isinstance(_type, dict) and "id" in _type: + type = get_drink_type(_type.get("id")) + drink = get_drink(identifier) + for key, value in values.items(): + setattr(drink, key, value if value != "" else None) + if type: + drink.type = type + db.session.commit() + return drink + + +def delete_drink(identifier): + drink = get_drink(identifier) + db.session.delete(drink) + db.session.commit() + + def get_volume(identifier): return DrinkPriceVolume.query.get(identifier)