from enum import Enum from sqlalchemy import Column, String, Boolean, Enum as SQLEnum, Text, UniqueConstraint, ForeignKey from sqlalchemy.dialects.postgresql import UUID, JSONB from sqlalchemy.orm import relationship from .base import Base, TimestampMixin, UUIDMixin class TemplateType(str, Enum): INVITATION = "invitation" REMINDER = "reminder" CONFIRMATION = "confirmation" UPDATE = "update" THANK_YOU = "thank_you" CUSTOM = "custom" class EmailTemplate(Base, UUIDMixin, TimestampMixin): __tablename__ = 'email_templates' # Template Identification name = Column(String, nullable=False) description = Column(String) template_type = Column(SQLEnum(TemplateType), nullable=False) # Template Content subject = Column(String, nullable=False) html_content = Column(Text, nullable=False) text_content = Column(Text, nullable=False) # Template Variables variables = Column(JSONB, default=dict) # Organization event_id = Column(UUID(as_uuid=True), ForeignKey('events.id')) # Optional, for event-specific templates created_by = Column(UUID(as_uuid=True), ForeignKey('users.id'), nullable=False) # Status is_active = Column(Boolean, default=True, nullable=False) is_system = Column(Boolean, default=False, nullable=False) # For system-provided templates # Relationships event = relationship("Event", back_populates="email_templates", foreign_keys=[event_id]) creator = relationship("User", foreign_keys=[created_by]) # Ensure unique template names within an event or globally for system templates __table_args__ = ( UniqueConstraint('event_id', 'name', name='uq_event_template_name'), ) def __repr__(self): return f"" def render(self, context: dict) -> tuple[str, str, str]: """ Render the template with given context Returns tuple of (subject, html_content, text_content) """ # This would normally use a template engine like Jinja2 # For now, we'll use a simple placeholder implementation subject = self.subject html = self.html_content text = self.text_content # Replace placeholders in the form {{variable}} for key, value in context.items(): placeholder = f"{{{{{key}}}}}" subject = subject.replace(placeholder, str(value)) html = html.replace(placeholder, str(value)) text = text.replace(placeholder, str(value)) return subject, html, text