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