Aug 6 - Update enrollment page, notify admin user
This commit is contained in:
+113
-15
@@ -13,7 +13,11 @@ response), and the From identity comes from branded_sender() so it stays an
|
||||
SMTP-authorized address that actually delivers (rules 64 / 76).
|
||||
|
||||
This is the only part of app/enrollment that touches shared mail
|
||||
infrastructure; it still imports no models and writes no DB row.
|
||||
infrastructure. It performs exactly ONE database read — resolving the active
|
||||
admin accounts to notify — and no write. That read is a deliberate, narrowed
|
||||
exception to the package's no-models rule (rule 88): the alternative, a
|
||||
hand-maintained recipient list in config, drifts out of step with reality the
|
||||
first time someone joins or leaves. Everything else here stays model-free.
|
||||
"""
|
||||
|
||||
import logging
|
||||
@@ -53,6 +57,113 @@ def _text_body(record, people, corrections_email):
|
||||
return '\n'.join(lines)
|
||||
|
||||
|
||||
def _dispatch(msg, label, record):
|
||||
"""Send one message on a background thread. Never raises.
|
||||
|
||||
Rule 14 — the HTTP response must not wait on SMTP. The submission is
|
||||
already on disk by the time anything here runs, so a mail failure is
|
||||
logged and dropped rather than surfaced to the customer.
|
||||
"""
|
||||
app = current_app._get_current_object()
|
||||
|
||||
def _send():
|
||||
with app.app_context():
|
||||
try:
|
||||
from app import mail
|
||||
mail.send(msg)
|
||||
logger.info('ENROLLMENT %s SENT | to=%s | id=%s',
|
||||
label, msg.recipients, record.get('id'))
|
||||
except Exception as exc:
|
||||
logger.error('ENROLLMENT %s FAILED | to=%s | id=%s | error=%s',
|
||||
label, msg.recipients, record.get('id'), exc)
|
||||
|
||||
threading.Thread(target=_send, daemon=True).start()
|
||||
|
||||
|
||||
def _admin_recipients():
|
||||
"""Addresses to alert when a new enrollment arrives.
|
||||
|
||||
Active `admin` accounts, plus any extra addresses in the optional
|
||||
ENROLLMENT_NOTIFY_EMAILS config (comma-separated) for people who should be
|
||||
told but do not hold a JQC login. Deduplicated case-insensitively.
|
||||
|
||||
The User import is function-local and read-only — see the module docstring.
|
||||
"""
|
||||
emails = []
|
||||
try:
|
||||
from app.models.user import User
|
||||
rows = User.query.filter(User.role == 'admin',
|
||||
User.active == True).all() # noqa: E712
|
||||
emails += [u.email for u in rows if u.email]
|
||||
except Exception:
|
||||
# A DB problem must not stop the confirmation going out, nor the
|
||||
# submission from succeeding.
|
||||
logger.exception('ENROLLMENT | could not resolve admin recipients')
|
||||
|
||||
extra = current_app.config.get('ENROLLMENT_NOTIFY_EMAILS') or ''
|
||||
emails += [e.strip() for e in extra.split(',') if e.strip()]
|
||||
|
||||
seen, out = set(), []
|
||||
for e in emails:
|
||||
low = e.lower()
|
||||
if low not in seen:
|
||||
seen.add(low)
|
||||
out.append(e)
|
||||
return out
|
||||
|
||||
|
||||
def send_admin_notification(record, base_url=None):
|
||||
"""Alert JQC admins that a new enrollment form has arrived. Never raises."""
|
||||
if not current_app.config.get('MAIL_SERVER'):
|
||||
logger.warning('ENROLLMENT ADMIN EMAIL SKIPPED | no MAIL_SERVER | id=%s',
|
||||
record.get('id'))
|
||||
return
|
||||
|
||||
try:
|
||||
from flask_mail import Message
|
||||
from app.utils.mail_utils import branded_sender
|
||||
from . import schema
|
||||
|
||||
recipients = _admin_recipients()
|
||||
if not recipients:
|
||||
logger.warning('ENROLLMENT | no admin recipients for id=%s',
|
||||
record.get('id'))
|
||||
return
|
||||
|
||||
effective_base = (base_url
|
||||
or current_app.config.get('APP_BASE_URL', '')).rstrip('/')
|
||||
people = schema.people_of(record)
|
||||
link = f'{effective_base}/enrollment/admin/{record.get("id")}'
|
||||
|
||||
lines = [
|
||||
'A new JQC enrollment form has been submitted.',
|
||||
'',
|
||||
f'Project: {record.get("project_name")}',
|
||||
f'Requester: {record.get("request_by")} <{record.get("requester_email")}>',
|
||||
f'Reference: {record.get("id")}',
|
||||
f'People: {len(people)}',
|
||||
'',
|
||||
f'Open it here: {link}',
|
||||
]
|
||||
if record.get('notes'):
|
||||
lines += ['', f'Customer notes: {record["notes"]}']
|
||||
|
||||
msg = Message(
|
||||
subject = f'[JQC] New enrollment — {record.get("project_name")}',
|
||||
sender = branded_sender(effective_base),
|
||||
recipients = recipients,
|
||||
body = '\n'.join(lines),
|
||||
html = render_template('enrollment/email_admin_notice.html',
|
||||
record=record, people=people,
|
||||
schema=schema, link=link),
|
||||
)
|
||||
_dispatch(msg, 'ADMIN EMAIL', record)
|
||||
|
||||
except Exception:
|
||||
logger.exception('ENROLLMENT ADMIN EMAIL BUILD FAILED | id=%s',
|
||||
record.get('id'))
|
||||
|
||||
|
||||
def send_confirmation(record, base_url=None):
|
||||
"""Email the requester a copy of what they submitted. Never raises."""
|
||||
email = (record.get('requester_email') or '').strip()
|
||||
@@ -66,7 +177,6 @@ def send_confirmation(record, base_url=None):
|
||||
|
||||
try:
|
||||
from flask_mail import Message
|
||||
from app import mail
|
||||
from app.utils.mail_utils import branded_sender
|
||||
from . import schema
|
||||
|
||||
@@ -84,19 +194,7 @@ def send_confirmation(record, base_url=None):
|
||||
schema=schema),
|
||||
)
|
||||
|
||||
app = current_app._get_current_object()
|
||||
|
||||
def _send():
|
||||
with app.app_context():
|
||||
try:
|
||||
mail.send(msg)
|
||||
logger.info('ENROLLMENT EMAIL SENT | to=%s | id=%s',
|
||||
email, record.get('id'))
|
||||
except Exception as exc:
|
||||
logger.error('ENROLLMENT EMAIL FAILED | to=%s | id=%s | error=%s',
|
||||
email, record.get('id'), exc)
|
||||
|
||||
threading.Thread(target=_send, daemon=True).start()
|
||||
_dispatch(msg, 'EMAIL', record)
|
||||
|
||||
except Exception:
|
||||
# Building the message failed (bad template, mail misconfigured, …).
|
||||
|
||||
@@ -243,9 +243,10 @@ def submit():
|
||||
logger.info('ENROLLMENT | submitted | id=%s project=%r people=%d ip=%s',
|
||||
record['id'], project_name, len(named), request.remote_addr)
|
||||
|
||||
# Confirmation to the requester. Fired AFTER the save and fully guarded —
|
||||
# a mail problem must never cost the customer their submission.
|
||||
mailer.send_confirmation(record, base_url=request.host_url)
|
||||
# Both emails fire AFTER the save and are fully guarded — a mail problem
|
||||
# must never cost the customer their submission.
|
||||
mailer.send_confirmation(record, base_url=request.host_url) # requester
|
||||
mailer.send_admin_notification(record, base_url=request.host_url) # JQC admins
|
||||
|
||||
return render_template('enrollment/submitted.html', reference=record['id'],
|
||||
email=requester_email)
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
{# Internal alert to JQC admins when a new enrollment form arrives. Inline
|
||||
styles only and no external assets — mail clients strip <style> blocks and
|
||||
block remote resources. #}
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<body style="font-family:Arial,Helvetica,sans-serif;color:#333;max-width:640px;margin:auto;padding:12px;">
|
||||
|
||||
<h2 style="color:#1a6fb5;margin:0 0 4px;">New enrollment form</h2>
|
||||
<p style="color:#6b7280;margin:0 0 20px;">JQC · internal notification</p>
|
||||
|
||||
<table style="border-collapse:collapse;margin:0 0 18px;">
|
||||
<tr>
|
||||
<td style="padding:4px 14px 4px 0;color:#6b7280;">Project</td>
|
||||
<td style="padding:4px 0;font-weight:bold;">{{ record.project_name }}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="padding:4px 14px 4px 0;color:#6b7280;">Requester</td>
|
||||
<td style="padding:4px 0;">
|
||||
{{ record.request_by }}
|
||||
{% if record.requester_email %}
|
||||
<<a href="mailto:{{ record.requester_email }}">{{ record.requester_email }}</a>>
|
||||
{% endif %}
|
||||
</td>
|
||||
</tr>
|
||||
{% if record.date_requested %}
|
||||
<tr>
|
||||
<td style="padding:4px 14px 4px 0;color:#6b7280;">Date requested</td>
|
||||
<td style="padding:4px 0;">{{ record.date_requested }}</td>
|
||||
</tr>
|
||||
{% endif %}
|
||||
<tr>
|
||||
<td style="padding:4px 14px 4px 0;color:#6b7280;">Reference</td>
|
||||
<td style="padding:4px 0;">{{ record.id }}</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<p style="margin:0 0 22px;">
|
||||
<a href="{{ link }}"
|
||||
style="background:#1a6fb5;color:#fff;text-decoration:none;padding:10px 18px;
|
||||
border-radius:6px;display:inline-block;font-weight:bold;">
|
||||
Open in JQC
|
||||
</a>
|
||||
</p>
|
||||
|
||||
<h3 style="font-size:1rem;margin:0 0 8px;">
|
||||
Accounts requested ({{ people | length }})
|
||||
</h3>
|
||||
|
||||
<table style="border-collapse:collapse;width:100%;font-size:.92rem;">
|
||||
<thead>
|
||||
<tr style="background:#dbeafe;">
|
||||
<th style="border:1px solid #cbd5e1;padding:6px 9px;text-align:left;">Name</th>
|
||||
<th style="border:1px solid #cbd5e1;padding:6px 9px;text-align:left;">Role</th>
|
||||
<th style="border:1px solid #cbd5e1;padding:6px 9px;text-align:left;">Email</th>
|
||||
<th style="border:1px solid #cbd5e1;padding:6px 9px;text-align:center;">Mobile App</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for person in people %}
|
||||
<tr>
|
||||
<td style="border:1px solid #cbd5e1;padding:6px 9px;">
|
||||
{{ person.name }}
|
||||
{% if person.job_title %}
|
||||
<div style="color:#6b7280;font-size:.82rem;">{{ person.job_title }}</div>
|
||||
{% endif %}
|
||||
</td>
|
||||
<td style="border:1px solid #cbd5e1;padding:6px 9px;">{{ person.role_label }}</td>
|
||||
<td style="border:1px solid #cbd5e1;padding:6px 9px;">{{ person.email }}</td>
|
||||
<td style="border:1px solid #cbd5e1;padding:6px 9px;text-align:center;">
|
||||
{{ 'Yes' if schema.wants_mobile(record, person.key) else '—' }}
|
||||
</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
{% if record.notes %}
|
||||
<h3 style="font-size:1rem;margin:22px 0 6px;">Customer notes</h3>
|
||||
<div style="white-space:pre-wrap;background:#f8fafc;border:1px solid #e5e7eb;
|
||||
border-radius:6px;padding:10px;">{{ record.notes }}</div>
|
||||
{% endif %}
|
||||
|
||||
<hr style="border:none;border-top:1px solid #e5e7eb;margin:26px 0 12px;">
|
||||
<p style="color:#9ca3af;font-size:.8rem;margin:0;">
|
||||
You are receiving this because you hold a JQC admin account. The full
|
||||
selection of tasks per person is on the enrollment page.
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user