[pricelist] add serverside pagination of drinks
This commit is contained in:
parent
ff13eefb45
commit
26a00ed6a6
|
@ -214,10 +214,21 @@ def get_drinks(identifier=None):
|
||||||
|
|
||||||
if identifier:
|
if identifier:
|
||||||
result = pricelist_controller.get_drink(identifier, public=public)
|
result = pricelist_controller.get_drink(identifier, public=public)
|
||||||
|
return jsonify(result)
|
||||||
else:
|
else:
|
||||||
result = pricelist_controller.get_drinks(public=public)
|
limit = request.args.get("limit")
|
||||||
logger.debug(f"GET drink {result}")
|
offset = request.args.get("offset")
|
||||||
return jsonify(result)
|
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"])
|
@PriceListPlugin.blueprint.route("/drinks/search/<string:name>", methods=["GET"])
|
||||||
|
|
|
@ -131,13 +131,22 @@ def _create_public_drink(drink):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def get_drinks(name=None, public=False):
|
def get_drinks(name=None, public=False, limit=None, offset=None):
|
||||||
|
count = None
|
||||||
if name:
|
if name:
|
||||||
drinks = Drink.query.filter(Drink.name.contains(name)).all()
|
query = Drink.query.filter(Drink.name.contains(name))
|
||||||
drinks = Drink.query.all()
|
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:
|
if public:
|
||||||
return [_create_public_drink(drink) for drink in drinks if _create_public_drink(drink)]
|
return [_create_public_drink(drink) for drink in drinks if _create_public_drink(drink)], count
|
||||||
return drinks
|
return drinks, count
|
||||||
|
|
||||||
|
|
||||||
def get_drink(identifier, public=False):
|
def get_drink(identifier, public=False):
|
||||||
|
@ -209,6 +218,9 @@ def set_volumes(volumes):
|
||||||
|
|
||||||
def delete_drink(identifier):
|
def delete_drink(identifier):
|
||||||
drink = get_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.delete(drink)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue