Phase 2: implement template form editor
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
from app import db
|
||||
from datetime import datetime
|
||||
import json
|
||||
|
||||
|
||||
class InspectionTemplate(db.Model):
|
||||
__tablename__ = 'inspection_templates'
|
||||
@@ -11,13 +13,28 @@ class InspectionTemplate(db.Model):
|
||||
created_by = db.Column(db.Integer, db.ForeignKey('users.id'))
|
||||
created_at = db.Column(db.DateTime, default=datetime.utcnow)
|
||||
|
||||
# Form builder schema — stores the full dynamic field layout as JSON
|
||||
form_schema = db.Column(db.JSON, nullable=True)
|
||||
|
||||
# Relationships
|
||||
checklist_items = db.relationship('ChecklistItem', backref='template', lazy='dynamic', cascade='all, delete-orphan')
|
||||
inspections = db.relationship('Inspection', backref='template', lazy='dynamic')
|
||||
|
||||
def get_form_schema(self):
|
||||
"""Return the form schema as a Python list, or empty list if not set."""
|
||||
if self.form_schema is None:
|
||||
return []
|
||||
if isinstance(self.form_schema, str):
|
||||
try:
|
||||
return json.loads(self.form_schema)
|
||||
except (json.JSONDecodeError, TypeError):
|
||||
return []
|
||||
return self.form_schema
|
||||
|
||||
def __repr__(self):
|
||||
return f'<InspectionTemplate {self.name}>'
|
||||
|
||||
|
||||
class ChecklistItem(db.Model):
|
||||
__tablename__ = 'checklist_items'
|
||||
|
||||
@@ -36,6 +53,7 @@ class ChecklistItem(db.Model):
|
||||
def __repr__(self):
|
||||
return f'<ChecklistItem {self.item_description[:30]}>'
|
||||
|
||||
|
||||
class Inspection(db.Model):
|
||||
__tablename__ = 'inspections'
|
||||
|
||||
@@ -57,6 +75,7 @@ class Inspection(db.Model):
|
||||
def __repr__(self):
|
||||
return f'<Inspection {self.id} - {self.inspection_date}>'
|
||||
|
||||
|
||||
class InspectionResult(db.Model):
|
||||
__tablename__ = 'inspection_results'
|
||||
|
||||
@@ -69,4 +88,4 @@ class InspectionResult(db.Model):
|
||||
photo_path = db.Column(db.String(255))
|
||||
|
||||
def __repr__(self):
|
||||
return f'<InspectionResult {self.id}>'
|
||||
return f'<InspectionResult {self.id}>'
|
||||
Reference in New Issue
Block a user