[pricelist] finish drinks, can add, modify and delete

This commit is contained in:
Tim Gröger 2021-03-17 21:36:51 +01:00
parent 5e0e5edf6f
commit 642d95b2a5
2 changed files with 62 additions and 5 deletions

View File

@ -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/<int:identifier>", methods=["PUT"])
def update_drink(identifier):
data = request.get_json()
return jsonify(pricelist_controller.update_drink(identifier, data))
@pricelist_bp.route("/drinks/<int:identifier>", methods=["DELETE"])
def delete_drink(identifier):
pricelist_controller.delete_drink(identifier)
return "", NO_CONTENT
@pricelist_bp.route("/prices", methods=["GET"])

View File

@ -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)