[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) id: int = db.Column("id", db.Integer, primary_key=True)
volume: float = db.Column(db.Numeric(precision=5, scale=2, asdecimal=False), nullable=False) 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_id: int = db.Column("drink_ingredient_id", db.Integer, db.ForeignKey("drink.id"))
drink_ingredient = db.relationship("Drink") # drink_ingredient: Drink = db.relationship("Drink")
price: float = 0 # price: float = 0
@property
def price(self): # @property
try: # def price(self):
return self.drink_ingredient.cost_price_pro_volume * self.volume # try:
except AttributeError: # return self.drink_ingredient.cost_price_pro_volume * self.volume
pass # except AttributeError:
# pass
class Ingredient(db.Model, ModelSerializeMixin): class Ingredient(db.Model, ModelSerializeMixin):
@ -90,11 +91,20 @@ class Ingredient(db.Model, ModelSerializeMixin):
__tablename__ = "ingredient_association" __tablename__ = "ingredient_association"
id: int = db.Column("id", db.Integer, primary_key=True) 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_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_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): class DrinkPriceVolume(db.Model, ModelSerializeMixin):
@ -106,11 +116,11 @@ class DrinkPriceVolume(db.Model, ModelSerializeMixin):
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"), nullable=False)
volume: float = db.Column(db.Numeric(precision=5, scale=2, asdecimal=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") 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): 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")) _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") tags: Optional[list[Tag]] = db.relationship("Tag", secondary=drink_tag_association, cascade="save-update, merge")
type: DrinkType = db.relationship("DrinkType", foreign_keys=[_type_id]) type: Optional[DrinkType] = db.relationship("DrinkType", foreign_keys=[_type_id])
volumes: list[DrinkPriceVolume] = db.relationship(DrinkPriceVolume) volumes: list[DrinkPriceVolume] = db.relationship(DrinkPriceVolume)

View File

@ -269,12 +269,15 @@ def set_drink_ingredient(data):
allowedKeys = DrinkIngredient().serialize().keys() allowedKeys = DrinkIngredient().serialize().keys()
if "id" in data: if "id" in data:
data.pop("id") data.pop("id")
drink = None
if "drink_ingredient" in data: if "drink_ingredient" in data:
drink_ingredient_ = data.pop("drink_ingredient") 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_: if "id" in drink_ingredient_:
drink = get_drink(drink_ingredient_.get("id")) 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: if drink:
drink_ingredient.drink_ingredient = drink drink_ingredient.drink_ingredient = drink
db.session.add(drink_ingredient) db.session.add(drink_ingredient)
@ -291,15 +294,16 @@ def set_ingredient(data, volume_id):
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}
drink_ingredient_value = None
extra_ingredient_value = None
if "drink_ingredient" in values: if "drink_ingredient" in values:
drink_ingredient_values = values.get("drink_ingredient") drink_ingredient_value = values.pop("drink_ingredient")
if "extra_ingredient" in values: if "extra_ingredient" in values:
extra_ingredient_value = values.get("extra_ingredient") extra_ingredient_value = values.pop("extra_ingredient")
ingredient = Ingredient(**values) ingredient = Ingredient(**values)
volume = get_volume(volume_id) volume = get_volume(volume_id)
if drink_ingredient_values: if drink_ingredient_value:
ingredient.drink_ingredient = set_drink_ingredient(drink_ingredient_values) 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"))
@ -315,12 +319,9 @@ def update_ingredient(identifier, data):
if "extra_ingredient" in data and isinstance(data.get("extra_ingredient"), dict): if "extra_ingredient" in data and isinstance(data.get("extra_ingredient"), dict):
if "id" in data.get("extra_ingredient"): if "id" in data.get("extra_ingredient"):
ingredient.extra_ingredient = get_extra_ingredient(data.get("extra_ingredient").get("id")) 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 and ingredient.drink_ingredient:
if "drink_ingredient" in data.get("drink_ingredient"): if data.get("drink_ingredient").get("drink_ingredient_id") > -1:
if "id" in data.get("drink_ingredient").get("drink_ingredient"): ingredient.drink_ingredient.drink_ingredient_id = data.get("drink_ingredient").get("drink_ingredient_id")
ingredient.drink_ingredient.drink_ingredient = get_drink(
data.get("drink_ingredient").get("drink_ingredient").get("id")
)
if "volume" in data.get("drink_ingredient"): if "volume" in data.get("drink_ingredient"):
ingredient.drink_ingredient.volume = data.get("drink_ingredient").get("volume") ingredient.drink_ingredient.volume = data.get("drink_ingredient").get("volume")
db.session.commit() db.session.commit()