From 26a00ed6a6b5a709960fe0d6c78eb4f4de6829cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Gr=C3=B6ger?= Date: Fri, 12 Nov 2021 22:09:16 +0100 Subject: [PATCH] [pricelist] add serverside pagination of drinks --- flaschengeist/plugins/pricelist/__init__.py | 17 +++++++++++--- .../plugins/pricelist/pricelist_controller.py | 22 ++++++++++++++----- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/flaschengeist/plugins/pricelist/__init__.py b/flaschengeist/plugins/pricelist/__init__.py index b4df1ac..67c92db 100644 --- a/flaschengeist/plugins/pricelist/__init__.py +++ b/flaschengeist/plugins/pricelist/__init__.py @@ -214,10 +214,21 @@ def get_drinks(identifier=None): if identifier: result = pricelist_controller.get_drink(identifier, public=public) + return jsonify(result) else: - result = pricelist_controller.get_drinks(public=public) - logger.debug(f"GET drink {result}") - return jsonify(result) + limit = request.args.get("limit") + offset = request.args.get("offset") + try: + if limit is not None: + limit = int(limit) + if offset is not None: + offset = int(offset) + except ValueError: + raise BadRequest + drinks, count = pricelist_controller.get_drinks(public=public, limit=limit, offset=offset) + logger.debug(f"GET drink {drinks}, {count}") + # return jsonify({"drinks": drinks, "count": count}) + return jsonify({"drinks": drinks, "count": count}) @PriceListPlugin.blueprint.route("/drinks/search/", methods=["GET"]) diff --git a/flaschengeist/plugins/pricelist/pricelist_controller.py b/flaschengeist/plugins/pricelist/pricelist_controller.py index c89dc2d..6ea9b87 100644 --- a/flaschengeist/plugins/pricelist/pricelist_controller.py +++ b/flaschengeist/plugins/pricelist/pricelist_controller.py @@ -131,13 +131,22 @@ def _create_public_drink(drink): return None -def get_drinks(name=None, public=False): +def get_drinks(name=None, public=False, limit=None, offset=None): + count = None if name: - drinks = Drink.query.filter(Drink.name.contains(name)).all() - drinks = Drink.query.all() + query = Drink.query.filter(Drink.name.contains(name)) + else: + query = Drink.query + + if limit is not None: + count = query.count() + query = query.limit(limit) + if offset is not None: + query = query.offset(offset) + drinks = query.all() if public: - return [_create_public_drink(drink) for drink in drinks if _create_public_drink(drink)] - return drinks + return [_create_public_drink(drink) for drink in drinks if _create_public_drink(drink)], count + return drinks, count def get_drink(identifier, public=False): @@ -209,6 +218,9 @@ def set_volumes(volumes): def delete_drink(identifier): drink = get_drink(identifier) + if drink.uuid: + path = config["pricelist"]["path"] + delete_picture(f"{path}/{drink.uuid}") db.session.delete(drink) db.session.commit()