From 57a03a80cc0e2f2b503840972aac6645c4813fa7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Gr=C3=B6ger?= Date: Mon, 15 Nov 2021 09:19:50 +0100 Subject: [PATCH] [pricelist] add serverside sorting for pricelist, sorting by name for drinks --- flaschengeist/plugins/pricelist/__init__.py | 12 +++++++- .../plugins/pricelist/pricelist_controller.py | 29 +++++++++++++++++-- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/flaschengeist/plugins/pricelist/__init__.py b/flaschengeist/plugins/pricelist/__init__.py index 68693e8..afe21e4 100644 --- a/flaschengeist/plugins/pricelist/__init__.py +++ b/flaschengeist/plugins/pricelist/__init__.py @@ -269,6 +269,8 @@ def get_pricelist(): search_key = request.args.get("search_key") ingredient = request.args.get("ingredient", type=bool) receipt = request.args.get("receipt", type=bool) + descending = request.args.get("descending", type=bool) + sortBy = request.args.get("sortBy") try: if limit is not None: limit = int(limit) @@ -278,10 +280,18 @@ def get_pricelist(): ingredient = bool(ingredient) if receipt is not None: receipt = bool(receipt) + if descending is not None: + descending = bool(descending) except ValueError: raise BadRequest pricelist, count = pricelist_controller.get_pricelist( - public=public, limit=limit, offset=offset, search_name=search_name, search_key=search_key + public=public, + limit=limit, + offset=offset, + search_name=search_name, + search_key=search_key, + descending=descending, + sortBy=sortBy, ) logger.debug(f"GET pricelist {pricelist}, {count}") return jsonify({"pricelist": pricelist, "count": count}) diff --git a/flaschengeist/plugins/pricelist/pricelist_controller.py b/flaschengeist/plugins/pricelist/pricelist_controller.py index 1c5ac35..136711c 100644 --- a/flaschengeist/plugins/pricelist/pricelist_controller.py +++ b/flaschengeist/plugins/pricelist/pricelist_controller.py @@ -161,6 +161,7 @@ def get_drinks( | (Drink.type.has(DrinkType.name.contains(search_name))) | (Drink.tags.any(Tag.name.contains(search_name))) ) + query = query.order_by(Drink.name.asc()) if limit is not None: count = query.count() @@ -176,11 +177,13 @@ def get_drinks( return drinks, count -def get_pricelist(public=False, limit=None, offset=None, search_name=None, search_key=None): +def get_pricelist( + public=False, limit=None, offset=None, search_name=None, search_key=None, sortBy=None, descending=False +): count = None query = DrinkPrice.query if public: - query.filter(DrinkPrice.public) + query = 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)))) @@ -217,16 +220,36 @@ def get_pricelist(public=False, limit=None, offset=None, search_name=None, searc ) | ( DrinkPrice._volume.has( - DrinkPriceVolume._drink.has(Drink.tags.any(Tag.name.conaitns(search_name))) + DrinkPriceVolume._drink.has(Drink.tags.any(Tag.name.contains(search_name))) ) ) | (DrinkPrice.description.contains(search_name)) ) + if sortBy == "type": + query = ( + query.join(DrinkPrice._volume) + .join(DrinkPriceVolume._drink) + .join(Drink.type) + .order_by(DrinkType.name.desc() if descending else DrinkType.name.asc()) + ) + elif sortBy == "volume": + query = query.join(DrinkPrice._volume).order_by( + DrinkPriceVolume.volume.desc() if descending else DrinkPriceVolume.volume.asc() + ) + elif sortBy == "price": + query = query.order_by(DrinkPrice.price.desc() if descending else DrinkPrice.price.asc()) + else: + query = ( + query.join(DrinkPrice._volume) + .join(DrinkPriceVolume._drink) + .order_by(Drink.name.desc() if descending else Drink.name.asc()) + ) 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