July 4 - Update with recurring/scheduled inspections
This commit is contained in:
@@ -201,6 +201,7 @@ def create_app(config_name='default'):
|
||||
from app.routes import projects # Phase 1/2 — Project management
|
||||
from app.routes import customers # Phase 5 — Customer management
|
||||
from app.routes import scheduled_reports # Phase 6 — Scheduled reports
|
||||
from app.routes import inspection_schedules # phase34 — recurring inspections
|
||||
from app.routes import support # Support chat + admin tickets
|
||||
from app.routes import broadcast # Admin broadcast messages
|
||||
from app.routes import devices # Admin device management
|
||||
@@ -221,6 +222,7 @@ def create_app(config_name='default'):
|
||||
app.register_blueprint(projects.bp)
|
||||
app.register_blueprint(customers.bp)
|
||||
app.register_blueprint(scheduled_reports.bp)
|
||||
app.register_blueprint(inspection_schedules.bp)
|
||||
app.register_blueprint(support.bp)
|
||||
app.register_blueprint(broadcast.bp)
|
||||
app.register_blueprint(devices.bp)
|
||||
|
||||
@@ -5,4 +5,5 @@ from app.models.inspection import (InspectionTemplate, ChecklistItem,
|
||||
from app.models.issue import Issue
|
||||
from app.models.project import Project, CustomerAssignment
|
||||
from app.models.api_token import RefreshToken, DeviceToken
|
||||
from app.models.notification_matrix import NotificationMatrix
|
||||
from app.models.notification_matrix import NotificationMatrix
|
||||
from app.models.inspection_schedule import InspectionSchedule
|
||||
@@ -0,0 +1,68 @@
|
||||
"""
|
||||
app/models/inspection_schedule.py
|
||||
---------------------------------
|
||||
Recurring inspection schedules (phase34).
|
||||
|
||||
An InspectionSchedule declares that a given template should be inspected at a
|
||||
given facility (optionally scoped to one area) by a given inspector on a fixed
|
||||
cadence. A token-protected cron endpoint (`/inspection-schedules/run`) walks
|
||||
all active, due schedules and *materialises* a real `Inspection` row in
|
||||
`in_progress` status — exactly as if the inspector had clicked "Start
|
||||
Inspection" — then notifies the assigned inspector. The inspector opens it from
|
||||
their queue and fills it out through the normal execute flow.
|
||||
|
||||
This is purely additive: no existing inspection behaviour changes. A schedule is
|
||||
just an automated `inspections.start()`.
|
||||
"""
|
||||
|
||||
from app import db
|
||||
from app.utils.time_utils import now_eastern
|
||||
|
||||
|
||||
class InspectionSchedule(db.Model):
|
||||
__tablename__ = 'inspection_schedules'
|
||||
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
name = db.Column(db.String(255), nullable=False)
|
||||
|
||||
template_id = db.Column(
|
||||
db.Integer, db.ForeignKey('inspection_templates.id', ondelete='CASCADE'),
|
||||
nullable=False
|
||||
)
|
||||
facility_id = db.Column(
|
||||
db.Integer, db.ForeignKey('facilities.id', ondelete='CASCADE'),
|
||||
nullable=False
|
||||
)
|
||||
area_id = db.Column(
|
||||
db.Integer, db.ForeignKey('areas.id', ondelete='SET NULL'),
|
||||
nullable=True
|
||||
)
|
||||
inspector_id = db.Column(
|
||||
db.Integer, db.ForeignKey('users.id', ondelete='CASCADE'),
|
||||
nullable=False
|
||||
)
|
||||
|
||||
# daily | weekly | monthly | quarterly — mirrors InspectionTemplate.frequency
|
||||
frequency = db.Column(
|
||||
db.Enum('daily', 'weekly', 'monthly', 'quarterly'),
|
||||
nullable=False, default='weekly'
|
||||
)
|
||||
active = db.Column(db.Boolean, nullable=False, default=True)
|
||||
|
||||
created_by = db.Column(
|
||||
db.Integer, db.ForeignKey('users.id', ondelete='SET NULL'),
|
||||
nullable=True
|
||||
)
|
||||
created_at = db.Column(db.DateTime, nullable=False, default=now_eastern)
|
||||
last_run_at = db.Column(db.DateTime, nullable=True) # last successful materialisation
|
||||
next_run_at = db.Column(db.DateTime, nullable=True) # when the next inspection is due
|
||||
|
||||
# Relationships — explicit foreign_keys because two columns point at users.id.
|
||||
template = db.relationship('InspectionTemplate', foreign_keys=[template_id])
|
||||
facility = db.relationship('Facility', foreign_keys=[facility_id])
|
||||
area = db.relationship('Area', foreign_keys=[area_id])
|
||||
inspector = db.relationship('User', foreign_keys=[inspector_id])
|
||||
creator = db.relationship('User', foreign_keys=[created_by])
|
||||
|
||||
def __repr__(self):
|
||||
return f'<InspectionSchedule {self.id} {self.name!r} {self.frequency}>'
|
||||
@@ -29,6 +29,10 @@ EVENT_SCORE_ALERT = 'score_alert'
|
||||
|
||||
EVENT_ADMIN_BROADCAST = 'admin_broadcast' # bulk messages sent by admin to all apps
|
||||
|
||||
# Fired when a recurring InspectionSchedule materialises a due inspection and
|
||||
# notifies the assigned inspector (phase34).
|
||||
EVENT_INSPECTION_SCHEDULED = 'inspection_scheduled'
|
||||
|
||||
ALL_EVENT_TYPES = {
|
||||
EVENT_ISSUE_ASSIGNED: 'Issue assigned to me',
|
||||
EVENT_ISSUE_STATUS: 'Issue status changed',
|
||||
@@ -38,6 +42,7 @@ ALL_EVENT_TYPES = {
|
||||
EVENT_INSPECTION_DONE: 'Inspection completed',
|
||||
EVENT_SLA_ALERT: 'SLA at-risk / breached alerts',
|
||||
EVENT_ADMIN_BROADCAST: 'Admin broadcast (system announcements)',
|
||||
EVENT_INSPECTION_SCHEDULED: 'Scheduled inspection due (assigned to me)',
|
||||
# Customer-facing — only relevant for customer role accounts
|
||||
EVENT_CUSTOMER_INSPECTION_DONE: 'Inspection completed at my facility (portal)',
|
||||
EVENT_CUSTOMER_ISSUE_UPDATED: 'Issue created or updated at my facility (portal)',
|
||||
|
||||
@@ -0,0 +1,299 @@
|
||||
"""
|
||||
app/routes/inspection_schedules.py
|
||||
-----------------------------------
|
||||
CRUD management for recurring InspectionSchedule configs + a cron-triggered
|
||||
materialisation endpoint (phase34).
|
||||
|
||||
Management is admin / director / project_manager (@project_manager_required),
|
||||
mirroring who may start inspections. The /run route is token-protected with the
|
||||
same DIGEST_SECRET used by the other cron endpoints.
|
||||
|
||||
Cron example
|
||||
------------
|
||||
# Every day at 06:00 — materialises all schedules that have come due.
|
||||
0 6 * * * curl -s -X POST https://yourdomain.com/inspection-schedules/run \
|
||||
-d "token=YOUR_DIGEST_SECRET"
|
||||
"""
|
||||
|
||||
import logging
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from flask import (Blueprint, render_template, redirect, url_for, flash,
|
||||
request, jsonify, current_app, abort)
|
||||
from flask_login import login_required, current_user
|
||||
|
||||
from app import db, csrf
|
||||
from app.models.inspection_schedule import InspectionSchedule
|
||||
from app.models.inspection import Inspection, InspectionTemplate
|
||||
from app.models.facility import Facility, Area
|
||||
from app.models.user import User
|
||||
from app.utils.decorators import project_manager_required
|
||||
from app.utils.audit import log_action, ACTION_CREATE, ACTION_UPDATE, ACTION_DELETE
|
||||
from app.utils.time_utils import now_eastern
|
||||
from app.utils.notifications import notify
|
||||
from app.models.notification import EVENT_INSPECTION_SCHEDULED
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
bp = Blueprint('inspection_schedules', __name__, url_prefix='/inspection-schedules')
|
||||
|
||||
_FREQUENCIES = ('daily', 'weekly', 'monthly', 'quarterly')
|
||||
|
||||
|
||||
# ── Helpers ───────────────────────────────────────────────────────────────────
|
||||
|
||||
def _compute_next_run(frequency: str, from_dt: datetime = None) -> datetime:
|
||||
"""Return the next due datetime for a given cadence, at 06:00 local.
|
||||
|
||||
Monthly/quarterly advance by calendar months (targeting the same day-of-month
|
||||
is avoided — we simply add 30/90 days, which is predictable and never raises
|
||||
the datetime.replace(month=13) ValueError).
|
||||
"""
|
||||
now = from_dt or now_eastern()
|
||||
if frequency == 'daily':
|
||||
base = now + timedelta(days=1)
|
||||
elif frequency == 'weekly':
|
||||
base = now + timedelta(weeks=1)
|
||||
elif frequency == 'monthly':
|
||||
base = now + timedelta(days=30)
|
||||
else: # quarterly
|
||||
base = now + timedelta(days=90)
|
||||
return base.replace(hour=6, minute=0, second=0, microsecond=0)
|
||||
|
||||
|
||||
def _active_inspectors():
|
||||
"""Users who can be assigned inspections (inspector-capable roles)."""
|
||||
return User.query.filter(
|
||||
User.active.is_(True),
|
||||
User.role.in_(['inspector', 'project_manager', 'director', 'admin']),
|
||||
).order_by(User.full_name, User.username).all()
|
||||
|
||||
|
||||
def _materialise(schedule: InspectionSchedule, when: datetime) -> Inspection:
|
||||
"""Create an in_progress Inspection from a schedule and notify the inspector.
|
||||
|
||||
Does NOT commit — the caller commits after advancing schedule bookkeeping so
|
||||
the new inspection and the schedule update land in one transaction.
|
||||
"""
|
||||
inspection = Inspection(
|
||||
template_id = schedule.template_id,
|
||||
facility_id = schedule.facility_id,
|
||||
area_id = schedule.area_id,
|
||||
inspector_id = schedule.inspector_id,
|
||||
inspection_date = when,
|
||||
status = 'in_progress',
|
||||
notes = None,
|
||||
)
|
||||
db.session.add(inspection)
|
||||
db.session.flush() # assign inspection.id without committing
|
||||
|
||||
inspector = schedule.inspector
|
||||
if inspector is not None:
|
||||
fac_name = schedule.facility.name if schedule.facility else 'a facility'
|
||||
notify(
|
||||
inspector,
|
||||
title='Scheduled inspection due',
|
||||
body=(f'A recurring inspection "{schedule.name}" at {fac_name} '
|
||||
f'is due. Open it from your inspections list to begin.'),
|
||||
link=url_for('inspections.execute', inspection_id=inspection.id),
|
||||
inspection_id=inspection.id,
|
||||
event_type=EVENT_INSPECTION_SCHEDULED,
|
||||
)
|
||||
return inspection
|
||||
|
||||
|
||||
# ── CRUD ──────────────────────────────────────────────────────────────────────
|
||||
|
||||
@bp.route('/')
|
||||
@login_required
|
||||
@project_manager_required
|
||||
def index():
|
||||
schedules = InspectionSchedule.query.order_by(
|
||||
InspectionSchedule.active.desc(), InspectionSchedule.name
|
||||
).all()
|
||||
return render_template('inspection_schedules/index.html',
|
||||
schedules=schedules, now=now_eastern())
|
||||
|
||||
|
||||
def _form_choices():
|
||||
templates = InspectionTemplate.query.filter_by(active=True).order_by(InspectionTemplate.name).all()
|
||||
facilities = Facility.query.filter_by(active=True).order_by(Facility.name).all()
|
||||
inspectors = _active_inspectors()
|
||||
return templates, facilities, inspectors
|
||||
|
||||
|
||||
@bp.route('/new', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
@project_manager_required
|
||||
def create():
|
||||
templates, facilities, inspectors = _form_choices()
|
||||
|
||||
if request.method == 'POST':
|
||||
name = request.form.get('name', '').strip()
|
||||
template_id = request.form.get('template_id', type=int)
|
||||
facility_id = request.form.get('facility_id', type=int)
|
||||
area_id = request.form.get('area_id', type=int) or None
|
||||
inspector_id = request.form.get('inspector_id', type=int)
|
||||
frequency = request.form.get('frequency', 'weekly')
|
||||
|
||||
errors = []
|
||||
if not name:
|
||||
errors.append('A schedule name is required.')
|
||||
if not template_id or db.session.get(InspectionTemplate, template_id) is None:
|
||||
errors.append('Please choose a valid template.')
|
||||
if not facility_id or db.session.get(Facility, facility_id) is None:
|
||||
errors.append('Please choose a valid facility.')
|
||||
if not inspector_id or db.session.get(User, inspector_id) is None:
|
||||
errors.append('Please choose a valid inspector.')
|
||||
if frequency not in _FREQUENCIES:
|
||||
errors.append('Invalid frequency.')
|
||||
|
||||
if errors:
|
||||
for e in errors:
|
||||
flash(e, 'warning')
|
||||
return render_template('inspection_schedules/form.html',
|
||||
templates=templates, facilities=facilities,
|
||||
inspectors=inspectors, frequencies=_FREQUENCIES,
|
||||
title='New Inspection Schedule')
|
||||
|
||||
schedule = InspectionSchedule(
|
||||
name = name,
|
||||
template_id = template_id,
|
||||
facility_id = facility_id,
|
||||
area_id = area_id,
|
||||
inspector_id = inspector_id,
|
||||
frequency = frequency,
|
||||
active = True,
|
||||
created_by = current_user.id,
|
||||
created_at = now_eastern(),
|
||||
next_run_at = _compute_next_run(frequency),
|
||||
)
|
||||
db.session.add(schedule)
|
||||
db.session.commit()
|
||||
log_action(ACTION_CREATE, 'InspectionSchedule', schedule.id, schedule.name,
|
||||
f'frequency={frequency}; template_id={template_id}; facility_id={facility_id}')
|
||||
flash(f'Inspection schedule "{schedule.name}" created.', 'success')
|
||||
return redirect(url_for('inspection_schedules.index'))
|
||||
|
||||
return render_template('inspection_schedules/form.html',
|
||||
templates=templates, facilities=facilities,
|
||||
inspectors=inspectors, frequencies=_FREQUENCIES,
|
||||
title='New Inspection Schedule')
|
||||
|
||||
|
||||
@bp.route('/<int:schedule_id>/edit', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
@project_manager_required
|
||||
def edit(schedule_id):
|
||||
schedule = db.session.get(InspectionSchedule, schedule_id)
|
||||
if schedule is None:
|
||||
abort(404)
|
||||
templates, facilities, inspectors = _form_choices()
|
||||
|
||||
if request.method == 'POST':
|
||||
schedule.name = request.form.get('name', '').strip() or schedule.name
|
||||
template_id = request.form.get('template_id', type=int)
|
||||
facility_id = request.form.get('facility_id', type=int)
|
||||
inspector_id = request.form.get('inspector_id', type=int)
|
||||
frequency = request.form.get('frequency', schedule.frequency)
|
||||
|
||||
if template_id and db.session.get(InspectionTemplate, template_id):
|
||||
schedule.template_id = template_id
|
||||
if facility_id and db.session.get(Facility, facility_id):
|
||||
schedule.facility_id = facility_id
|
||||
if inspector_id and db.session.get(User, inspector_id):
|
||||
schedule.inspector_id = inspector_id
|
||||
schedule.area_id = request.form.get('area_id', type=int) or None
|
||||
if frequency in _FREQUENCIES:
|
||||
schedule.frequency = frequency
|
||||
schedule.active = bool(request.form.get('active'))
|
||||
# Recompute the next run from now against the (possibly changed) cadence.
|
||||
schedule.next_run_at = _compute_next_run(schedule.frequency)
|
||||
|
||||
db.session.commit()
|
||||
log_action(ACTION_UPDATE, 'InspectionSchedule', schedule.id, schedule.name,
|
||||
f'frequency={schedule.frequency}; active={schedule.active}')
|
||||
flash(f'Inspection schedule "{schedule.name}" updated.', 'success')
|
||||
return redirect(url_for('inspection_schedules.index'))
|
||||
|
||||
return render_template('inspection_schedules/form.html', schedule=schedule,
|
||||
templates=templates, facilities=facilities,
|
||||
inspectors=inspectors, frequencies=_FREQUENCIES,
|
||||
title='Edit Inspection Schedule')
|
||||
|
||||
|
||||
@bp.route('/<int:schedule_id>/delete', methods=['POST'])
|
||||
@login_required
|
||||
@project_manager_required
|
||||
def delete(schedule_id):
|
||||
schedule = db.session.get(InspectionSchedule, schedule_id)
|
||||
if schedule is None:
|
||||
abort(404)
|
||||
name = schedule.name
|
||||
sid = schedule.id
|
||||
db.session.delete(schedule)
|
||||
db.session.commit()
|
||||
log_action(ACTION_DELETE, 'InspectionSchedule', sid, name)
|
||||
flash(f'Inspection schedule "{name}" deleted.', 'success')
|
||||
return redirect(url_for('inspection_schedules.index'))
|
||||
|
||||
|
||||
@bp.route('/<int:schedule_id>/run-now', methods=['POST'])
|
||||
@login_required
|
||||
@project_manager_required
|
||||
def run_now(schedule_id):
|
||||
"""Manually materialise one inspection from a schedule — for testing / ad-hoc."""
|
||||
schedule = db.session.get(InspectionSchedule, schedule_id)
|
||||
if schedule is None:
|
||||
abort(404)
|
||||
now = now_eastern()
|
||||
inspection = _materialise(schedule, now)
|
||||
schedule.last_run_at = now
|
||||
schedule.next_run_at = _compute_next_run(schedule.frequency, now)
|
||||
db.session.commit()
|
||||
log_action(ACTION_UPDATE, 'InspectionSchedule', schedule.id, schedule.name,
|
||||
f'manual run_now by {current_user.username}; inspection_id={inspection.id}')
|
||||
flash(f'Inspection created from "{schedule.name}". It is now in the inspector\'s queue.',
|
||||
'success')
|
||||
return redirect(url_for('inspection_schedules.index'))
|
||||
|
||||
|
||||
# ── Cron endpoint ─────────────────────────────────────────────────────────────
|
||||
|
||||
@bp.route('/run', methods=['POST'])
|
||||
@csrf.exempt
|
||||
def run():
|
||||
"""Token-protected endpoint called by cron to materialise all due schedules.
|
||||
|
||||
POST body: token=<DIGEST_SECRET>
|
||||
"""
|
||||
token = request.form.get('token') or request.args.get('token')
|
||||
expected = current_app.config.get('DIGEST_SECRET')
|
||||
|
||||
if not expected or token != expected:
|
||||
logger.warning('INSPECTION SCHEDULES RUN REJECTED | bad/missing token')
|
||||
return jsonify({'ok': False, 'error': 'unauthorized'}), 403
|
||||
|
||||
now = now_eastern()
|
||||
schedules = InspectionSchedule.query.filter_by(active=True).all()
|
||||
due = [s for s in schedules if s.next_run_at is None or s.next_run_at <= now]
|
||||
|
||||
created = 0
|
||||
for schedule in due:
|
||||
try:
|
||||
inspection = _materialise(schedule, now)
|
||||
schedule.last_run_at = now
|
||||
schedule.next_run_at = _compute_next_run(schedule.frequency, now)
|
||||
created += 1
|
||||
logger.info('INSPECTION SCHEDULE MATERIALISED | schedule_id=%s | inspection_id=%s',
|
||||
schedule.id, inspection.id)
|
||||
except Exception as exc:
|
||||
# Advance next_run_at anyway so one broken schedule can't wedge the
|
||||
# whole cron run on every subsequent tick.
|
||||
schedule.next_run_at = _compute_next_run(schedule.frequency, now)
|
||||
logger.error('INSPECTION SCHEDULE FAILED | schedule_id=%s | error=%s',
|
||||
schedule.id, exc)
|
||||
db.session.commit()
|
||||
|
||||
logger.info('INSPECTION SCHEDULES CRON | due=%s | created=%s', len(due), created)
|
||||
return jsonify({'ok': True, 'due': len(due), 'created': created})
|
||||
@@ -154,6 +154,11 @@
|
||||
<li class="nav-item">
|
||||
<a class="nav-link {{ 'active' if request.endpoint and request.endpoint.startswith('inspections.') }}" href="{{ url_for('inspections.index') }}">Inspections</a>
|
||||
</li>
|
||||
{% if current_user.role in ['admin', 'director', 'project_manager'] %}
|
||||
<li class="nav-item">
|
||||
<a class="nav-link {{ 'active' if request.endpoint and request.endpoint.startswith('inspection_schedules.') }}" href="{{ url_for('inspection_schedules.index') }}">Schedules</a>
|
||||
</li>
|
||||
{% endif %}
|
||||
<li class="nav-item">
|
||||
<a class="nav-link {{ 'active' if request.endpoint and request.endpoint.startswith('issues.') and request.endpoint != 'issues.verification_queue' }}" href="{{ url_for('issues.index') }}">Issues</a>
|
||||
</li>
|
||||
|
||||
@@ -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 %}
|
||||
Reference in New Issue
Block a user