Files
JQC_multi_tenant/app/models/inspection_schedule.py
T

69 lines
2.8 KiB
Python

"""
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}>'