import json from datetime import datetime from app.extensions import db class DocumentTemplate(db.Model): """ Uploaded Word (.docx) templates with Jinja2-style placeholders. Template authors write {{ variable_name }} in their .docx file. When a document is generated the placeholders are replaced with actual context values (user, asset, assignment data). PII variables (those prefixed user_name, user_email, user_phone) are re-rendered with masked values when a user's record is masked. """ __tablename__ = 'document_templates' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(200), nullable=False) description = db.Column(db.Text, nullable=True) # Category maps to Paperwork.document_type so the UI can pre-filter category = db.Column(db.String(30), nullable=True) # handover|assignment|return|offboarding|custom # Stored filename relative to TEMPLATE_FOLDER filename = db.Column(db.String(300), nullable=False) # JSON list of placeholder names detected at upload time, e.g. ["user_name","asset_serial"] variables_json = db.Column(db.Text, nullable=True) created_by_id = db.Column(db.Integer, db.ForeignKey('admin_users.id'), nullable=True) created_at = db.Column(db.DateTime, default=datetime.utcnow) updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow) created_by = db.relationship('AdminUser', foreign_keys=[created_by_id]) paperwork_docs = db.relationship('Paperwork', back_populates='template', lazy='dynamic') @property def variables(self): if self.variables_json: try: return json.loads(self.variables_json) except (json.JSONDecodeError, TypeError): return [] return [] @variables.setter def variables(self, val): self.variables_json = json.dumps(val) def __repr__(self): return f''