[pricelist][fix] autodeletion of relationship. drinks can be modified

This commit is contained in:
Tim Gröger 2021-11-15 20:47:14 +01:00
parent 6a9db1b36a
commit f205291d6d
2 changed files with 15 additions and 43 deletions

View File

@ -99,7 +99,7 @@ class Ingredient(db.Model, ModelSerializeMixin):
__tablename__ = "drink_ingredient_association"
id: int = db.Column("id", Serial, primary_key=True)
volume_id = db.Column(Serial, db.ForeignKey("drink_price_volume.id"))
drink_ingredient: Optional[DrinkIngredient] = db.relationship(DrinkIngredient)
drink_ingredient: Optional[DrinkIngredient] = db.relationship(DrinkIngredient, cascade="all,delete")
extra_ingredient: Optional[ExtraIngredient] = db.relationship(ExtraIngredient)
_drink_ingredient_id = db.Column(Serial, db.ForeignKey("drink_ingredient.id"))
@ -132,10 +132,10 @@ class DrinkPriceVolume(db.Model, ModelSerializeMixin):
_prices: list[DrinkPrice] = db.relationship(
DrinkPrice, back_populates="_volume", cascade="all,delete,delete-orphan"
)
ingredients: list[Ingredient] = db.relationship("Ingredient", foreign_keys=Ingredient.volume_id)
ingredients: list[Ingredient] = db.relationship("Ingredient", foreign_keys=Ingredient.volume_id, cascade="all,delete,delete-orphan")
def __repr__(self):
return f"DrinkPriceVolume({self.id},{self.drink_id},{self.prices})"
return f"DrinkPriceVolume({self.id},{self.drink_id},{self.volume},{self.prices})"
class Drink(db.Model, ModelSerializeMixin):

View File

@ -1,9 +1,7 @@
from werkzeug.exceptions import BadRequest, NotFound
from sqlalchemy.exc import IntegrityError
from uuid import uuid4
from flaschengeist import logger
from flaschengeist.config import config
from flaschengeist.database import db
from flaschengeist.utils.decorators import extract_session
@ -357,16 +355,9 @@ def set_volume(data):
prices = values.pop("prices")
if "ingredients" in values:
ingredients = values.pop("ingredients")
vol_id = values.pop("id", None)
if vol_id < 0:
values.pop("id", None)
volume = DrinkPriceVolume(**values)
db.session.add(volume)
else:
volume = get_volume(vol_id)
if not volume:
raise NotFound
for key, value in values.items():
setattr(volume, key, value if value != "" else None)
if prices and session.user_.has_permission(EDIT_PRICE):
set_prices(prices, volume)
@ -416,16 +407,9 @@ def set_price(data):
allowed_keys.append("description")
logger.debug(f"allowed_key {allowed_keys}")
values = {key: value for key, value in data.items() if key in allowed_keys}
price_id = values.pop("id", -1)
if price_id < 0:
values.pop("id", -1)
price = DrinkPrice(**values)
db.session.add(price)
else:
price = get_price(price_id)
if not price:
raise NotFound
for key, value in values.items():
setattr(price, key, value)
return price
@ -443,16 +427,9 @@ def set_drink_ingredient(data):
values.pop("cost_per_volume")
if "name" in values:
values.pop("name")
ingredient_id = values.pop("id", -1)
if ingredient_id < 0:
values.pop("id", -1)
drink_ingredient = DrinkIngredient(**values)
db.session.add(drink_ingredient)
else:
drink_ingredient = DrinkIngredient.query.get(ingredient_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
@ -467,14 +444,9 @@ def set_ingredient(data):
drink_ingredient_value = data.pop("drink_ingredient")
if "extra_ingredient" in data:
extra_ingredient_value = data.pop("extra_ingredient")
ingredient_id = data.pop("id", -1)
if ingredient_id < 0:
data.pop("id", -1)
ingredient = Ingredient(**data)
db.session.add(ingredient)
else:
ingredient = get_ingredient(ingredient_id)
if not ingredient:
raise NotFound
if drink_ingredient_value:
ingredient.drink_ingredient = set_drink_ingredient(drink_ingredient_value)
if extra_ingredient_value: