printedparts stage 13: pick alert recipients from shopdb users
Some checks failed
CI / backend (push) Successful in 1m42s
CI / naming (push) Successful in 2s
CI / frontend (push) Successful in 8s
CI / migrations-mysql (push) Failing after 8s

Contract 0.13.0 puts the User model on the plugin surface. The
settings page gains a checkbox picker over the user list; selected
users receive low-stock alerts at their account email, merged and
deduped with the free-text address list, inactive accounts skipped,
site alert_recipients still the fallback when both are empty.
This commit is contained in:
cproudlock
2026-07-17 08:30:04 -04:00
parent 427eb0de8c
commit a8a6baf979
10 changed files with 136 additions and 11 deletions

View File

@@ -250,6 +250,27 @@ def _ledger_write(item, transactiontype, quantitychange, sso, name, reason=None)
_send_lowstock_alert(item)
def _alert_recipients():
"""Merge selected shopdb users' account emails with the free-text list.
Empty result means fall back to the site-wide alert_recipients."""
from shopdb.api import User
recipients = []
userids = (Setting.get('printedparts_alert_userids') or '').strip()
for rawid in userids.split(','):
rawid = rawid.strip()
if not rawid.isdigit():
continue
user = db.session.get(User, int(rawid))
if user and user.isactive and user.email:
recipients.append(user.email)
extra = (Setting.get('printedparts_alert_email') or '').strip()
recipients.extend(address.strip() for address in extra.split(',')
if address.strip())
# dedupe, order-preserving
return list(dict.fromkeys(recipients))
def _send_lowstock_alert(item):
"""Best-effort email when an item crosses its low-stock threshold.
@@ -265,10 +286,9 @@ def _send_lowstock_alert(item):
f'<p>Bin: {item.binlocation or "-"}</p>'
f'<p>Time to print more.</p>')
try:
recipients = (Setting.get('printedparts_alert_email') or '').strip()
recipients = _alert_recipients()
if recipients:
send_email([address.strip() for address in recipients.split(',')
if address.strip()], subject, html)
send_email(recipients, subject, html)
else:
send_alert(subject, html)
except Exception:

View File

@@ -133,6 +133,9 @@ class PrintedpartsPlugin(BasePlugin):
('printedparts_alert_email', '', 'string',
'Comma-separated low-stock alert recipients; empty uses the '
'site alert_recipients'),
('printedparts_alert_userids', '', 'string',
'Comma-separated shopdb user ids whose account emails receive '
'low-stock alerts'),
]
for key, value, valuetype, description in defaults:
if Setting.get(key) is None: