feat(email): each account gets its own email, in its own zone (milestone 495)
CI / lint (push) Successful in 3s
CI / integration (push) Successful in 1m0s
CI / unit (push) Successful in 1m24s
CI / publish (push) Successful in 1m32s

Milestone 495 "Email goes to each account, in its own time zone", on the
operator's ruling (System 156): notifications go to each user, times in
their zone, defaults on, toggled per user or per role.

- users.notify_alerts / notify_report (migration 0036): None follows the
  role's default, notify.<kind>.<role> in settings, all on to start.
- mail.pick_recipients: active accounts that want the email and have an
  address mail can reach (an SSO placeholder like name@oidc is skipped and
  flagged), each in its own zone with no UTC beside it, then the Also send
  to addresses in their chosen zone with UTC beside. One copy per address,
  all over one SMTP connection.
- Alerts, the weekly report and the test email (to you, in your zone, plus
  the typed addresses) send that way. The save-time smtp.tz capture is gone.
- Notifications: a role × email table of defaults, who each email reaches
  and who can't be reached, and Also send to with its own zone, which starts
  as the first admin's to open the page.
- Account page: your address and your two switches. Users: an Emailed
  column, and an admin sets an account's switches on its page.
- The Alerts page counts the accounts it emails. zone_select macro and
  formatting.zones_by_region shared by Account and Notifications.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-08 15:16:17 -04:00
co-authored by Claude Opus 5.5
parent c533eec33a
commit 22090b02d8
27 changed files with 601 additions and 132 deletions
+26
View File
@@ -16,7 +16,9 @@ from sqlalchemy import func, or_, select
from sqlalchemy.exc import IntegrityError
from steward.auth.middleware import require_role
from steward.core import mail
from steward.core.audit import audit_request
from steward.core.settings import get_all_settings
from steward.models.alerts import AlertRule, MaintenanceWindow
from steward.models.ansible import AnsibleRun
from steward.models.api_tokens import ApiToken
@@ -102,9 +104,13 @@ async def users_list():
users = (await db.execute(select(User))).scalars().all()
last = await _last_sign_ins(db)
tokens = await _token_counts(db)
settings = await get_all_settings(db)
users = sorted(users, key=lambda u: (not u.is_active, u.username.lower()))
gets = {u.id: [label for kind, label in (("alerts", "alerts"), ("report", "report"))
if u.is_active and mail.wants(u, kind, settings)] for u in users}
return await render_template(
"settings/users.html", users=users, last=last, tokens=tokens, sources=SOURCES,
gets=gets, reachable={u.id for u in users if mail.is_real_address(u.email)},
admins=sum(1 for u in users if u.role == UserRole.admin and u.is_active),
disabled=sum(1 for u in users if not u.is_active))
@@ -150,6 +156,7 @@ async def user_create():
_DONE = {"created": "Account added.", "saved": "Saved.", "role": "Role changed.",
"email": "Email settings saved.",
"password": "Password set. It works from their next sign-in.",
"disabled": "Account disabled. They are signed out at their next click.",
"enabled": "Account enabled."}
@@ -162,9 +169,11 @@ async def _user_page(user_id: str, error: str | None = None, status: int = 200):
last = (await _last_sign_ins(db)).get(user.id)
tokens = (await _token_counts(db)).get(user.id, 0)
holds = await held_by(db, user.id)
defaults = mail.role_defaults(user, await get_all_settings(db))
me = session.get("user_id")
return await render_template(
"settings/user.html", user=user, last_sign_in=last, tokens=tokens, holds=holds,
defaults=defaults, reachable=mail.is_real_address(user.email),
source=SOURCES.get(user.auth_source, user.auth_source), roles=list(UserRole),
min_password=MIN_PASSWORD, error=error, done=_DONE.get(request.args.get("done", "")),
demote_refused=refusal("demote", user, me, admins),
@@ -231,6 +240,23 @@ async def user_role(user_id: str):
return redirect(f"/settings/users/{user_id}?done=role")
@users_bp.post("/<user_id>/email")
@require_role(UserRole.admin)
async def user_email(user_id: str):
"""Whether this account is emailed alerts and the weekly report (milestone
495); its address is edited with its name."""
form = {k: v for k, v in (await request.form).items() if k != "email"}
async with current_app.db_sessionmaker() as db:
async with db.begin():
user = await _target(db, user_id)
changes, error = mail.apply_email_form(user, form)
if error:
return await _user_page(user_id, error, 400)
if changes:
await audit_request("user.email_settings", "user", user_id, {"username": user.username} | changes)
return redirect(f"/settings/users/{user_id}?done=email")
@users_bp.post("/<user_id>/password")
@require_role(UserRole.admin)
async def user_password(user_id: str):