Jul 8 - Update scheduled inspection 3: add assignee notification
This commit is contained in:
@@ -55,6 +55,28 @@ def _active_contracts():
|
|||||||
return Project.query.filter_by(active=True).order_by(Project.name).all()
|
return Project.query.filter_by(active=True).order_by(Project.name).all()
|
||||||
|
|
||||||
|
|
||||||
|
def _notify_assignee(sched, reassigned=False):
|
||||||
|
"""Send an immediate in-app + email notification to the assigned inspector
|
||||||
|
that a scheduled inspection was assigned (or reassigned) to them.
|
||||||
|
No-op when there is no active inspector. Caller commits."""
|
||||||
|
inspector = sched.inspector
|
||||||
|
if not inspector or not inspector.active:
|
||||||
|
return
|
||||||
|
fac = sched.facility.name if sched.facility else '—'
|
||||||
|
tpl = sched.template.name if sched.template else '—'
|
||||||
|
verb = 'reassigned to you' if reassigned else 'assigned to you'
|
||||||
|
notify(
|
||||||
|
recipient = inspector,
|
||||||
|
title = f'Scheduled inspection {verb} — {fac}',
|
||||||
|
body = (f'A "{tpl}" inspection at {fac} has been {verb} '
|
||||||
|
f'({sched.frequency_label.lower()}), due '
|
||||||
|
f'{sched.next_due_date:%b %d, %Y}.'),
|
||||||
|
link = url_for('scheduled_inspections.index'),
|
||||||
|
event_type = EVENT_SCHEDULED_INSPECTION,
|
||||||
|
send_email = True,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def _selected_project_id(form):
|
def _selected_project_id(form):
|
||||||
"""Contract of the submitted facility (for restoring the selector on
|
"""Contract of the submitted facility (for restoring the selector on
|
||||||
re-render), or None."""
|
re-render), or None."""
|
||||||
@@ -117,6 +139,11 @@ def create():
|
|||||||
f'{sched.template.name} @ {sched.facility.name}',
|
f'{sched.template.name} @ {sched.facility.name}',
|
||||||
f'freq={sched.frequency}; due={sched.next_due_date}; inspector={sched.inspector_id}')
|
f'freq={sched.frequency}; due={sched.next_due_date}; inspector={sched.inspector_id}')
|
||||||
logger.info('SCHED INSP | create | by=%s | id=%s', current_user.username, sched.id)
|
logger.info('SCHED INSP | create | by=%s | id=%s', current_user.username, sched.id)
|
||||||
|
|
||||||
|
# Notify the assigned inspector immediately.
|
||||||
|
_notify_assignee(sched, reassigned=False)
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
flash('Scheduled inspection created.', 'success')
|
flash('Scheduled inspection created.', 'success')
|
||||||
return redirect(url_for('scheduled_inspections.index'))
|
return redirect(url_for('scheduled_inspections.index'))
|
||||||
|
|
||||||
@@ -139,6 +166,7 @@ def edit(schedule_id):
|
|||||||
_populate_choices(form)
|
_populate_choices(form)
|
||||||
|
|
||||||
if form.validate_on_submit():
|
if form.validate_on_submit():
|
||||||
|
old_inspector_id = sched.inspector_id
|
||||||
sched.facility_id = form.facility_id.data
|
sched.facility_id = form.facility_id.data
|
||||||
sched.template_id = form.template_id.data
|
sched.template_id = form.template_id.data
|
||||||
sched.inspector_id = form.inspector_id.data
|
sched.inspector_id = form.inspector_id.data
|
||||||
@@ -150,6 +178,12 @@ def edit(schedule_id):
|
|||||||
log_action(ACTION_UPDATE, 'ScheduledInspection', sched.id,
|
log_action(ACTION_UPDATE, 'ScheduledInspection', sched.id,
|
||||||
f'{sched.template.name} @ {sched.facility.name}',
|
f'{sched.template.name} @ {sched.facility.name}',
|
||||||
f'freq={sched.frequency}; due={sched.next_due_date}; active={sched.active}')
|
f'freq={sched.frequency}; due={sched.next_due_date}; active={sched.active}')
|
||||||
|
|
||||||
|
# Notify the inspector if the assignment changed to them.
|
||||||
|
if sched.active and sched.inspector_id and sched.inspector_id != old_inspector_id:
|
||||||
|
_notify_assignee(sched, reassigned=True)
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
flash('Scheduled inspection updated.', 'success')
|
flash('Scheduled inspection updated.', 'success')
|
||||||
return redirect(url_for('scheduled_inspections.index'))
|
return redirect(url_for('scheduled_inspections.index'))
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user