printedparts stage 13: pick alert recipients from shopdb users
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:
@@ -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:
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user