Add pagination, device detail unassign-all, and batch install with slot limit
This commit is contained in:
@@ -958,8 +958,45 @@ def create_app(config_object="config"):
|
||||
f"Unassigned {count} batter{'y' if count == 1 else 'ies'} from {device.name}.",
|
||||
"success",
|
||||
)
|
||||
nxt = request.form.get("next", "")
|
||||
if nxt.startswith("/"):
|
||||
return redirect(nxt)
|
||||
return redirect(url_for("device_list"))
|
||||
|
||||
# ------------------------------------------------------------------ #
|
||||
# Devices — batch install specific batteries
|
||||
# ------------------------------------------------------------------ #
|
||||
|
||||
@app.route("/device/<int:device_id>/install-batch", methods=["POST"])
|
||||
def device_install_batch(device_id):
|
||||
device = db.get(Device, device_id)
|
||||
if device is None:
|
||||
abort(404)
|
||||
battery_ids = request.form.getlist("battery_ids")
|
||||
free = device.battery_slots - device.installed_count()
|
||||
if not battery_ids:
|
||||
flash("No batteries selected.", "warning")
|
||||
return redirect(url_for("device_detail", device_id=device_id))
|
||||
installed = 0
|
||||
for bid in battery_ids:
|
||||
if installed >= free:
|
||||
break
|
||||
battery = db.get(Battery, int(bid))
|
||||
if not battery or battery.status != "available":
|
||||
continue
|
||||
battery.status = "installed"
|
||||
battery.device_id = device.id
|
||||
installed += 1
|
||||
db.commit()
|
||||
if installed:
|
||||
flash(
|
||||
f"Installed {installed} batter{'y' if installed == 1 else 'ies'} into {device.name}.",
|
||||
"success",
|
||||
)
|
||||
else:
|
||||
flash("No batteries were installed.", "warning")
|
||||
return redirect(url_for("device_detail", device_id=device_id))
|
||||
|
||||
# ------------------------------------------------------------------ #
|
||||
# Export
|
||||
# ------------------------------------------------------------------ #
|
||||
|
||||
Reference in New Issue
Block a user