Card refinements
This commit is contained in:
@@ -1020,12 +1020,11 @@ def _get_low_supplies_data():
|
||||
|
||||
if has_low:
|
||||
# location name for the report row
|
||||
location_name = None
|
||||
if asset.locationid:
|
||||
from shopdb.api import Location
|
||||
loc = db.session.get(Location, asset.locationid)
|
||||
if loc:
|
||||
location_name = loc.locationname
|
||||
# Via the relationship, the way the printers list does it. The
|
||||
# previous lookup went through db.session.get on locationid, and
|
||||
# every row came back with no location even where one is set.
|
||||
location_name = (asset.location.locationname
|
||||
if asset.location else None)
|
||||
|
||||
results.append({
|
||||
'printerid': printer.printerid,
|
||||
@@ -1441,27 +1440,37 @@ def dashboard_supplies():
|
||||
"""
|
||||
data = _get_low_supplies_data()
|
||||
|
||||
threshold = 5
|
||||
setting = Setting.query.filter_by(key='printers_dashboardpercent').first()
|
||||
if setting and (setting.value or '').strip():
|
||||
try:
|
||||
threshold = int(setting.value)
|
||||
except (TypeError, ValueError):
|
||||
pass
|
||||
|
||||
rows = []
|
||||
for printer in data.get('printers', []):
|
||||
criticals = [s for s in printer['supplies'] if s['status'] == 'critical']
|
||||
lows = [s for s in printer['supplies'] if s['status'] == 'low']
|
||||
if not criticals and not lows:
|
||||
# The CARD is tighter than the report. The report lists anything the
|
||||
# thresholds call low, which is the right scope for planning an order;
|
||||
# the dashboard is asking what to walk out and change today, and a
|
||||
# cartridge at 18% is not that. Anything at or below the threshold.
|
||||
depleted = [supply for supply in printer['supplies']
|
||||
if isinstance(supply.get('remaining'), (int, float))
|
||||
and supply['remaining'] <= threshold]
|
||||
if not depleted:
|
||||
continue
|
||||
# Criticals first. Every depleted supply is listed, not just the worst
|
||||
# class - whoever walks out there wants to carry both cartridges.
|
||||
depleted.sort(key=lambda supply: supply['remaining'])
|
||||
rows.append({
|
||||
'printerid': printer['printerid'],
|
||||
'printername': printer['printername'] or printer['assetnumber'],
|
||||
'location': printer['location'] or 'No location set',
|
||||
'iscritical': bool(criticals),
|
||||
# One chip per depleted supply: what is low, and on hover what to
|
||||
# order to fix it.
|
||||
'iscritical': any(s['status'] == 'critical' for s in depleted),
|
||||
'supplies': [{
|
||||
'text': '{} {}%'.format(_shortsupplyname(supply.get('name')),
|
||||
supply.get('remaining')),
|
||||
'title': _reordertip(supply),
|
||||
'level': supply.get('status'),
|
||||
} for supply in criticals + lows],
|
||||
} for supply in depleted],
|
||||
})
|
||||
|
||||
rows.sort(key=lambda r: (not r['iscritical'], r['printername']))
|
||||
|
||||
Reference in New Issue
Block a user