Aug 4 - Update code to follow up - MT12b

This commit is contained in:
2026-08-04 13:37:29 -04:00
parent 73ed0157fc
commit f4c80cfcef
7 changed files with 762 additions and 2 deletions
+22 -1
View File
@@ -230,6 +230,12 @@ def _materialise(schedule: InspectionSchedule, when: datetime) -> Inspection:
status = 'in_progress',
notes = schedule.notes,
inspection_schedule_id = schedule.id, # phase43 — link back to the plan
# phase48 — a schedule created by "Schedule Follow-up" carries the
# inspection it answers. Inheriting it here is what makes the run a real
# linked re-inspection: execute() pre-fills from the parent and submit
# clears the parent's follow_up_required. NULL for ordinary schedules,
# which is every pre-phase48 row.
parent_inspection_id = schedule.parent_inspection_id,
)
db.session.add(inspection)
db.session.flush() # assign inspection.id without committing
@@ -546,6 +552,18 @@ def start(schedule_id):
flash('The template for this schedule has no form fields yet.', 'warning')
return redirect(url_for('inspection_schedules.index'))
# Already started but not submitted? Resume it rather than opening a second
# inspection against the same occurrence. Without this, a manager and the
# inspector both pressing Start — or one double-tap — leaves two in_progress
# rows against one schedule, only one of which fulfils it on submit.
existing = (Inspection.query
.filter_by(inspection_schedule_id=schedule.id, status='in_progress')
.order_by(Inspection.id.desc())
.first())
if existing is not None:
flash('Resuming the inspection you already started for this schedule.', 'info')
return redirect(url_for('inspections.execute', inspection_id=existing.id))
inspection = Inspection(
template_id = schedule.template_id,
facility_id = schedule.facility_id,
@@ -555,12 +573,15 @@ def start(schedule_id):
status = 'in_progress',
notes = schedule.notes,
inspection_schedule_id = schedule.id,
# phase48 — see _materialise().
parent_inspection_id = schedule.parent_inspection_id,
)
db.session.add(inspection)
db.session.commit()
log_action(ACTION_CREATE, 'Inspection', inspection.id,
f'{inspection.template.name} @ {inspection.facility.name}',
f'started from inspection_schedule_id={schedule.id}')
f'started from inspection_schedule_id={schedule.id}; '
f'parent_inspection_id={schedule.parent_inspection_id}')
logger.info('INSPECTION SCHEDULE | start | schedule=%s | inspection=%s | by=%s',
schedule.id, inspection.id, current_user.username)
flash('Inspection started from schedule. Complete and submit the form below.', 'info')