Files
JQC_multi_tenant/app/models/facility.py
T

50 lines
1.9 KiB
Python

from app import db
from app.utils.time_utils import now_eastern
class Facility(db.Model):
__tablename__ = 'facilities'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(255), nullable=False)
address = db.Column(db.Text)
contact_person = db.Column(db.String(100))
contact_phone = db.Column(db.String(20))
active = db.Column(db.Boolean, default=True)
created_at = db.Column(db.DateTime, default=now_eastern, nullable=True)
# Phase 1: link facility to a project (nullable for backward compatibility)
project_id = db.Column(db.Integer, db.ForeignKey('projects.id', ondelete='SET NULL'),
nullable=True, index=True)
# phase38: unguessable token behind the public QR scan page (/f/<token>).
# NULL until first requested — ensure_qr_token() generates it lazily.
qr_token = db.Column(db.String(64), unique=True, nullable=True)
# Relationships
areas = db.relationship('Area', backref='facility', lazy='dynamic')
inspections = db.relationship('Inspection', backref='facility', lazy='dynamic')
def ensure_qr_token(self):
"""Generate the QR token on first use. Caller commits."""
if not self.qr_token:
import secrets
self.qr_token = secrets.token_urlsafe(32)
return self.qr_token
def __repr__(self):
return f'<Facility {self.name}>'
class Area(db.Model):
__tablename__ = 'areas'
id = db.Column(db.Integer, primary_key=True)
facility_id = db.Column(db.Integer, db.ForeignKey('facilities.id'), nullable=False)
name = db.Column(db.String(255), nullable=False)
area_type = db.Column(db.String(50))
# Relationships
inspections = db.relationship('Inspection', backref='area', lazy='dynamic')
issues = db.relationship('Issue', backref='area', lazy='dynamic')
def __repr__(self):
return f'<Area {self.name}>'