[pricelist] add serverside pagination of drinks

This commit is contained in:
Tim Gröger 2021-11-12 22:09:16 +01:00
parent ff13eefb45
commit 26a00ed6a6
2 changed files with 31 additions and 8 deletions

View File

@ -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/<string:name>", methods=["GET"])

View File

@ -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()