feature/pricelist #15

Merged
ferfissimo merged 11 commits from feature/pricelist into develop 2021-11-15 09:55:11 +00:00
3 changed files with 127 additions and 9 deletions
Showing only changes of commit dba60fdab8 - Show all commits

View File

@ -242,11 +242,51 @@ def get_drinks(identifier=None):
ingredient=ingredient, ingredient=ingredient,
receipt=receipt, receipt=receipt,
) )
mop = drinks.copy()
logger.debug(f"GET drink {drinks}, {count}") logger.debug(f"GET drink {drinks}, {count}")
# return jsonify({"drinks": drinks, "count": count}) # return jsonify({"drinks": drinks, "count": count})
return jsonify({"drinks": drinks, "count": count}) return jsonify({"drinks": drinks, "count": count})
@PriceListPlugin.blueprint.route("/list", methods=["GET"])
def get_pricelist():
"""Get Priclist
Route: ``/pricelist/list`` | Method: ``GET``
Returns:
JSON encoded list of DrinkPrices or HTTP-KeyError
"""
public = True
try:
extract_session()
public = False
except Unauthorized:
public = True
limit = request.args.get("limit")
offset = request.args.get("offset")
search_name = request.args.get("search_name")
search_key = request.args.get("search_key")
ingredient = request.args.get("ingredient", type=bool)
receipt = request.args.get("receipt", type=bool)
try:
if limit is not None:
limit = int(limit)
if offset is not None:
offset = int(offset)
if ingredient is not None:
ingredient = bool(ingredient)
if receipt is not None:
receipt = bool(receipt)
except ValueError:
raise BadRequest
pricelist, count = pricelist_controller.get_pricelist(
public=public, limit=limit, offset=offset, search_name=search_name, search_key=search_key
)
logger.debug(f"GET pricelist {pricelist}, {count}")
return jsonify({"pricelist": pricelist, "count": count})
@PriceListPlugin.blueprint.route("/drinks/search/<string:name>", methods=["GET"]) @PriceListPlugin.blueprint.route("/drinks/search/<string:name>", methods=["GET"])
def search_drinks(name): def search_drinks(name):
"""Search Drink """Search Drink

View File

@ -48,7 +48,8 @@ class DrinkPrice(db.Model, ModelSerializeMixin):
id: int = db.Column("id", db.Integer, primary_key=True) id: int = db.Column("id", db.Integer, primary_key=True)
price: float = db.Column(db.Numeric(precision=5, scale=2, asdecimal=False)) 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_id_ = db.Column("volume_id", db.Integer, db.ForeignKey("drink_price_volume.id"))
volume = db.relationship("DrinkPriceVolume", back_populates="prices") volume: "DrinkPriceVolume" = None
_volume: "DrinkPriceVolume" = db.relationship("DrinkPriceVolume", back_populates="_prices", join_depth=1)
public: bool = db.Column(db.Boolean, default=True) public: bool = db.Column(db.Boolean, default=True)
description: Optional[str] = db.Column(db.String(30)) description: Optional[str] = db.Column(db.String(30))
@ -121,11 +122,15 @@ class DrinkPriceVolume(db.Model, ModelSerializeMixin):
__tablename__ = "drink_price_volume" __tablename__ = "drink_price_volume"
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")) drink_id = db.Column(db.Integer, db.ForeignKey("drink.id"))
drink: "Drink" = None
_drink: "Drink" = db.relationship("Drink", back_populates="_volumes")
volume: float = db.Column(db.Numeric(precision=5, scale=2, asdecimal=False)) volume: float = db.Column(db.Numeric(precision=5, scale=2, asdecimal=False))
min_prices: list[MinPrices] = [] min_prices: list[MinPrices] = []
# ingredients: list[Ingredient] = [] # ingredients: list[Ingredient] = []
prices: list[DrinkPrice] = []
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"
)
ingredients: list[Ingredient] = db.relationship("Ingredient", foreign_keys=Ingredient.volume_id) ingredients: list[Ingredient] = db.relationship("Ingredient", foreign_keys=Ingredient.volume_id)
def __repr__(self): def __repr__(self):
@ -153,7 +158,10 @@ class Drink(db.Model, ModelSerializeMixin):
tags: Optional[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: Optional[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] = []
_volumes: list[DrinkPriceVolume] = db.relationship(
DrinkPriceVolume, back_populates="_drink", cascade="all,delete,delete-orphan"
)
def __repr__(self): def __repr__(self):
return f"Drink({self.id},{self.name},{self.volumes})" return f"Drink({self.id},{self.name},{self.volumes})"

View File

@ -142,9 +142,9 @@ def get_drinks(
if ingredient: if ingredient:
query = query.filter(Drink.cost_per_volume >= 0) query = query.filter(Drink.cost_per_volume >= 0)
if receipt: if receipt:
query = query.filter(Drink.volumes.any(DrinkPriceVolume.ingredients != None)) query = query.filter(Drink._volumes.any(DrinkPriceVolume.ingredients != None))
if public: if public:
query = query.filter(Drink.volumes.any(DrinkPriceVolume.prices.any(DrinkPrice.public))) query = query.filter(Drink._volumes.any(DrinkPriceVolume._prices.any(DrinkPrice.public)))
if search_name: if search_name:
if search_key == "name": if search_key == "name":
query = query.filter(Drink.name.contains(search_name)) query = query.filter(Drink.name.contains(search_name))
@ -168,9 +168,72 @@ def get_drinks(
if offset is not None: if offset is not None:
query = query.offset(offset) query = query.offset(offset)
drinks = query.all() drinks = query.all()
for drink in drinks:
for volume in drink._volumes:
volume.prices = volume._prices
drink.volumes = drink._volumes
return drinks, count return drinks, count
def get_pricelist(public=False, limit=None, offset=None, search_name=None, search_key=None):
count = None
query = DrinkPrice.query
if public:
query.filter(DrinkPrice.public)
if search_name:
if search_key == "name":
query = query.filter(DrinkPrice._volume.has(DrinkPriceVolume._drink.has(Drink.name.contains(search_name))))
if search_key == "type":
query = query.filter(
DrinkPrice._volume.has(
DrinkPriceVolume._drink.has(Drink.type.has(DrinkType.name.contains(search_name)))
)
)
if search_key == "tags":
query = query.filter(
DrinkPrice._volume.has(DrinkPriceVolume._drink.has(Drink.tags.any(Tag.name.conaitns(search_name))))
)
if search_key == "volume":
query = query.filter(DrinkPrice._volume.has(DrinkPriceVolume.volume == float(search_name)))
if search_key == "price":
query = query.filter(DrinkPrice.price == float(search_name))
if search_key == "description":
query = query.filter(DrinkPrice.description.contains(search_name))
else:
try:
search_name = float(search_name)
query = query.filter(
(DrinkPrice._volume.has(DrinkPriceVolume.volume == float(search_name)))
| (DrinkPrice.price == float(search_name))
)
except:
query = query.filter(
(DrinkPrice._volume.has(DrinkPriceVolume._drink.has(Drink.name.contains(search_name))))
| (
DrinkPrice._volume.has(
DrinkPriceVolume._drink.has(Drink.type.has(DrinkType.name.contains(search_name)))
)
)
| (
DrinkPrice._volume.has(
DrinkPriceVolume._drink.has(Drink.tags.any(Tag.name.conaitns(search_name)))
)
)
| (DrinkPrice.description.contains(search_name))
)
if limit is not None:
count = query.count()
query = query.limit(limit)
if offset is not None:
query = query.offset(offset)
prices = query.all()
for price in prices:
price._volume.drink = price._volume._drink
price.volume = price._volume
return prices, count
def get_drink(identifier, public=False): def get_drink(identifier, public=False):
drink = None drink = None
if isinstance(identifier, int): if isinstance(identifier, int):
@ -183,6 +246,9 @@ def get_drink(identifier, public=False):
raise NotFound raise NotFound
if public: if public:
return _create_public_drink(drink) return _create_public_drink(drink)
for volume in drink._volumes:
volume.prices = volume._prices
drink.volumes = drink._volumes
return drink return drink
@ -219,11 +285,15 @@ def update_drink(identifier, data):
if drink_type: if drink_type:
drink.type = drink_type drink.type = drink_type
if volumes is not None and session.user_.has_permission(EDIT_VOLUME): if volumes is not None and session.user_.has_permission(EDIT_VOLUME):
drink.volumes = [] drink._volumes = []
drink.volumes = set_volumes(volumes) drink._volumes = set_volumes(volumes)
if len(tags) > 0: if len(tags) > 0:
drink.tags = tags drink.tags = tags
db.session.commit() db.session.commit()
for volume in drink._volumes:
volume.prices = volume._prices
drink.volumes = drink._volumes
return drink return drink
except (NotFound, KeyError): except (NotFound, KeyError):
raise BadRequest raise BadRequest
@ -291,7 +361,7 @@ def set_prices(prices, volume):
for _price in prices: for _price in prices:
price = set_price(_price) price = set_price(_price)
_prices.append(price) _prices.append(price)
volume.prices = _prices volume._prices = _prices
def set_ingredients(ingredients, volume): def set_ingredients(ingredients, volume):