Add size-in-label checkbox to battery add form
This commit is contained in:
@@ -144,14 +144,19 @@ def create_app(config_object="config"):
|
||||
purchase_date = f.get("purchase_date", "").strip() or None
|
||||
storage_location = f.get("storage_location", "").strip() or None
|
||||
|
||||
include_size = f.get("include_size_in_label") == "on"
|
||||
label_prefix = f"{brand} {size}" if include_size and size else brand
|
||||
|
||||
existing_labels = [
|
||||
r[0] for r in db.query(Battery.label).filter(Battery.brand == brand).all()
|
||||
]
|
||||
nums = [int(m.group(1)) for lbl in existing_labels
|
||||
prefix_labels = [lbl for lbl in existing_labels
|
||||
if lbl == label_prefix or lbl.startswith(label_prefix + " ")]
|
||||
nums = [int(m.group(1)) for lbl in prefix_labels
|
||||
if (m := re.search(r'(\d+)$', lbl))]
|
||||
next_num = max(nums, default=0)
|
||||
for i in range(count):
|
||||
label = f"{brand} {next_num + i + 1:03d}"
|
||||
label = f"{label_prefix} {next_num + i + 1:03d}"
|
||||
db.add(Battery(label=label, brand=brand, status="available", notes=notes,
|
||||
size=size, chemistry=chemistry, capacity_mah=capacity_mah,
|
||||
purchase_date=purchase_date, storage_location=storage_location))
|
||||
@@ -165,14 +170,21 @@ def create_app(config_object="config"):
|
||||
.filter(Battery.storage_location.isnot(None))
|
||||
.distinct().order_by(Battery.storage_location).all()
|
||||
]
|
||||
brand_counts = {
|
||||
r[0]: r[1]
|
||||
for r in db.query(Battery.brand, func.count(Battery.id))
|
||||
.group_by(Battery.brand).all()
|
||||
}
|
||||
prefix_max_nums = {}
|
||||
for lbl, brand, size in db.query(Battery.label, Battery.brand, Battery.size).all():
|
||||
m = re.search(r'(\d+)$', lbl)
|
||||
if not m:
|
||||
continue
|
||||
num = int(m.group(1))
|
||||
if num > prefix_max_nums.get(brand, 0):
|
||||
prefix_max_nums[brand] = num
|
||||
if size:
|
||||
key = f"{brand} {size}"
|
||||
if num > prefix_max_nums.get(key, 0):
|
||||
prefix_max_nums[key] = num
|
||||
return render_template("battery_add.html", form_count=1, brands=brands,
|
||||
storage_locations=storage_locations,
|
||||
brand_counts=brand_counts)
|
||||
prefix_max_nums=prefix_max_nums)
|
||||
|
||||
# ------------------------------------------------------------------ #
|
||||
# Battery — detail
|
||||
|
||||
Reference in New Issue
Block a user