166 lines
5.4 KiB
Python
166 lines
5.4 KiB
Python
from __future__ import annotations # TODO: Remove if python requirement is >= 3.10
|
|
|
|
from flaschengeist.database import db
|
|
from flaschengeist.models import ModelSerializeMixin
|
|
|
|
from typing import Optional
|
|
|
|
drink_tag_association = db.Table(
|
|
"drink_x_tag",
|
|
db.Column("drink_id", db.Integer, db.ForeignKey("drink.id")),
|
|
db.Column("tag_id", db.Integer, db.ForeignKey("drink_tag.id")),
|
|
)
|
|
|
|
drink_type_association = db.Table(
|
|
"drink_x_type",
|
|
db.Column("drink_id", db.Integer, db.ForeignKey("drink.id")),
|
|
db.Column("type_id", db.Integer, db.ForeignKey("drink_type.id")),
|
|
)
|
|
|
|
|
|
class Tag(db.Model, ModelSerializeMixin):
|
|
"""
|
|
Tag
|
|
"""
|
|
|
|
__tablename__ = "drink_tag"
|
|
id: int = db.Column("id", db.Integer, primary_key=True)
|
|
name: str = db.Column(db.String(30), nullable=False, unique=True)
|
|
color: str = db.Column(db.String(7), nullable=False)
|
|
|
|
|
|
class DrinkType(db.Model, ModelSerializeMixin):
|
|
"""
|
|
DrinkType
|
|
"""
|
|
|
|
__tablename__ = "drink_type"
|
|
id: int = db.Column("id", db.Integer, primary_key=True)
|
|
name: str = db.Column(db.String(30), nullable=False, unique=True)
|
|
|
|
|
|
class DrinkPrice(db.Model, ModelSerializeMixin):
|
|
"""
|
|
PriceFromVolume
|
|
"""
|
|
|
|
__tablename__ = "drink_price"
|
|
id: int = db.Column("id", db.Integer, primary_key=True)
|
|
price: float = db.Column(db.Numeric(precision=5, scale=2, asdecimal=False))
|
|
volume_id_ = db.Column("volume_id", db.Integer, db.ForeignKey("drink_price_volume.id"))
|
|
volume = db.relationship("DrinkPriceVolume", back_populates="prices")
|
|
public: bool = db.Column(db.Boolean, default=True)
|
|
description: Optional[str] = db.Column(db.String(30))
|
|
|
|
def __repr__(self):
|
|
return f"DrinkPric({self.id},{self.price},{self.public},{self.description})"
|
|
|
|
|
|
class ExtraIngredient(db.Model, ModelSerializeMixin):
|
|
"""
|
|
ExtraIngredient
|
|
"""
|
|
|
|
__tablename__ = "extra_ingredient"
|
|
id: int = db.Column("id", db.Integer, primary_key=True)
|
|
name: str = db.Column(db.String(30), unique=True, nullable=False)
|
|
price: float = db.Column(db.Numeric(precision=5, scale=2, asdecimal=False))
|
|
|
|
|
|
class DrinkIngredient(db.Model, ModelSerializeMixin):
|
|
"""
|
|
Drink Ingredient
|
|
"""
|
|
|
|
__tablename__ = "drink_ingredient"
|
|
id: int = db.Column("id", db.Integer, primary_key=True)
|
|
volume: float = db.Column(db.Numeric(precision=5, scale=2, asdecimal=False), nullable=False)
|
|
ingredient_id: int = db.Column(db.Integer, db.ForeignKey("drink.id"))
|
|
# 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
|
|
|
|
|
|
class Ingredient(db.Model, ModelSerializeMixin):
|
|
"""
|
|
Ingredient Associationtable
|
|
"""
|
|
|
|
__tablename__ = "ingredient_association"
|
|
id: int = db.Column("id", db.Integer, primary_key=True)
|
|
volume_id = db.Column(db.Integer, db.ForeignKey("drink_price_volume.id"))
|
|
drink_ingredient: Optional[DrinkIngredient] = db.relationship(DrinkIngredient)
|
|
extra_ingredient: Optional[ExtraIngredient] = db.relationship(ExtraIngredient)
|
|
|
|
_drink_ingredient_id = db.Column(db.Integer, db.ForeignKey("drink_ingredient.id"))
|
|
_extra_ingredient_id = db.Column(db.Integer, db.ForeignKey("extra_ingredient.id"))
|
|
|
|
|
|
class MinPrices(ModelSerializeMixin):
|
|
"""
|
|
MinPrices
|
|
"""
|
|
|
|
percentage: float
|
|
price: float
|
|
|
|
|
|
class DrinkPriceVolume(db.Model, ModelSerializeMixin):
|
|
"""
|
|
Drink Volumes and Prices
|
|
"""
|
|
|
|
__tablename__ = "drink_price_volume"
|
|
id: int = db.Column("id", db.Integer, primary_key=True)
|
|
drink_id = db.Column(db.Integer, db.ForeignKey("drink.id"))
|
|
volume: float = db.Column(db.Numeric(precision=5, scale=2, asdecimal=False))
|
|
min_prices: list[MinPrices] = []
|
|
# ingredients: list[Ingredient] = []
|
|
|
|
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)
|
|
|
|
def __repr__(self):
|
|
return f"DrinkPriceVolume({self.id},{self.drink_id},{self.prices})"
|
|
|
|
|
|
class Drink(db.Model, ModelSerializeMixin):
|
|
"""
|
|
DrinkPrice
|
|
"""
|
|
|
|
__tablename__ = "drink"
|
|
id: int = db.Column("id", db.Integer, primary_key=True)
|
|
article_id: Optional[str] = db.Column(db.String(64))
|
|
package_size: Optional[int] = db.Column(db.Integer)
|
|
name: str = db.Column(db.String(60), nullable=False)
|
|
volume: Optional[float] = db.Column(db.Numeric(precision=5, scale=2, asdecimal=False))
|
|
cost_per_volume: Optional[float] = db.Column(db.Numeric(precision=5, scale=3, asdecimal=False))
|
|
cost_per_package: Optional[float] = db.Column(db.Numeric(precision=5, scale=3, asdecimal=False))
|
|
|
|
uuid: str = db.Column(db.String(36))
|
|
receipt: Optional[list[str]] = db.Column(db.PickleType(protocol=4))
|
|
|
|
_type_id = db.Column("type_id", db.Integer, db.ForeignKey("drink_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)
|
|
|
|
def __repr__(self):
|
|
return f"Drink({self.id},{self.name},{self.volumes})"
|
|
|
|
|
|
class _Picture:
|
|
"""Wrapper class for pictures binaries"""
|
|
|
|
mimetype = ""
|
|
binary = bytearray()
|