Apr 2 2026: implement notification settings matrix
This commit is contained in:
+53
-1
@@ -230,4 +230,56 @@ def toggle_active(user_id):
|
||||
f'account {action_label} by {current_user.username}',
|
||||
)
|
||||
flash(f'User {user.username} has been {action_label}.', 'success')
|
||||
return redirect(request.referrer or url_for('auth.list_users'))
|
||||
return redirect(request.referrer or url_for('auth.list_users'))
|
||||
|
||||
# ── Notification Matrix ───────────────────────────────────────────────────────
|
||||
|
||||
@bp.route('/notification-matrix', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
@admin_required
|
||||
def notification_matrix():
|
||||
"""Admin-only notification matrix — controls who receives each event type."""
|
||||
import json as _json
|
||||
from app.models.notification_matrix import (
|
||||
NotificationMatrix, MATRIX_EVENTS, MATRIX_ROLES, MATRIX_DEFAULTS,
|
||||
)
|
||||
|
||||
if request.method == 'POST':
|
||||
for event_key in MATRIX_EVENTS:
|
||||
for role_key, _ in MATRIX_ROLES:
|
||||
row = NotificationMatrix.query.filter_by(
|
||||
event_type=event_key, role_key=role_key
|
||||
).first()
|
||||
if row is None:
|
||||
row = NotificationMatrix(event_type=event_key, role_key=role_key)
|
||||
db.session.add(row)
|
||||
|
||||
if role_key == 'custom':
|
||||
raw = request.form.get(f'custom_{event_key}', '').strip()
|
||||
# Parse comma-separated emails into a JSON list
|
||||
emails = [e.strip() for e in raw.split(',') if e.strip()]
|
||||
row.custom_emails = _json.dumps(emails)
|
||||
row.enabled = bool(emails)
|
||||
else:
|
||||
row.enabled = bool(request.form.get(f'matrix_{event_key}_{role_key}'))
|
||||
|
||||
db.session.commit()
|
||||
log_action(ACTION_UPDATE, 'NotificationMatrix', None,
|
||||
'Notification Matrix', 'admin updated notification matrix')
|
||||
logger.info('NOTIFICATION MATRIX UPDATED | by=%s', current_user.username)
|
||||
flash('Notification matrix saved successfully.', 'success')
|
||||
return redirect(url_for('auth.notification_matrix'))
|
||||
|
||||
# Build current state dict: {event_key: {role_key: enabled/emails}}
|
||||
all_rows = NotificationMatrix.query.all()
|
||||
state = {} # event_key -> role_key -> row
|
||||
for row in all_rows:
|
||||
state.setdefault(row.event_type, {})[row.role_key] = row
|
||||
|
||||
return render_template(
|
||||
'auth/notification_matrix.html',
|
||||
matrix_events = MATRIX_EVENTS,
|
||||
matrix_roles = MATRIX_ROLES,
|
||||
defaults = MATRIX_DEFAULTS,
|
||||
state = state,
|
||||
)
|
||||
|
||||
+18
-32
@@ -15,7 +15,7 @@ from app.models.user import User
|
||||
from app.utils.forms import StartInspectionForm, IssueForm
|
||||
from app.utils.decorators import supervisor_required
|
||||
from app.utils.pdf_export import generate_inspection_pdf
|
||||
from app.utils.notifications import notify, notify_customers_for_facility
|
||||
from app.utils.notifications import notify, notify_customers_for_facility, notify_by_matrix
|
||||
from app.models.notification import (
|
||||
EVENT_INSPECTION_DONE, EVENT_ISSUE_ASSIGNED,
|
||||
EVENT_CUSTOMER_INSPECTION_DONE, EVENT_CUSTOMER_ISSUE_UPDATED,
|
||||
@@ -371,36 +371,21 @@ def execute(inspection_id):
|
||||
_save_responses(inspection, responses)
|
||||
db.session.commit()
|
||||
|
||||
supervisors = User.query.filter(User.role.in_(['admin', 'supervisor'])).all()
|
||||
inspection_link = url_for('inspections.view', inspection_id=inspection.id)
|
||||
score_display = f'{score:.1f}%' if score is not None else 'N/A'
|
||||
for supervisor in supervisors:
|
||||
if supervisor.id != current_user.id:
|
||||
notify(
|
||||
recipient = supervisor,
|
||||
title = f'Inspection #{inspection.id} Completed',
|
||||
body = (
|
||||
f'{current_user.username} completed an inspection at '
|
||||
f'{inspection.facility.name} using the '
|
||||
f'"{inspection.template.name}" template. '
|
||||
f'Overall score: {score_display}.'
|
||||
),
|
||||
link = inspection_link,
|
||||
inspection_id = inspection.id,
|
||||
event_type = EVENT_INSPECTION_DONE,
|
||||
send_email = True,
|
||||
)
|
||||
notify_customers_for_facility(
|
||||
facility_id = inspection.facility_id,
|
||||
event_type = EVENT_CUSTOMER_INSPECTION_DONE,
|
||||
title = f'Inspection Completed at {inspection.facility.name}',
|
||||
notify_by_matrix(
|
||||
event_type = 'inspection_completed',
|
||||
title = f'Inspection #{inspection.id} Completed',
|
||||
body = (
|
||||
f'An inspection using the "{inspection.template.name}" template '
|
||||
f'was completed at {inspection.facility.name}. '
|
||||
f'{current_user.username} completed an inspection at '
|
||||
f'{inspection.facility.name} using the '
|
||||
f'"{inspection.template.name}" template. '
|
||||
f'Overall score: {score_display}.'
|
||||
),
|
||||
link = url_for('inspections.view', inspection_id=inspection.id),
|
||||
link = inspection_link,
|
||||
inspection_id = inspection.id,
|
||||
facility_id = inspection.facility_id,
|
||||
exclude_user_ids = {current_user.id},
|
||||
)
|
||||
db.session.commit()
|
||||
log_action(ACTION_UPDATE, 'Inspection', inspection.id,
|
||||
@@ -736,19 +721,20 @@ def flag_issue(inspection_id):
|
||||
)
|
||||
db.session.commit()
|
||||
|
||||
notify_customers_for_facility(
|
||||
facility_id = inspection.facility_id,
|
||||
event_type = EVENT_CUSTOMER_ISSUE_UPDATED,
|
||||
title = f'New Issue #{issue.id} at {inspection.facility.name}',
|
||||
body = (
|
||||
notify_by_matrix(
|
||||
event_type = 'issue_flagged',
|
||||
title = f'New Issue #{issue.id} at {inspection.facility.name}',
|
||||
body = (
|
||||
f'A new {issue.severity.title()}-severity issue has been logged '
|
||||
f'in {issue.area.name} at {inspection.facility.name} '
|
||||
f'during inspection #{inspection_id}. '
|
||||
f'Description: {issue.description[:120]}'
|
||||
f'{"…" if len(issue.description) > 120 else ""}'
|
||||
),
|
||||
link = url_for('issues.view', issue_id=issue.id),
|
||||
issue_id = issue.id,
|
||||
link = url_for('issues.view', issue_id=issue.id),
|
||||
issue_id = issue.id,
|
||||
facility_id = inspection.facility_id,
|
||||
exclude_user_ids = {current_user.id},
|
||||
)
|
||||
db.session.commit()
|
||||
|
||||
|
||||
+35
-39
@@ -14,7 +14,7 @@ from app.models.notification import (
|
||||
)
|
||||
from app.utils.forms import IssueForm, IssueUpdateForm
|
||||
from app.utils.decorators import supervisor_required
|
||||
from app.utils.notifications import notify, notify_customers_for_facility
|
||||
from app.utils.notifications import notify, notify_customers_for_facility, notify_by_matrix
|
||||
from app.utils.audit import log_action, ACTION_CREATE, ACTION_UPDATE, ACTION_DELETE
|
||||
from app.utils.scope import get_customer_scope
|
||||
from app.utils.sla import sla_status
|
||||
@@ -268,21 +268,22 @@ def view(issue_id):
|
||||
exclude_user_ids = exclude_ids,
|
||||
)
|
||||
|
||||
# ── Notify customer portal users for this facility ──────────
|
||||
# ── Notify via matrix (issue_updated_customer) ───────────────
|
||||
facility_id = issue.area.facility_id if issue.area else None
|
||||
if facility_id:
|
||||
changes_summary = '; '.join(changes) if changes else 'updated'
|
||||
notify_customers_for_facility(
|
||||
facility_id = facility_id,
|
||||
event_type = EVENT_CUSTOMER_ISSUE_UPDATED,
|
||||
title = f'Issue #{issue.id} Updated at {issue.area.facility.name}',
|
||||
body = (
|
||||
if facility_id and changes:
|
||||
changes_summary = '; '.join(changes)
|
||||
notify_by_matrix(
|
||||
event_type = 'issue_updated_customer',
|
||||
title = f'Issue #{issue.id} Updated at {issue.area.facility.name}',
|
||||
body = (
|
||||
f'Issue #{issue.id} ({issue.severity.title()} severity) '
|
||||
f'in {issue.area.name} was updated: {changes_summary}. '
|
||||
f'Current status: {issue.status.replace("_", " ").title()}.'
|
||||
),
|
||||
link = url_for('issues.view', issue_id=issue.id),
|
||||
issue_id = issue.id,
|
||||
link = url_for('issues.view', issue_id=issue.id),
|
||||
issue_id = issue.id,
|
||||
facility_id = facility_id,
|
||||
exclude_user_ids = {current_user.id},
|
||||
)
|
||||
db.session.commit() # Commit all notifications
|
||||
log_action(ACTION_UPDATE, 'Issue', issue.id,
|
||||
@@ -398,21 +399,22 @@ def create():
|
||||
)
|
||||
db.session.commit()
|
||||
|
||||
# ── Notify customer portal users for this facility ──────────
|
||||
# ── Notify via matrix (issue_created) ────────────────────────
|
||||
area = db.session.get(Area, issue.area_id)
|
||||
if area:
|
||||
notify_customers_for_facility(
|
||||
facility_id = area.facility_id,
|
||||
event_type = EVENT_CUSTOMER_ISSUE_UPDATED,
|
||||
title = f'New Issue #{issue.id} at {area.facility.name}',
|
||||
body = (
|
||||
notify_by_matrix(
|
||||
event_type = 'issue_created',
|
||||
title = f'New Issue #{issue.id} at {area.facility.name}',
|
||||
body = (
|
||||
f'A new {issue.severity.title()}-severity issue has been logged '
|
||||
f'in {area.name} at {area.facility.name}. '
|
||||
f'Description: {issue.description[:120]}'
|
||||
f'{"…" if len(issue.description) > 120 else ""}'
|
||||
),
|
||||
link = url_for('issues.view', issue_id=issue.id),
|
||||
issue_id = issue.id,
|
||||
link = url_for('issues.view', issue_id=issue.id),
|
||||
issue_id = issue.id,
|
||||
facility_id = area.facility_id,
|
||||
exclude_user_ids = {current_user.id},
|
||||
)
|
||||
db.session.commit()
|
||||
flash('Issue created.', 'success')
|
||||
@@ -489,25 +491,19 @@ def request_verification(issue_id):
|
||||
f'#{issue_id} in {issue.area.name}',
|
||||
f'status=pending_verification; requested_by={current_user.username}')
|
||||
|
||||
# Notify supervisors
|
||||
from app.utils.notifications import notify
|
||||
from app.models.notification import EVENT_ISSUE_STATUS
|
||||
supervisors = User.query.filter(User.role.in_(['admin', 'supervisor'])).all()
|
||||
for sup in supervisors:
|
||||
if sup.id != current_user.id:
|
||||
notify(
|
||||
recipient = sup,
|
||||
title = f'Issue #{issue_id} Awaiting Verification',
|
||||
body = (
|
||||
f'{current_user.username} has marked Issue #{issue_id} '
|
||||
f'({issue.severity.title()} severity) in {issue.area.name} '
|
||||
f'as pending your verification.'
|
||||
),
|
||||
link = url_for('issues.view', issue_id=issue_id),
|
||||
issue_id = issue_id,
|
||||
event_type = EVENT_ISSUE_STATUS,
|
||||
send_email = True,
|
||||
)
|
||||
# Notify via matrix (verification_requested)
|
||||
notify_by_matrix(
|
||||
event_type = 'verification_requested',
|
||||
title = f'Issue #{issue_id} Awaiting Verification',
|
||||
body = (
|
||||
f'{current_user.username} has marked Issue #{issue_id} '
|
||||
f'({issue.severity.title()} severity) in {issue.area.name} '
|
||||
f'as pending your verification.'
|
||||
),
|
||||
link = url_for('issues.view', issue_id=issue_id),
|
||||
issue_id = issue_id,
|
||||
exclude_user_ids = {current_user.id},
|
||||
)
|
||||
db.session.commit()
|
||||
flash('Issue marked as pending verification. Supervisors have been notified.', 'info')
|
||||
return redirect(url_for('issues.view', issue_id=issue_id))
|
||||
@@ -601,4 +597,4 @@ def delete(issue_id):
|
||||
f'facility={facility_name}; description={issue_desc}')
|
||||
|
||||
flash(f'Issue #{issue_id_snap} has been permanently deleted.', 'success')
|
||||
return redirect(url_for('issues.index'))
|
||||
return redirect(url_for('issues.index'))
|
||||
|
||||
Reference in New Issue
Block a user