[pricelist][break] some cleanup, removed unneccessary api

This commit is contained in:
Tim Gröger 2021-03-20 15:01:18 +01:00
parent e16f4704b5
commit e31c5756a6
4 changed files with 112 additions and 158 deletions

View File

@ -14,8 +14,13 @@ class ModelSerializeMixin:
return False return False
import typing import typing
hint = typing.get_type_hints(self.__class__)[param] 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 return getattr(self, param) is None
def serialize(self): def serialize(self):

View File

@ -138,52 +138,12 @@ def delete_drink(identifier):
return "", NO_CONTENT return "", NO_CONTENT
@pricelist_bp.route("/prices", methods=["GET"])
@pricelist_bp.route("/prices/<int:identifier>", 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/<int:identifier>/prices", methods=["POST"])
def create_price(identifier):
data = request.get_json()
return jsonify(pricelist_controller.set_price(identifier, data))
@pricelist_bp.route("/prices/<int:identifier>", methods=["PUT"])
def modify_price(identifier):
data = request.get_json()
return jsonify(pricelist_controller.update_price(identifier, data))
@pricelist_bp.route("/prices/<int:identifier>", methods=["DELETE"]) @pricelist_bp.route("/prices/<int:identifier>", methods=["DELETE"])
def delete_price(identifier): def delete_price(identifier):
pricelist_controller.delete_price(identifier) pricelist_controller.delete_price(identifier)
return "", NO_CONTENT return "", NO_CONTENT
@pricelist_bp.route("/drinks/<int:identifier>/volumes", methods=["POST"])
def set_volume(identifier):
data = request.get_json()
return jsonify(pricelist_controller.set_volume(identifier, data))
@pricelist_bp.route("/volumes/<int:identifier>/ingredients", methods=["POST"])
def set_ingredient(identifier):
data = request.get_json()
return jsonify(pricelist_controller.set_ingredient(data, identifier))
@pricelist_bp.route("/volumes/<int:identifier>", methods=["PUT"])
def update_volume(identifier):
data = request.get_json()
return jsonify(pricelist_controller.update_volume(identifier, data))
@pricelist_bp.route("/volumes/<int:identifier>", methods=["DELETE"]) @pricelist_bp.route("/volumes/<int:identifier>", methods=["DELETE"])
def delete_volume(identifier): def delete_volume(identifier):
pricelist_controller.delete_volume(identifier) pricelist_controller.delete_volume(identifier)
@ -195,12 +155,6 @@ def get_extraIngredients():
return jsonify(pricelist_controller.get_extra_ingredients()) return jsonify(pricelist_controller.get_extra_ingredients())
@pricelist_bp.route("/ingredients/<int:identifier>", methods=["PUT"])
def update_ingredient(identifier):
data = request.get_json()
return jsonify(pricelist_controller.update_ingredient(identifier, data))
@pricelist_bp.route("/ingredients/<int:identifier>", methods=["DELETE"]) @pricelist_bp.route("/ingredients/<int:identifier>", methods=["DELETE"])
def delete_ingredient(identifier): def delete_ingredient(identifier):
pricelist_controller.delete_ingredient(identifier) pricelist_controller.delete_ingredient(identifier)

View File

@ -114,7 +114,7 @@ class DrinkPriceVolume(db.Model, ModelSerializeMixin):
__tablename__ = "drink_price_volume" __tablename__ = "drink_price_volume"
id: int = db.Column("id", db.Integer, primary_key=True) 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)) volume: float = db.Column(db.Numeric(precision=5, scale=2, asdecimal=False))
min_prices: list[MinPrices] = [] min_prices: list[MinPrices] = []
# ingredients: list[Ingredient] = [] # ingredients: list[Ingredient] = []

View File

@ -121,24 +121,7 @@ def get_drink(identifier):
def set_drink(data): def set_drink(data):
allowedKeys = Drink().serialize().keys() return update_drink(-1, data)
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
def update_drink(identifier, data): def update_drink(identifier, data):
@ -146,23 +129,43 @@ def update_drink(identifier, data):
if "id" in data: if "id" in data:
data.pop("id") data.pop("id")
values = {key: value for key, value in data.items() if key in allowedKeys} values = {key: value for key, value in data.items() if key in allowedKeys}
volumes = None
if "volumes" in values: if "volumes" in values:
values.pop("volumes") volumes = values.pop("volumes")
if "tags" in values: if "tags" in values:
values.pop("tags") values.pop("tags")
type = None
if "type" in values: if "type" in values:
_type = values.pop("type") _type = values.pop("type")
if isinstance(_type, dict) and "id" in _type: if isinstance(_type, dict) and "id" in _type:
type = get_drink_type(_type.get("id")) type = get_drink_type(_type.get("id"))
drink = get_drink(identifier) if identifier == -1:
for key, value in values.items(): drink = Drink(**values)
setattr(drink, key, value if value != "" else None) 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: if type:
drink.type = type drink.type = type
if volumes:
set_volumes(volumes, drink)
db.session.commit() db.session.commit()
return drink 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): def delete_drink(identifier):
drink = get_drink(identifier) drink = get_drink(identifier)
db.session.delete(drink) db.session.delete(drink)
@ -179,39 +182,52 @@ def get_volumes(drink_id=None):
return DrinkPriceVolume.query.all() return DrinkPriceVolume.query.all()
def set_volume(identifier, data): def set_volume(data):
allowed_keys = DrinkPriceVolume().serialize().keys() 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} values = {key: value for key, value in data.items() if key in allowed_keys}
prices = None
ingredients = None
if "prices" in values: if "prices" in values:
prices = values.pop("prices") prices = values.pop("prices")
if "ingredients" in values: if "ingredients" in values:
ingredients = values.pop("ingredients") ingredients = values.pop("ingredients")
volume = DrinkPriceVolume(**values) id = None
drink = get_drink(identifier) if "id" in values:
if not drink: id = values.pop("id")
raise BadRequest volume = None
drink.volumes.append(volume) if id < 0:
db.session.add(volume) volume = DrinkPriceVolume(**values)
db.session.commit() 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 return volume
def update_volume(identifier, data): def set_prices(prices, volume):
allowed_keys = DrinkPriceVolume().serialize().keys() if isinstance(prices, list):
if "id" in data: _prices = []
data.pop("id") for _price in prices:
values = {key: value for key, value in data.items() if key in allowed_keys} price = set_price(_price)
if "prices" in values: _prices.append(price)
prices = values.pop("prices") volume.prices = _prices
if "ingredients" in values:
ingredients = values.pop("ingredients")
volume = get_volume(identifier) def set_ingredients(ingredients, volume):
for key, value in values.items(): if isinstance(ingredients, list):
setattr(volume, key, value if value != "" else None) _ingredietns = []
db.session.commit() for _ingredient in ingredients:
return volume ingredient = set_ingredient(_ingredient)
_ingredietns.append(ingredient)
volume.ingredients = _ingredietns
def delete_volume(identifier): def delete_volume(identifier):
@ -232,30 +248,23 @@ def get_prices(volume_id=None):
return DrinkPrice.query.all() return DrinkPrice.query.all()
def set_price(identifier, data): def set_price(data):
allowed_keys = DrinkPrice().serialize().keys() 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} values = {key: value for key, value in data.items() if key in allowed_keys}
price = DrinkPrice(**values) id = None
volume = get_volume(identifier) if "id" in values:
if not volume: id = values.pop("id")
raise BadRequest price = None
volume.prices.append(price) if id < 0:
db.session.add(price) price = DrinkPrice(**values)
db.session.commit() db.session.add(price)
return 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 return price
@ -267,21 +276,20 @@ def delete_price(identifier):
def set_drink_ingredient(data): def set_drink_ingredient(data):
allowedKeys = DrinkIngredient().serialize().keys() allowedKeys = DrinkIngredient().serialize().keys()
if "id" in data:
data.pop("id")
drink = None 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} values = {key: value for key, value in data.items() if key in allowedKeys}
if "price" in values: id = None
values.pop("price") if "id" in values:
drink_ingredient = DrinkIngredient(**values) id = values.pop("id")
if drink: if id < 0:
drink_ingredient.drink_ingredient = drink drink_ingredient = DrinkIngredient(**values)
db.session.add(drink_ingredient) db.session.add(drink_ingredient)
db.session.commit() 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 return drink_ingredient
@ -289,42 +297,29 @@ def get_ingredient(identifier):
return Ingredient.query.get(identifier) return Ingredient.query.get(identifier)
def set_ingredient(data, volume_id): def set_ingredient(data):
allowedKeys = Ingredient().serialize().keys()
if "id" in data:
data.pop("id")
values = {key: value for key, value in data.items() if key in allowedKeys}
drink_ingredient_value = None drink_ingredient_value = None
extra_ingredient_value = None extra_ingredient_value = None
if "drink_ingredient" in values: if "drink_ingredient" in data:
drink_ingredient_value = values.pop("drink_ingredient") drink_ingredient_value = data.pop("drink_ingredient")
if "extra_ingredient" in values: if "extra_ingredient" in data:
extra_ingredient_value = values.pop("extra_ingredient") extra_ingredient_value = data.pop("extra_ingredient")
ingredient = Ingredient(**values) id = None
volume = get_volume(volume_id) 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: if drink_ingredient_value:
ingredient.drink_ingredient = set_drink_ingredient(drink_ingredient_value) ingredient.drink_ingredient = set_drink_ingredient(drink_ingredient_value)
if extra_ingredient_value: if extra_ingredient_value:
if "id" in extra_ingredient_value: if "id" in extra_ingredient_value:
ingredient.extra_ingredient = get_extra_ingredient(extra_ingredient_value.get("id")) 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 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} values = {key: value for key, value in data.items() if key in allowedKeys}
extra_ingredient = get_extra_ingredient(identifier) extra_ingredient = get_extra_ingredient(identifier)
if extra_ingredient: if extra_ingredient:
for key, value in data.items: for key, value in values.items():
setattr(extra_ingredient, key, value) setattr(extra_ingredient, key, value)
db.session.commit() db.session.commit()
return extra_ingredient return extra_ingredient