Fix operator report email alerts with proper template and logging

- Use render_email() to wrap HTML in proper email template
- Add error handling and logging for URL generation
- Add log statement before sending to aid debugging
- Import render_email at module level
This commit is contained in:
Proudlock
2026-09-09 09:02:43 -04:00
parent f5d1b12e3f
commit 6e497a4751

View File

@@ -32,7 +32,7 @@ from ..models import (
)
from ..commands import COMMAND_ALLOWLIST, is_allowed_action, ALLOWED_ACTIONS_FOR, row_actions_for, requires_target, action_targetkind, elevated_permission_for, PLACE_POLICY_HOLD, ACTIVE_POLICY_HOLDS, HOLDABLE_ACTIONS_FOR, RELEASE_POLICY_HOLD, ACTIVE_LOCK_FOR, PLACE_ENDPOINT_LOCK, RELEASE_ENDPOINT_LOCK
from ..eventcatalog import CRITICAL_EVENT_IDS, event_severity, event_title
from shopdb.utils.mailer import send_alert
from shopdb.utils.mailer import send_alert, render_email
endpoints_bp = Blueprint('endpoints', __name__)
@@ -2942,21 +2942,30 @@ def _send_operator_report_alert(report, asset_id, hostname):
asset = Asset.query.get(asset_id)
if not asset:
current_app.logger.warning('Cannot send operator report alert: asset %d not found', asset_id)
return
device_url = url_for('assets.detail', asset_id=asset_id, _external=True)
try:
device_url = url_for('assets.detail', asset_id=asset_id, _external=True)
except Exception as e:
current_app.logger.error('Failed to generate device URL: %s', e)
device_url = f'/shopdb/assets/{asset_id}'
reported_at = report.createdat.strftime('%Y-%m-%d %H:%M:%S UTC') if report.createdat else 'Unknown'
screenshot_info = ''
if report.imagefilename:
screenshot_url = url_for('endpoints.serve_operator_report_image',
operatorreportid=report.operatorreportid,
_external=True)
screenshot_info = f'<p><strong>Screenshot:</strong> <a href="{screenshot_url}">View Screenshot</a></p>'
try:
screenshot_url = url_for('endpoints.serve_operator_report_image',
operatorreportid=report.operatorreportid,
_external=True)
screenshot_info = f'<p><strong>Screenshot:</strong> <a href="{screenshot_url}">View Screenshot</a></p>'
except Exception as e:
current_app.logger.warning('Failed to generate screenshot URL: %s', e)
details_html = f'<p style="white-space: pre-wrap;">{report.details or "No additional details provided."}</p>'
html_body = f'''
body_html = f'''
<div style="padding: 16px; background: #f9f9f9; border-left: 4px solid #d93f3f; margin-bottom: 16px;">
<p style="margin: 0 0 8px; font-weight: bold; color: #d93f3f;">New Issue Report from Shop Floor</p>
<p style="margin: 0;"><strong>Device:</strong> <a href="{device_url}">{asset.computername or hostname}</a> ({hostname})</p>
@@ -2971,8 +2980,11 @@ def _send_operator_report_alert(report, asset_id, hostname):
'''
subject = f'Operator Issue Report: {asset.computername or hostname}'
html, text = render_email(subject, body_html)
send_alert(subject, html_body)
current_app.logger.info('Sending operator report alert for %s (report %d)',
hostname, report.operatorreportid)
send_alert(subject, html, text=text)
@endpoints_bp.route('/operatorreports', methods=['POST'])