[pricelist] fixed circula issue

This commit is contained in:
Tim Gröger 2021-03-18 17:26:02 +01:00
parent 87f9b0aa48
commit d5ba1f023e
2 changed files with 40 additions and 29 deletions

View File

@ -72,15 +72,16 @@ class DrinkIngredient(db.Model, ModelSerializeMixin):
id: int = db.Column("id", db.Integer, primary_key=True)
volume: float = db.Column(db.Numeric(precision=5, scale=2, asdecimal=False), nullable=False)
drink_ingredient_id: int = db.Column("drink_ingredient_id", db.Integer, db.ForeignKey("drink.id"))
drink_ingredient = db.relationship("Drink")
price: float = 0
# drink_ingredient: Drink = db.relationship("Drink")
# price: float = 0
@property
def price(self):
try:
return self.drink_ingredient.cost_price_pro_volume * self.volume
except AttributeError:
pass
# @property
# def price(self):
# try:
# return self.drink_ingredient.cost_price_pro_volume * self.volume
# except AttributeError:
# pass
class Ingredient(db.Model, ModelSerializeMixin):
@ -90,11 +91,20 @@ class Ingredient(db.Model, ModelSerializeMixin):
__tablename__ = "ingredient_association"
id: int = db.Column("id", db.Integer, primary_key=True)
volume_id: int = db.Column(db.Integer, db.ForeignKey("drink_price_volume.id"))
volume_id = db.Column(db.Integer, db.ForeignKey("drink_price_volume.id"))
drink_ingredient_id = db.Column(db.Integer, db.ForeignKey("drink_ingredient.id"))
drink_ingredient: DrinkIngredient = db.relationship(DrinkIngredient)
drink_ingredient: Optional[DrinkIngredient] = db.relationship(DrinkIngredient)
extra_ingredient_id = db.Column(db.Integer, db.ForeignKey("extra_ingredient.id"))
extra_ingredient: ExtraIngredient = db.relationship(ExtraIngredient)
extra_ingredient: Optional[ExtraIngredient] = db.relationship(ExtraIngredient)
class MinPrices(ModelSerializeMixin):
"""
MinPrices
"""
percentage: float
price: float
class DrinkPriceVolume(db.Model, ModelSerializeMixin):
@ -106,11 +116,11 @@ class DrinkPriceVolume(db.Model, ModelSerializeMixin):
id: int = db.Column("id", db.Integer, primary_key=True)
drink_id = db.Column(db.Integer, db.ForeignKey("drink.id"), nullable=False)
volume: float = db.Column(db.Numeric(precision=5, scale=2, asdecimal=False))
ingredients: list[DrinkIngredient, ExtraIngredient] = []
min_prices: list[MinPrices] = []
# ingredients: list[Ingredient] = []
prices: list[DrinkPrice] = db.relationship(DrinkPrice, back_populates="volume", cascade="all,delete,delete-orphan")
# TODO: Really protected or just not exported (e.g. name_)?
_ingredients: list[Ingredient] = db.relationship("Ingredient", foreign_keys=Ingredient.volume_id)
ingredients: list[Ingredient] = db.relationship("Ingredient", foreign_keys=Ingredient.volume_id)
class Drink(db.Model, ModelSerializeMixin):
@ -129,6 +139,6 @@ class Drink(db.Model, ModelSerializeMixin):
_type_id = db.Column("type_id", db.Integer, db.ForeignKey("drink_type.id"))
tags: list[Tag] = db.relationship("Tag", secondary=drink_tag_association, cascade="save-update, merge")
type: DrinkType = db.relationship("DrinkType", foreign_keys=[_type_id])
tags: Optional[list[Tag]] = db.relationship("Tag", secondary=drink_tag_association, cascade="save-update, merge")
type: Optional[DrinkType] = db.relationship("DrinkType", foreign_keys=[_type_id])
volumes: list[DrinkPriceVolume] = db.relationship(DrinkPriceVolume)

View File

@ -269,12 +269,15 @@ 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")
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"))
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)
@ -291,15 +294,16 @@ def set_ingredient(data, volume_id):
if "id" in data:
data.pop("id")
values = {key: value for key, value in data.items() if key in allowedKeys}
drink_ingredient_value = None
extra_ingredient_value = None
if "drink_ingredient" in values:
drink_ingredient_values = values.get("drink_ingredient")
drink_ingredient_value = values.pop("drink_ingredient")
if "extra_ingredient" in values:
extra_ingredient_value = values.get("extra_ingredient")
extra_ingredient_value = values.pop("extra_ingredient")
ingredient = Ingredient(**values)
volume = get_volume(volume_id)
if drink_ingredient_values:
ingredient.drink_ingredient = set_drink_ingredient(drink_ingredient_values)
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"))
@ -315,12 +319,9 @@ def update_ingredient(identifier, data):
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 "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()