Extract _filter_compatible helper for size-compatible battery queries
This commit is contained in:
@@ -32,6 +32,13 @@ def _safe_next(default_url):
|
||||
return default_url
|
||||
|
||||
|
||||
def _filter_compatible(query, battery_size):
|
||||
"""Restrict a Battery query to size-compatible batteries (matching size or unsized)."""
|
||||
if battery_size:
|
||||
query = query.filter((Battery.size == battery_size) | (Battery.size == None))
|
||||
return query
|
||||
|
||||
|
||||
def _record_charge(db, battery, date_val, increment, notes):
|
||||
"""Apply one charge event to battery. Caller must call db.commit()."""
|
||||
if increment:
|
||||
@@ -787,16 +794,13 @@ def create_app(config_object="config"):
|
||||
if device is None:
|
||||
abort(404)
|
||||
all_devices = db.query(Device).all()
|
||||
brands_q = db.query(Battery.brand).filter(Battery.status == "available")
|
||||
if device.battery_size:
|
||||
brands_q = brands_q.filter(
|
||||
(Battery.size == device.battery_size) | (Battery.size == None)
|
||||
brands_q = _filter_compatible(
|
||||
db.query(Battery.brand).filter(Battery.status == "available"),
|
||||
device.battery_size,
|
||||
)
|
||||
brands = [r[0] for r in brands_q.distinct().order_by(Battery.brand).all()]
|
||||
avail_q = db.query(Battery).filter_by(status="available")
|
||||
if device.battery_size:
|
||||
avail_q = avail_q.filter(
|
||||
(Battery.size == device.battery_size) | (Battery.size == None)
|
||||
avail_q = _filter_compatible(
|
||||
db.query(Battery).filter_by(status="available"), device.battery_size
|
||||
)
|
||||
available_batteries = avail_q.order_by(Battery.label).all()
|
||||
device_types = sorted({d.device_type for d in all_devices if d.device_type})
|
||||
@@ -827,10 +831,8 @@ def create_app(config_object="config"):
|
||||
for b in child.batteries:
|
||||
if b.status == "installed":
|
||||
flat_installed.append((b, child.name, child.id))
|
||||
avail_q = db.query(Battery).filter_by(status="available")
|
||||
if child.battery_size:
|
||||
avail_q = avail_q.filter(
|
||||
(Battery.size == child.battery_size) | (Battery.size == None)
|
||||
avail_q = _filter_compatible(
|
||||
db.query(Battery).filter_by(status="available"), child.battery_size
|
||||
)
|
||||
children_avail[child.id] = avail_q.order_by(Battery.label).all()
|
||||
return render_template("device_detail.html", device=device, brands=brands,
|
||||
@@ -975,10 +977,9 @@ def create_app(config_object="config"):
|
||||
|
||||
# Validate availability before writing anything
|
||||
for brand, qty in pairs:
|
||||
avail_q = db.query(func.count(Battery.id)).filter_by(brand=brand, status="available")
|
||||
if device.battery_size:
|
||||
avail_q = avail_q.filter(
|
||||
(Battery.size == device.battery_size) | (Battery.size == None)
|
||||
avail_q = _filter_compatible(
|
||||
db.query(func.count(Battery.id)).filter_by(brand=brand, status="available"),
|
||||
device.battery_size,
|
||||
)
|
||||
available_count = avail_q.scalar()
|
||||
if available_count < qty:
|
||||
@@ -991,10 +992,9 @@ def create_app(config_object="config"):
|
||||
# All checks passed — perform installs
|
||||
total_installed = 0
|
||||
for brand, qty in pairs:
|
||||
batch_q = db.query(Battery).filter_by(brand=brand, status="available")
|
||||
if device.battery_size:
|
||||
batch_q = batch_q.filter(
|
||||
(Battery.size == device.battery_size) | (Battery.size == None)
|
||||
batch_q = _filter_compatible(
|
||||
db.query(Battery).filter_by(brand=brand, status="available"),
|
||||
device.battery_size,
|
||||
)
|
||||
batch = batch_q.order_by(Battery.id).limit(qty).all()
|
||||
for b in batch:
|
||||
|
||||
Reference in New Issue
Block a user