From e31c5756a63d1e6192a5250a7b16b1518182cca5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Gr=C3=B6ger?= Date: Sat, 20 Mar 2021 15:01:18 +0100 Subject: [PATCH] [pricelist][break] some cleanup, removed unneccessary api --- flaschengeist/models/__init__.py | 7 +- flaschengeist/plugins/pricelist/__init__.py | 46 ---- flaschengeist/plugins/pricelist/models.py | 2 +- .../plugins/pricelist/pricelist_controller.py | 215 +++++++++--------- 4 files changed, 112 insertions(+), 158 deletions(-) diff --git a/flaschengeist/models/__init__.py b/flaschengeist/models/__init__.py index 27431a0..5cee5bf 100644 --- a/flaschengeist/models/__init__.py +++ b/flaschengeist/models/__init__.py @@ -14,8 +14,13 @@ class ModelSerializeMixin: return False import typing + hint = typing.get_type_hints(self.__class__)[param] - if typing.get_origin(hint) is typing.Union and len(typing.get_args(hint)) == 2 and typing.get_args(hint)[1] is type(None): + if ( + typing.get_origin(hint) is typing.Union + and len(typing.get_args(hint)) == 2 + and typing.get_args(hint)[1] is type(None) + ): return getattr(self, param) is None def serialize(self): diff --git a/flaschengeist/plugins/pricelist/__init__.py b/flaschengeist/plugins/pricelist/__init__.py index de9eb35..cc3ec44 100644 --- a/flaschengeist/plugins/pricelist/__init__.py +++ b/flaschengeist/plugins/pricelist/__init__.py @@ -138,52 +138,12 @@ def delete_drink(identifier): return "", NO_CONTENT -@pricelist_bp.route("/prices", methods=["GET"]) -@pricelist_bp.route("/prices/", methods=["GET"]) -def get_prices(identifier=None): - if identifier: - result = pricelist_controller.get_price(identifier) - else: - result = pricelist_controller.get_prices() - return jsonify(result) - - -@pricelist_bp.route("/volumes//prices", methods=["POST"]) -def create_price(identifier): - data = request.get_json() - return jsonify(pricelist_controller.set_price(identifier, data)) - - -@pricelist_bp.route("/prices/", methods=["PUT"]) -def modify_price(identifier): - data = request.get_json() - return jsonify(pricelist_controller.update_price(identifier, data)) - - @pricelist_bp.route("/prices/", methods=["DELETE"]) def delete_price(identifier): pricelist_controller.delete_price(identifier) 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//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() - return jsonify(pricelist_controller.update_volume(identifier, data)) - - @pricelist_bp.route("/volumes/", methods=["DELETE"]) def delete_volume(identifier): pricelist_controller.delete_volume(identifier) @@ -195,12 +155,6 @@ 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) diff --git a/flaschengeist/plugins/pricelist/models.py b/flaschengeist/plugins/pricelist/models.py index 703a792..b98acba 100644 --- a/flaschengeist/plugins/pricelist/models.py +++ b/flaschengeist/plugins/pricelist/models.py @@ -114,7 +114,7 @@ class DrinkPriceVolume(db.Model, ModelSerializeMixin): __tablename__ = "drink_price_volume" id: int = db.Column("id", db.Integer, primary_key=True) - drink_id = db.Column(db.Integer, db.ForeignKey("drink.id"), nullable=False) + drink_id = db.Column(db.Integer, db.ForeignKey("drink.id")) volume: float = db.Column(db.Numeric(precision=5, scale=2, asdecimal=False)) min_prices: list[MinPrices] = [] # ingredients: list[Ingredient] = [] diff --git a/flaschengeist/plugins/pricelist/pricelist_controller.py b/flaschengeist/plugins/pricelist/pricelist_controller.py index 8eceec9..1544d97 100644 --- a/flaschengeist/plugins/pricelist/pricelist_controller.py +++ b/flaschengeist/plugins/pricelist/pricelist_controller.py @@ -121,24 +121,7 @@ def get_drink(identifier): def set_drink(data): - allowedKeys = Drink().serialize().keys() - if "id" in data: - data.pop("id") - 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: - 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 + return update_drink(-1, data) def update_drink(identifier, data): @@ -146,23 +129,43 @@ def update_drink(identifier, data): if "id" in data: data.pop("id") values = {key: value for key, value in data.items() if key in allowedKeys} + volumes = None if "volumes" in values: - values.pop("volumes") + volumes = values.pop("volumes") if "tags" in values: values.pop("tags") + type = None 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 identifier == -1: + drink = Drink(**values) + db.session.add(drink) + else: + drink = get_drink(identifier) + if not drink: + raise NotFound + for key, value in values.items(): + setattr(drink, key, value if value != "" else None) + if type: drink.type = type + if volumes: + set_volumes(volumes, drink) db.session.commit() return drink +def set_volumes(volumes, drink): + if isinstance(volumes, list): + _volumes = [] + for _volume in volumes: + volume = set_volume(_volume) + _volumes.append(volume) + drink.volumes = _volumes + + def delete_drink(identifier): drink = get_drink(identifier) db.session.delete(drink) @@ -179,39 +182,52 @@ def get_volumes(drink_id=None): return DrinkPriceVolume.query.all() -def set_volume(identifier, data): +def set_volume(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} + prices = None + ingredients = None if "prices" in values: prices = values.pop("prices") if "ingredients" in values: ingredients = values.pop("ingredients") - volume = DrinkPriceVolume(**values) - drink = get_drink(identifier) - if not drink: - raise BadRequest - drink.volumes.append(volume) - db.session.add(volume) - db.session.commit() + id = None + if "id" in values: + id = values.pop("id") + volume = None + if id < 0: + volume = DrinkPriceVolume(**values) + db.session.add(volume) + else: + volume = get_volume(id) + if not volume: + raise NotFound + for key, value in values.items(): + setattr(volume, key, value if value != "" else None) + + if prices: + set_prices(prices, volume) + if ingredients: + set_ingredients(ingredients, volume) 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 if value != "" else None) - db.session.commit() - return volume +def set_prices(prices, volume): + if isinstance(prices, list): + _prices = [] + for _price in prices: + price = set_price(_price) + _prices.append(price) + volume.prices = _prices + + +def set_ingredients(ingredients, volume): + if isinstance(ingredients, list): + _ingredietns = [] + for _ingredient in ingredients: + ingredient = set_ingredient(_ingredient) + _ingredietns.append(ingredient) + volume.ingredients = _ingredietns def delete_volume(identifier): @@ -232,30 +248,23 @@ def get_prices(volume_id=None): return DrinkPrice.query.all() -def set_price(identifier, data): +def set_price(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) - if not volume: - raise BadRequest - volume.prices.append(price) - db.session.add(price) - db.session.commit() - return price + id = None + if "id" in values: + id = values.pop("id") + price = None + if id < 0: + price = DrinkPrice(**values) + db.session.add(price) + else: + price = get_price(id) + if not price: + raise NotFound + for key, value in values.items(): + setattr(price, key, value) - -def update_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 = get_price(identifier) - for key, value in values.items(): - setattr(price, key, value) - db.session.commit() return price @@ -267,21 +276,20 @@ def delete_price(identifier): def set_drink_ingredient(data): allowedKeys = DrinkIngredient().serialize().keys() - if "id" in data: - data.pop("id") drink = None - if "drink_ingredient" in data: - drink_ingredient_ = data.pop("drink_ingredient") - if "id" in drink_ingredient_: - drink = get_drink(drink_ingredient_.get("id")) values = {key: value for key, value in data.items() if key in allowedKeys} - if "price" in values: - values.pop("price") - drink_ingredient = DrinkIngredient(**values) - if drink: - drink_ingredient.drink_ingredient = drink - db.session.add(drink_ingredient) - db.session.commit() + id = None + if "id" in values: + id = values.pop("id") + if id < 0: + drink_ingredient = DrinkIngredient(**values) + db.session.add(drink_ingredient) + else: + drink_ingredient = DrinkIngredient.query.get(id) + if not drink_ingredient: + raise NotFound + for key, value in values.items(): + setattr(drink_ingredient, key, value if value != "" else None) return drink_ingredient @@ -289,42 +297,29 @@ 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} +def set_ingredient(data): drink_ingredient_value = None extra_ingredient_value = None - if "drink_ingredient" in values: - drink_ingredient_value = values.pop("drink_ingredient") - if "extra_ingredient" in values: - extra_ingredient_value = values.pop("extra_ingredient") - ingredient = Ingredient(**values) - volume = get_volume(volume_id) + if "drink_ingredient" in data: + drink_ingredient_value = data.pop("drink_ingredient") + if "extra_ingredient" in data: + extra_ingredient_value = data.pop("extra_ingredient") + id = None + if "id" in data: + id = data.pop("id") + ingredient = None + if id < 0: + ingredient = Ingredient(**data) + db.session.add(ingredient) + else: + ingredient = get_ingredient(id) + if not ingredient: + raise NotFound if drink_ingredient_value: ingredient.drink_ingredient = set_drink_ingredient(drink_ingredient_value) 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: - if data.get("drink_ingredient").get("drink_ingredient_id") > -1: - ingredient.drink_ingredient.drink_ingredient_id = data.get("drink_ingredient").get("drink_ingredient_id") - if "volume" in data.get("drink_ingredient"): - ingredient.drink_ingredient.volume = data.get("drink_ingredient").get("volume") - db.session.commit() return ingredient @@ -362,7 +357,7 @@ def update_extra_ingredient(identifier, data): 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: + for key, value in values.items(): setattr(extra_ingredient, key, value) db.session.commit() return extra_ingredient