[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"))
if identifier == -1:
drink = Drink(**values)
db.session.add(drink)
else:
drink = get_drink(identifier) drink = get_drink(identifier)
if not drink:
raise NotFound
for key, value in values.items(): for key, value in values.items():
setattr(drink, key, value if value != "" else None) 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,41 +182,54 @@ 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")
id = None
if "id" in values:
id = values.pop("id")
volume = None
if id < 0:
volume = DrinkPriceVolume(**values) volume = DrinkPriceVolume(**values)
drink = get_drink(identifier)
if not drink:
raise BadRequest
drink.volumes.append(volume)
db.session.add(volume) db.session.add(volume)
db.session.commit() else:
return volume volume = get_volume(id)
if not volume:
raise NotFound
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(): for key, value in values.items():
setattr(volume, key, value if value != "" else None) setattr(volume, key, value if value != "" else None)
db.session.commit()
if prices:
set_prices(prices, volume)
if ingredients:
set_ingredients(ingredients, volume)
return volume 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): def delete_volume(identifier):
volume = get_volume(identifier) volume = get_volume(identifier)
db.session.delete(volume) db.session.delete(volume)
@ -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}
id = None
if "id" in values:
id = values.pop("id")
price = None
if id < 0:
price = DrinkPrice(**values) price = DrinkPrice(**values)
volume = get_volume(identifier)
if not volume:
raise BadRequest
volume.prices.append(price)
db.session.add(price) db.session.add(price)
db.session.commit() else:
return price price = get_price(id)
if not price:
raise NotFound
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(): for key, value in values.items():
setattr(price, key, value) 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:
id = values.pop("id")
if id < 0:
drink_ingredient = DrinkIngredient(**values) drink_ingredient = DrinkIngredient(**values)
if drink:
drink_ingredient.drink_ingredient = drink
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