295 lines
8.4 KiB
Python
295 lines
8.4 KiB
Python
from werkzeug.exceptions import BadRequest, NotFound
|
|
from sqlalchemy.exc import IntegrityError
|
|
|
|
from flaschengeist import logger
|
|
from flaschengeist.database import db
|
|
from .models import Drink, DrinkPrice, Ingredient, Tag, DrinkType, DrinkPriceVolume, DrinkIngredient, ExtraIngredient
|
|
|
|
from math import ceil
|
|
|
|
|
|
def update():
|
|
db.session.commit()
|
|
|
|
|
|
def get_tags():
|
|
return Tag.query.all()
|
|
|
|
|
|
def get_tag(identifier):
|
|
if isinstance(identifier, int):
|
|
retVal = Tag.query.get(identifier)
|
|
elif isinstance(identifier, str):
|
|
retVal = Tag.query.filter(Tag.name == identifier).one_or_none()
|
|
else:
|
|
logger.debug("Invalid identifier type for Tag")
|
|
raise BadRequest
|
|
if not retVal:
|
|
raise NotFound
|
|
return retVal
|
|
|
|
|
|
def create_tag(name):
|
|
try:
|
|
tag = Tag(name=name)
|
|
db.session.add(tag)
|
|
update()
|
|
return tag
|
|
except IntegrityError:
|
|
raise BadRequest("Name already exists")
|
|
|
|
|
|
def rename_tag(identifier, new_name):
|
|
tag = get_tag(identifier)
|
|
tag.name = new_name
|
|
try:
|
|
update()
|
|
except IntegrityError:
|
|
raise BadRequest("Name already exists")
|
|
|
|
|
|
def delete_tag(identifier):
|
|
tag = get_tag(identifier)
|
|
db.session.delete(tag)
|
|
try:
|
|
update()
|
|
except IntegrityError:
|
|
raise BadRequest("Tag still in use")
|
|
|
|
|
|
def get_drink_types():
|
|
return DrinkType.query.all()
|
|
|
|
|
|
def get_drink_type(identifier):
|
|
if isinstance(identifier, int):
|
|
retVal = DrinkType.query.get(identifier)
|
|
elif isinstance(identifier, str):
|
|
retVal = DrinkType.query.filter(Tag.name == identifier).one_or_none()
|
|
else:
|
|
logger.debug("Invalid identifier type for DrinkType")
|
|
raise BadRequest
|
|
if not retVal:
|
|
raise NotFound
|
|
return retVal
|
|
|
|
|
|
def create_drink_type(name):
|
|
try:
|
|
drinkType = DrinkType(name=name)
|
|
db.session.add(drinkType)
|
|
update()
|
|
return drinkType
|
|
except IntegrityError:
|
|
raise BadRequest("Name already exists")
|
|
|
|
|
|
def rename_drink_type(identifier, new_name):
|
|
drink_type = get_drink_type(identifier)
|
|
drink_type.name = new_name
|
|
try:
|
|
update()
|
|
except IntegrityError:
|
|
raise BadRequest("Name already exists")
|
|
return drink_type
|
|
|
|
|
|
def delete_drink_type(identifier):
|
|
drinkType = get_drink_type(identifier)
|
|
db.session.delete(drinkType)
|
|
try:
|
|
update()
|
|
except IntegrityError:
|
|
raise BadRequest("DrinkType still in use")
|
|
|
|
|
|
def get_drinks(name=None):
|
|
if name:
|
|
return Drink.query.filter(Drink.name.contains(name)).all()
|
|
return Drink.query.all()
|
|
|
|
|
|
def get_drink(identifier):
|
|
if isinstance(identifier, int):
|
|
return Drink.query.get(identifier)
|
|
elif isinstance(identifier, str):
|
|
return Drink.query.filter(Tag.name == identifier).one_or_none()
|
|
else:
|
|
logger.debug("Invalid identifier type for Drink")
|
|
raise BadRequest
|
|
raise NotFound
|
|
|
|
|
|
def get_volume(identifier):
|
|
return DrinkPriceVolume.query.get(identifier)
|
|
|
|
|
|
def get_volumes(drink_id=None):
|
|
if drink_id:
|
|
return DrinkPriceVolume.query.filter(DrinkPriceVolume.drink_id == drink_id).all()
|
|
return DrinkPriceVolume.query.all()
|
|
|
|
|
|
def set_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 = DrinkPriceVolume(**values)
|
|
drink = get_drink(identifier)
|
|
if not drink:
|
|
raise BadRequest
|
|
drink.volumes.append(volume)
|
|
db.session.add(volume)
|
|
db.session.commit()
|
|
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 delete_volume(identifier):
|
|
volume = get_volume(identifier)
|
|
db.session.delete(volume)
|
|
db.session.commit()
|
|
|
|
|
|
def get_price(identifier):
|
|
if isinstance(identifier, int):
|
|
return DrinkPrice.query.get(identifier)
|
|
raise NotFound
|
|
|
|
|
|
def get_prices(volume_id=None):
|
|
if volume_id:
|
|
return DrinkPrice.query.filter(DrinkPrice.volume_id_ == volume_id).all()
|
|
return DrinkPrice.query.all()
|
|
|
|
|
|
def set_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 = DrinkPrice(**values)
|
|
volume = get_volume(identifier)
|
|
if not volume:
|
|
raise BadRequest
|
|
volume.prices.append(price)
|
|
db.session.add(price)
|
|
db.session.commit()
|
|
return price
|
|
|
|
|
|
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
|
|
|
|
|
|
def delete_price(identifier):
|
|
price = get_price(identifier)
|
|
db.session.delete(price)
|
|
db.session.commit()
|
|
|
|
|
|
def set_drink_ingredient(data):
|
|
allowedKeys = DrinkIngredient().serialize().keys()
|
|
if "id" in data:
|
|
data.pop("id")
|
|
if "drink_ingredient" in data:
|
|
drink_ingredient_ = data.pop("drink_ingredient")
|
|
values = {key: value for key, value in data.items() if key in allowedKeys}
|
|
drink_ingredient = DrinkIngredient(**values)
|
|
if "id" in drink_ingredient_:
|
|
drink = get_drink(drink_ingredient_.get("id"))
|
|
if drink:
|
|
drink_ingredient.drink_ingredient = drink
|
|
db.session.add(drink_ingredient)
|
|
db.session.commit()
|
|
return drink_ingredient
|
|
|
|
|
|
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}
|
|
if "drink_ingredient" in values:
|
|
drink_ingredient_values = values.get("drink_ingredient")
|
|
if "extra_ingredient" in values:
|
|
extra_ingredient_value = values.get("extra_ingredient")
|
|
|
|
ingredient = Ingredient(**values)
|
|
volume = get_volume(volume_id)
|
|
if drink_ingredient_values:
|
|
ingredient.drink_ingredient = set_drink_ingredient(drink_ingredient_values)
|
|
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 and isinstance(data.get("drink_ingredient"), dict):
|
|
if "drink_ingredient" in data.get("drink_ingredient"):
|
|
if "id" in data.get("drink_ingredient").get("drink_ingredient"):
|
|
ingredient.drink_ingredient.drink_ingredient = get_drink(
|
|
data.get("drink_ingredient").get("drink_ingredient").get("id")
|
|
)
|
|
if "volume" in data.get("drink_ingredient"):
|
|
ingredient.drink_ingredient.volume = data.get("drink_ingredient").get("volume")
|
|
db.session.commit()
|
|
return ingredient
|
|
|
|
|
|
def delete_ingredient(identifier):
|
|
ingredient = get_ingredient(identifier)
|
|
if ingredient.drink_ingredient:
|
|
db.session.delete(ingredient.drink_ingredient)
|
|
db.session.delete(ingredient)
|
|
db.session.commit()
|
|
|
|
|
|
def get_extra_ingredients():
|
|
return ExtraIngredient.query.all()
|
|
|
|
|
|
def get_extra_ingredient(identifier):
|
|
return ExtraIngredient.query.get(identifier)
|