Add size, chemistry, capacity, purchase date to bulk add form

Shared metadata fields settable at creation time apply to all
batteries in the batch, avoiding per-battery detail page visits.
This commit is contained in:
2026-04-12 15:15:07 -05:00
parent 604d7bb699
commit 5a7bbd46ab
2 changed files with 83 additions and 25 deletions
+19 -4
View File
@@ -37,10 +37,11 @@ def create_app(config_object="config"):
@app.route("/battery/add", methods=["GET", "POST"])
def battery_add():
if request.method == "POST":
brand = request.form.get("brand", "").strip()
notes = request.form.get("notes", "").strip() or None
f = request.form
brand = f.get("brand", "").strip()
notes = f.get("notes", "").strip() or None
try:
count = max(1, min(50, int(request.form.get("count", 1) or 1)))
count = max(1, min(50, int(f.get("count", 1) or 1)))
except (ValueError, TypeError):
count = 1
@@ -51,10 +52,24 @@ def create_app(config_object="config"):
form_brand="", form_count=1, form_notes=notes or "",
brands=brands), 400
def _int(key):
v = f.get(key, "").strip()
try:
return int(v) if v else None
except ValueError:
return None
size = f.get("size", "").strip() or None
chemistry = f.get("chemistry", "").strip() or None
capacity_mah = _int("capacity_mah")
purchase_date = f.get("purchase_date", "").strip() or None
existing = db.query(func.count(Battery.id)).filter_by(brand=brand).scalar()
for i in range(count):
label = f"{brand} {existing + i + 1:03d}"
db.add(Battery(label=label, brand=brand, status="available", notes=notes))
db.add(Battery(label=label, brand=brand, status="available", notes=notes,
size=size, chemistry=chemistry,
capacity_mah=capacity_mah, purchase_date=purchase_date))
db.commit()
flash(f"Added {count} {brand} batter{'y' if count == 1 else 'ies'}.", "success")
return redirect(url_for("dashboard"))