feat: implement email template system with vendor overrides
Add comprehensive email template management for both admin and vendors:
Admin Features:
- Email templates management page at /admin/email-templates
- Edit platform templates with language support (en, fr, de, lb)
- Preview templates with sample variables
- Send test emails
- View email logs per template
Vendor Features:
- Email templates customization page at /vendor/{code}/email-templates
- Override platform templates with vendor-specific versions
- Preview and test customized templates
- Revert to platform defaults
Technical Changes:
- Migration for vendor_email_templates table
- VendorEmailTemplate model with override management
- Enhanced EmailService with language resolution chain
(customer preferred -> vendor preferred -> platform default)
- Branding resolution (Wizamart default, removed for whitelabel)
- Platform-only template protection (billing templates)
- Admin and vendor API endpoints with full CRUD
- Updated seed script with billing and team templates
Files: 22 changed, ~3,900 lines added
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -20,6 +20,7 @@ from .content_page import ContentPage
|
||||
from .customer import Customer, CustomerAddress
|
||||
from .password_reset_token import PasswordResetToken
|
||||
from .email import EmailCategory, EmailLog, EmailStatus, EmailTemplate
|
||||
from .vendor_email_template import VendorEmailTemplate
|
||||
from .feature import Feature, FeatureCategory, FeatureCode, FeatureUILocation
|
||||
from .inventory import Inventory
|
||||
from .inventory_transaction import InventoryTransaction, TransactionType
|
||||
@@ -112,6 +113,7 @@ __all__ = [
|
||||
"EmailLog",
|
||||
"EmailStatus",
|
||||
"EmailTemplate",
|
||||
"VendorEmailTemplate",
|
||||
# Features
|
||||
"Feature",
|
||||
"FeatureCategory",
|
||||
|
||||
@@ -5,9 +5,15 @@ Email system database models.
|
||||
Provides:
|
||||
- EmailTemplate: Multi-language email templates stored in database
|
||||
- EmailLog: Email sending history and tracking
|
||||
|
||||
Platform vs Vendor Templates:
|
||||
- Platform templates (EmailTemplate) are the defaults
|
||||
- Vendors can override templates via VendorEmailTemplate
|
||||
- Platform-only templates (is_platform_only=True) cannot be overridden
|
||||
"""
|
||||
|
||||
import enum
|
||||
import json
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy import (
|
||||
@@ -16,11 +22,12 @@ from sqlalchemy import (
|
||||
DateTime,
|
||||
Enum,
|
||||
ForeignKey,
|
||||
Index,
|
||||
Integer,
|
||||
String,
|
||||
Text,
|
||||
)
|
||||
from sqlalchemy.orm import relationship
|
||||
from sqlalchemy.orm import Session, relationship
|
||||
|
||||
from app.core.database import Base
|
||||
|
||||
@@ -81,11 +88,20 @@ class EmailTemplate(Base, TimestampMixin):
|
||||
# e.g., ["first_name", "company_name", "login_url"]
|
||||
variables = Column(Text, nullable=True)
|
||||
|
||||
# Required variables (JSON list of variables that MUST be provided)
|
||||
# Subset of variables that are mandatory for the template to render
|
||||
required_variables = Column(Text, nullable=True)
|
||||
|
||||
# Status
|
||||
is_active = Column(Boolean, default=True, nullable=False)
|
||||
|
||||
# Platform-only flag: if True, vendors cannot override this template
|
||||
# Used for billing, subscription, and other platform-level emails
|
||||
is_platform_only = Column(Boolean, default=False, nullable=False)
|
||||
|
||||
# Unique constraint: one template per code+language
|
||||
__table_args__ = (
|
||||
Index("ix_email_templates_code_language", "code", "language", unique=True),
|
||||
{"sqlite_autoincrement": True},
|
||||
)
|
||||
|
||||
@@ -95,8 +111,6 @@ class EmailTemplate(Base, TimestampMixin):
|
||||
@property
|
||||
def variables_list(self) -> list[str]:
|
||||
"""Parse variables JSON to list."""
|
||||
import json
|
||||
|
||||
if not self.variables:
|
||||
return []
|
||||
try:
|
||||
@@ -104,6 +118,106 @@ class EmailTemplate(Base, TimestampMixin):
|
||||
except (json.JSONDecodeError, TypeError):
|
||||
return []
|
||||
|
||||
@property
|
||||
def required_variables_list(self) -> list[str]:
|
||||
"""Parse required_variables JSON to list."""
|
||||
if not self.required_variables:
|
||||
return []
|
||||
try:
|
||||
return json.loads(self.required_variables)
|
||||
except (json.JSONDecodeError, TypeError):
|
||||
return []
|
||||
|
||||
@classmethod
|
||||
def get_by_code_and_language(
|
||||
cls,
|
||||
db: Session,
|
||||
code: str,
|
||||
language: str,
|
||||
fallback_to_english: bool = True,
|
||||
) -> "EmailTemplate | None":
|
||||
"""
|
||||
Get a platform template by code and language.
|
||||
|
||||
Args:
|
||||
db: Database session
|
||||
code: Template code (e.g., "password_reset")
|
||||
language: Language code (en, fr, de, lb)
|
||||
fallback_to_english: If True, fall back to English if language not found
|
||||
|
||||
Returns:
|
||||
EmailTemplate if found, None otherwise
|
||||
"""
|
||||
template = (
|
||||
db.query(cls)
|
||||
.filter(
|
||||
cls.code == code,
|
||||
cls.language == language,
|
||||
cls.is_active == True, # noqa: E712
|
||||
)
|
||||
.first()
|
||||
)
|
||||
|
||||
# Fallback to English if requested language not found
|
||||
if not template and fallback_to_english and language != "en":
|
||||
template = (
|
||||
db.query(cls)
|
||||
.filter(
|
||||
cls.code == code,
|
||||
cls.language == "en",
|
||||
cls.is_active == True, # noqa: E712
|
||||
)
|
||||
.first()
|
||||
)
|
||||
|
||||
return template
|
||||
|
||||
@classmethod
|
||||
def get_all_templates(
|
||||
cls,
|
||||
db: Session,
|
||||
category: str | None = None,
|
||||
include_inactive: bool = False,
|
||||
) -> list["EmailTemplate"]:
|
||||
"""
|
||||
Get all platform templates, optionally filtered by category.
|
||||
|
||||
Args:
|
||||
db: Database session
|
||||
category: Optional category filter
|
||||
include_inactive: Include inactive templates
|
||||
|
||||
Returns:
|
||||
List of EmailTemplate objects
|
||||
"""
|
||||
query = db.query(cls)
|
||||
|
||||
if category:
|
||||
query = query.filter(cls.category == category)
|
||||
|
||||
if not include_inactive:
|
||||
query = query.filter(cls.is_active == True) # noqa: E712
|
||||
|
||||
return query.order_by(cls.code, cls.language).all()
|
||||
|
||||
@classmethod
|
||||
def get_overridable_templates(cls, db: Session) -> list["EmailTemplate"]:
|
||||
"""
|
||||
Get all templates that vendors can override.
|
||||
|
||||
Returns:
|
||||
List of EmailTemplate objects where is_platform_only=False
|
||||
"""
|
||||
return (
|
||||
db.query(cls)
|
||||
.filter(
|
||||
cls.is_platform_only == False, # noqa: E712
|
||||
cls.is_active == True, # noqa: E712
|
||||
)
|
||||
.order_by(cls.code, cls.language)
|
||||
.all()
|
||||
)
|
||||
|
||||
|
||||
class EmailLog(Base, TimestampMixin):
|
||||
"""
|
||||
|
||||
@@ -170,6 +170,13 @@ class Vendor(Base, TimestampMixin):
|
||||
cascade="all, delete-orphan",
|
||||
)
|
||||
|
||||
# Email template overrides (one-to-many)
|
||||
email_templates = relationship(
|
||||
"VendorEmailTemplate",
|
||||
back_populates="vendor",
|
||||
cascade="all, delete-orphan",
|
||||
)
|
||||
|
||||
# Subscription (one-to-one)
|
||||
subscription = relationship(
|
||||
"VendorSubscription",
|
||||
|
||||
229
models/database/vendor_email_template.py
Normal file
229
models/database/vendor_email_template.py
Normal file
@@ -0,0 +1,229 @@
|
||||
# models/database/vendor_email_template.py
|
||||
"""
|
||||
Vendor email template override model.
|
||||
|
||||
Allows vendors to customize platform email templates with their own content.
|
||||
Platform-only templates cannot be overridden (e.g., billing, subscription emails).
|
||||
"""
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy import (
|
||||
Boolean,
|
||||
Column,
|
||||
DateTime,
|
||||
ForeignKey,
|
||||
Integer,
|
||||
String,
|
||||
Text,
|
||||
UniqueConstraint,
|
||||
)
|
||||
from sqlalchemy.orm import Session, relationship
|
||||
|
||||
from app.core.database import Base
|
||||
|
||||
from .base import TimestampMixin
|
||||
|
||||
|
||||
class VendorEmailTemplate(Base, TimestampMixin):
|
||||
"""
|
||||
Vendor-specific email template override.
|
||||
|
||||
Each vendor can customize email templates for their shop.
|
||||
Overrides are per-template-code and per-language.
|
||||
|
||||
When sending emails:
|
||||
1. Check if vendor has an override for the template+language
|
||||
2. If yes, use vendor's version
|
||||
3. If no, fall back to platform template
|
||||
|
||||
Platform-only templates (is_platform_only=True on EmailTemplate)
|
||||
cannot be overridden.
|
||||
"""
|
||||
|
||||
__tablename__ = "vendor_email_templates"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True, autoincrement=True)
|
||||
|
||||
# Vendor relationship
|
||||
vendor_id = Column(
|
||||
Integer,
|
||||
ForeignKey("vendors.id", ondelete="CASCADE"),
|
||||
nullable=False,
|
||||
index=True,
|
||||
)
|
||||
|
||||
# Template identification (references EmailTemplate.code, not FK)
|
||||
template_code = Column(String(100), nullable=False, index=True)
|
||||
language = Column(String(5), nullable=False, default="en")
|
||||
|
||||
# Optional custom name (if None, uses platform template name)
|
||||
name = Column(String(255), nullable=True)
|
||||
|
||||
# Email content
|
||||
subject = Column(String(500), nullable=False)
|
||||
body_html = Column(Text, nullable=False)
|
||||
body_text = Column(Text, nullable=True)
|
||||
|
||||
# Status
|
||||
is_active = Column(Boolean, default=True, nullable=False)
|
||||
|
||||
# Relationships
|
||||
vendor = relationship("Vendor", back_populates="email_templates")
|
||||
|
||||
# Unique constraint: one override per vendor+template+language
|
||||
__table_args__ = (
|
||||
UniqueConstraint(
|
||||
"vendor_id",
|
||||
"template_code",
|
||||
"language",
|
||||
name="uq_vendor_email_template_code_language",
|
||||
),
|
||||
{"sqlite_autoincrement": True},
|
||||
)
|
||||
|
||||
def __repr__(self):
|
||||
return (
|
||||
f"<VendorEmailTemplate("
|
||||
f"vendor_id={self.vendor_id}, "
|
||||
f"code='{self.template_code}', "
|
||||
f"language='{self.language}')>"
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def get_override(
|
||||
cls,
|
||||
db: Session,
|
||||
vendor_id: int,
|
||||
template_code: str,
|
||||
language: str,
|
||||
) -> "VendorEmailTemplate | None":
|
||||
"""
|
||||
Get vendor's template override if it exists.
|
||||
|
||||
Args:
|
||||
db: Database session
|
||||
vendor_id: Vendor ID
|
||||
template_code: Template code to look up
|
||||
language: Language code (en, fr, de, lb)
|
||||
|
||||
Returns:
|
||||
VendorEmailTemplate if override exists, None otherwise
|
||||
"""
|
||||
return (
|
||||
db.query(cls)
|
||||
.filter(
|
||||
cls.vendor_id == vendor_id,
|
||||
cls.template_code == template_code,
|
||||
cls.language == language,
|
||||
cls.is_active == True, # noqa: E712
|
||||
)
|
||||
.first()
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def get_all_overrides_for_vendor(
|
||||
cls,
|
||||
db: Session,
|
||||
vendor_id: int,
|
||||
) -> list["VendorEmailTemplate"]:
|
||||
"""
|
||||
Get all template overrides for a vendor.
|
||||
|
||||
Args:
|
||||
db: Database session
|
||||
vendor_id: Vendor ID
|
||||
|
||||
Returns:
|
||||
List of VendorEmailTemplate objects
|
||||
"""
|
||||
return (
|
||||
db.query(cls)
|
||||
.filter(
|
||||
cls.vendor_id == vendor_id,
|
||||
cls.is_active == True, # noqa: E712
|
||||
)
|
||||
.order_by(cls.template_code, cls.language)
|
||||
.all()
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def create_or_update(
|
||||
cls,
|
||||
db: Session,
|
||||
vendor_id: int,
|
||||
template_code: str,
|
||||
language: str,
|
||||
subject: str,
|
||||
body_html: str,
|
||||
body_text: str | None = None,
|
||||
name: str | None = None,
|
||||
) -> "VendorEmailTemplate":
|
||||
"""
|
||||
Create or update a vendor email template override.
|
||||
|
||||
Args:
|
||||
db: Database session
|
||||
vendor_id: Vendor ID
|
||||
template_code: Template code
|
||||
language: Language code
|
||||
subject: Email subject
|
||||
body_html: HTML body content
|
||||
body_text: Optional plain text body
|
||||
name: Optional custom name
|
||||
|
||||
Returns:
|
||||
Created or updated VendorEmailTemplate
|
||||
"""
|
||||
existing = cls.get_override(db, vendor_id, template_code, language)
|
||||
|
||||
if existing:
|
||||
existing.subject = subject
|
||||
existing.body_html = body_html
|
||||
existing.body_text = body_text
|
||||
existing.name = name
|
||||
existing.updated_at = datetime.utcnow()
|
||||
return existing
|
||||
|
||||
new_template = cls(
|
||||
vendor_id=vendor_id,
|
||||
template_code=template_code,
|
||||
language=language,
|
||||
subject=subject,
|
||||
body_html=body_html,
|
||||
body_text=body_text,
|
||||
name=name,
|
||||
)
|
||||
db.add(new_template)
|
||||
return new_template
|
||||
|
||||
@classmethod
|
||||
def delete_override(
|
||||
cls,
|
||||
db: Session,
|
||||
vendor_id: int,
|
||||
template_code: str,
|
||||
language: str,
|
||||
) -> bool:
|
||||
"""
|
||||
Delete a vendor's template override (revert to platform default).
|
||||
|
||||
Args:
|
||||
db: Database session
|
||||
vendor_id: Vendor ID
|
||||
template_code: Template code
|
||||
language: Language code
|
||||
|
||||
Returns:
|
||||
True if deleted, False if not found
|
||||
"""
|
||||
deleted = (
|
||||
db.query(cls)
|
||||
.filter(
|
||||
cls.vendor_id == vendor_id,
|
||||
cls.template_code == template_code,
|
||||
cls.language == language,
|
||||
)
|
||||
.delete()
|
||||
)
|
||||
return deleted > 0
|
||||
Reference in New Issue
Block a user