Introduced new database models to handle email templates, activity logs, and notification logs, including relevant enums and utilities. Updated ER diagram to reflect new relationships and attributes, enhancing event tracking and notification management capabilities.
71 lines
2.5 KiB
Python
71 lines
2.5 KiB
Python
from enum import Enum
|
|
from sqlalchemy import Column, String, Boolean, Enum as SQLEnum, Text, UniqueConstraint
|
|
from sqlalchemy.dialects.postgresql import UUID, JSONB
|
|
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"<EmailTemplate {self.name} ({self.template_type.value})>"
|
|
|
|
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
|