July 4 - Update with recurring/scheduled inspections

This commit is contained in:
2026-07-04 13:11:16 -04:00
parent 215a6cd920
commit 07226b4878
12 changed files with 824 additions and 5 deletions
@@ -0,0 +1,134 @@
{% extends "base.html" %}
{% block title %}{{ title }}{% endblock %}
{% block content %}
<div class="row justify-content-center">
<div class="col-lg-8">
<h2 class="mb-4"><i class="bi bi-calendar2-week"></i> {{ title }}</h2>
<form method="POST" class="card shadow-sm">
<div class="card-body">
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
<div class="mb-3">
<label class="form-label">Schedule name</label>
<input type="text" name="name" class="form-control" required
value="{{ schedule.name if schedule else '' }}"
placeholder="e.g. Weekly restroom check — Main Office">
</div>
<div class="row">
<div class="col-md-6 mb-3">
<label class="form-label">Template</label>
<select name="template_id" class="form-select" required>
<option value="">— Choose a template —</option>
{% for t in templates %}
<option value="{{ t.id }}"
{{ 'selected' if schedule and schedule.template_id == t.id }}>{{ t.name }}</option>
{% endfor %}
</select>
</div>
<div class="col-md-6 mb-3">
<label class="form-label">Frequency</label>
<select name="frequency" class="form-select" required>
{% for f in frequencies %}
<option value="{{ f }}"
{{ 'selected' if (schedule and schedule.frequency == f) or (not schedule and f == 'weekly') }}>
{{ f|title }}</option>
{% endfor %}
</select>
</div>
</div>
<div class="row">
<div class="col-md-6 mb-3">
<label class="form-label">Facility</label>
<select name="facility_id" id="facilitySelect" class="form-select" required>
<option value="">— Choose a facility —</option>
{% for f in facilities %}
<option value="{{ f.id }}"
{{ 'selected' if schedule and schedule.facility_id == f.id }}>{{ f.name }}</option>
{% endfor %}
</select>
</div>
<div class="col-md-6 mb-3">
<label class="form-label">Area <span class="text-muted small">(optional)</span></label>
<select name="area_id" id="areaSelect" class="form-select">
<option value="">— Whole facility —</option>
{% if schedule and schedule.area %}
<option value="{{ schedule.area.id }}" selected>{{ schedule.area.name }}</option>
{% endif %}
</select>
</div>
</div>
<div class="mb-3">
<label class="form-label">Assign to inspector</label>
<select name="inspector_id" class="form-select" required>
<option value="">— Choose an inspector —</option>
{% for u in inspectors %}
<option value="{{ u.id }}"
{{ 'selected' if schedule and schedule.inspector_id == u.id }}>
{{ u.display_name }} ({{ u.role }})</option>
{% endfor %}
</select>
</div>
{% if schedule %}
<div class="form-check form-switch mb-1">
<input class="form-check-input" type="checkbox" name="active" id="activeSwitch"
{{ 'checked' if schedule.active }}>
<label class="form-check-label" for="activeSwitch">Active</label>
</div>
<p class="text-muted small">
Saving recomputes the next run from now. Next run:
{{ schedule.next_run_at.strftime('%Y-%m-%d %H:%M') if schedule.next_run_at else '—' }}
</p>
{% endif %}
</div>
<div class="card-footer d-flex justify-content-between">
<a href="{{ url_for('inspection_schedules.index') }}" class="btn btn-outline-secondary">Cancel</a>
<button type="submit" class="btn btn-primary">
<i class="bi bi-check-lg"></i> Save Schedule
</button>
</div>
</form>
</div>
</div>
<script>
// Facility → Area cascade, reusing the existing inspections AJAX endpoint.
(function () {
var facilitySelect = document.getElementById('facilitySelect');
var areaSelect = document.getElementById('areaSelect');
if (!facilitySelect || !areaSelect) return;
var preselectedAreaId = {{ (schedule.area_id if schedule and schedule.area_id else 0) | tojson }};
function loadAreas(facilityId, keepSelection) {
areaSelect.innerHTML = '<option value="">— Whole facility —</option>';
if (!facilityId) return;
fetch('{{ url_for('inspections.areas_for_facility', facility_id=0) }}'.replace('/0', '/' + facilityId))
.then(function (r) { return r.json(); })
.then(function (areas) {
areas.forEach(function (a) {
var opt = document.createElement('option');
opt.value = a.id;
opt.textContent = a.name;
if (keepSelection && a.id === preselectedAreaId) opt.selected = true;
areaSelect.appendChild(opt);
});
})
.catch(function () { /* leave the whole-facility default in place */ });
}
facilitySelect.addEventListener('change', function () {
preselectedAreaId = 0;
loadAreas(this.value, false);
});
// On edit load, refresh the area list for the saved facility and keep the saved area.
if (facilitySelect.value) loadAreas(facilitySelect.value, true);
})();
</script>
{% endblock %}
@@ -0,0 +1,92 @@
{% extends "base.html" %}
{% block title %}Inspection Schedules{% endblock %}
{% block content %}
<div class="d-flex justify-content-between align-items-center mb-4">
<h2><i class="bi bi-calendar2-week"></i> Inspection Schedules</h2>
<a href="{{ url_for('inspection_schedules.create') }}" class="btn btn-primary">
<i class="bi bi-plus-circle"></i> New Schedule
</a>
</div>
<p class="text-muted small mb-4">
Recurring schedules automatically create an in-progress inspection for the
assigned inspector each period. The inspector is notified and opens it from
their Inspections list to complete it.
</p>
{% if schedules %}
<div class="card shadow-sm">
<div class="card-body p-0">
<div class="table-responsive">
<table class="table table-hover mb-0 align-middle">
<thead class="table-light">
<tr>
<th>Name</th><th>Template</th><th>Facility / Area</th>
<th>Inspector</th><th>Frequency</th><th>Next Run</th>
<th>Last Run</th><th>Status</th><th width="150"></th>
</tr>
</thead>
<tbody>
{% for s in schedules %}
<tr class="{{ 'text-muted' if not s.active else '' }}">
<td><strong>{{ s.name }}</strong></td>
<td>{{ s.template.name if s.template else '—' }}</td>
<td>
{{ s.facility.name if s.facility else '—' }}
{% if s.area %}<span class="text-muted small">/ {{ s.area.name }}</span>{% endif %}
</td>
<td>{{ s.inspector.display_name if s.inspector else '—' }}</td>
<td><span class="badge bg-secondary">{{ s.frequency|title }}</span></td>
<td class="small {{ 'text-danger fw-semibold' if s.active and s.next_run_at and s.next_run_at <= now else 'text-muted' }}">
{{ s.next_run_at.strftime('%Y-%m-%d %H:%M') if s.next_run_at else '—' }}
</td>
<td class="small text-muted">
{{ s.last_run_at.strftime('%Y-%m-%d %H:%M') if s.last_run_at else 'Never' }}
</td>
<td>
{% if s.active %}<span class="badge bg-success">Active</span>
{% else %}<span class="badge bg-secondary">Paused</span>{% endif %}
</td>
<td class="text-end">
<a href="{{ url_for('inspection_schedules.edit', schedule_id=s.id) }}"
class="btn btn-sm btn-outline-secondary" title="Edit">
<i class="bi bi-pencil"></i>
</a>
<form method="POST"
action="{{ url_for('inspection_schedules.run_now', schedule_id=s.id) }}"
class="d-inline">
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
<button type="submit" class="btn btn-sm btn-outline-primary" title="Create inspection now"
onclick="return confirm('Create an inspection from this schedule now?')">
<i class="bi bi-play-circle"></i>
</button>
</form>
<form method="POST"
action="{{ url_for('inspection_schedules.delete', schedule_id=s.id) }}"
class="d-inline"
onsubmit="return confirm('Delete this schedule? Existing inspections are not affected.')">
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
<button type="submit" class="btn btn-sm btn-outline-danger" title="Delete">
<i class="bi bi-trash3"></i>
</button>
</form>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
{% else %}
<div class="card shadow-sm">
<div class="card-body text-center py-5 text-muted">
<i class="bi bi-calendar-x fs-1 d-block mb-3 opacity-25"></i>
<p class="mb-3">No inspection schedules configured yet.</p>
<a href="{{ url_for('inspection_schedules.create') }}" class="btn btn-primary">
<i class="bi bi-plus-circle"></i> Create First Schedule
</a>
</div>
</div>
{% endif %}
{% endblock %}