:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
chore(workflows): route email design and template reads through the facade (#109407)
Harley Alexanderddf29832992e
diff --git a/products/workflows/backend/facade/contracts.py b/products/workflows/backend/facade/contracts.pyindex bfa407b02509..431d3b6e0859 100644--- a/products/workflows/backend/facade/contracts.py+++ b/products/workflows/backend/facade/contracts.py@@ -362,3 +362,17 @@ class ProposalChanges:changes: dictconflicts: list[str]+++@frozen+class EditedEmailDesign:+ design: dict[str, Any]+ warnings: tuple[str, ...]+++class EmailDesignRenderingNotConfigured(Exception):+ pass+++class EmailDesignRenderFailed(Exception):+ passdiff --git a/products/workflows/backend/facade/email_design.py b/products/workflows/backend/facade/email_design.pynew file mode 100644index 000000000000..51cf90f48b87--- /dev/null+++ b/products/workflows/backend/facade/email_design.py@@ -0,0 +1,13 @@+"""Email step content: saved email templates, and design edits rendered to HTML."""++from products.workflows.backend.services.hog_flow_email_design import (+ apply_email_design_operations,+ get_email_template_content,+ render_email_design_html,+)++__all__ = [+ "apply_email_design_operations",+ "get_email_template_content",+ "render_email_design_html",+]diff --git a/products/workflows/backend/presentation/views/hog_flow.py b/products/workflows/backend/presentation/views/hog_flow.pyindex 3114a9599da5..cf69ab77c08f 100644--- a/products/workflows/backend/presentation/views/hog_flow.py+++ b/products/workflows/backend/presentation/views/hog_flow.py@@ -104,11 +104,7 @@ from products.access_control.backend.presentation.access_control import (AccessControlViewSetMixin,UserAccessControlSerializerMixin,)-from products.messaging.backend.api.design_operations import apply_design_operations-from products.messaging.backend.api.design_validation import validate_designfrom products.messaging.backend.api.message_templates import DesignOperationSerializer-from products.messaging.backend.models import MessageTemplate-from products.messaging.backend.unlayer import UnlayerNotConfiguredError, UnlayerRenderError, render_design_htmlfrom products.notifications.backend.facade.api import publish_resource_editedfrom products.tasks.backend.facade.api import list_workflow_last_runsfrom products.tasks.backend.facade.contracts import WorkflowLastRunDTO@@ -138,6 +134,8 @@ from products.workflows.backend.facade.blast_radius import ()from products.workflows.backend.facade.content import DRAFT_CONTENT_FIELDS, deep_merge, snapshot_contentfrom products.workflows.backend.facade.contracts import (+ EmailDesignRenderFailed,+ EmailDesignRenderingNotConfigured,StaffPausedError,WorkflowBatchJobNotFound,WorkflowDraftChanged,@@ -146,6 +144,11 @@ from products.workflows.backend.facade.contracts import (WorkflowRevisionSummary,WorkflowScheduleNotFound,)+from products.workflows.backend.facade.email_design import (+ apply_email_design_operations,+ get_email_template_content,+ render_email_design_html,+)from products.workflows.backend.facade.email_health import (fetch_aws_tenant_reputation,fetch_email_totals_by_source,@@ -604,16 +607,15 @@ def _apply_email_template_content(config: dict, team: Team, strict: bool, contex# list validates one action at a time, so without this each step re-queries the same row.# The context dict is shared across the many=True action list, so the memo (and the# materialized-bytes counter below) span all steps in one request.- template_cache: dict[str, Optional[MessageTemplate]] = context.setdefault("_message_template_cache", {})+ template_cache: dict[str, Optional[dict]] = context.setdefault("_message_template_cache", {})cache_key = str(parsed_uuid)if parsed_uuid is None:- template = None+ email_content = Noneelif cache_key in template_cache:- template = template_cache[cache_key]+ email_content = template_cache[cache_key]else:- template = MessageTemplate.objects.filter(team_id=team.id, id=parsed_uuid, deleted=False).first()- template_cache[cache_key] = template- email_content = (template.content or {}).get("email") if template else None+ email_content = get_email_template_content(team.id, parsed_uuid)+ template_cache[cache_key] = email_contentif not isinstance(email_content, dict) or not any(email_content.get(key) for key in _TEMPLATE_EMAIL_BODY_KEYS):if strict:raise serializers.ValidationError(@@ -3319,19 +3321,20 @@ def _render_action_email_operations("operations."})- new_design = apply_design_operations(design, operations)- for warning in validate_design(new_design):+ edited = apply_email_design_operations(design, operations)+ new_design = edited.design+ for warning in edited.warnings:logger.info("hog_flow_action_email_design_warning", warning=warning, action_id=action_id)try:- html = render_design_html(new_design)- except UnlayerNotConfiguredError:+ html = render_email_design_html(new_design)+ except EmailDesignRenderingNotConfigured:raise exceptions.ValidationError({"operations": "Design rendering is not configured on this instance - an administrator ""must set UNLAYER_API_KEY to enable design editing."})- except UnlayerRenderError as e:+ except EmailDesignRenderFailed as e:raise exceptions.ValidationError({"operations": f"Rendering the design to HTML failed: {e}"})return _RenderedActionEmailDesign(base_design=design, design=new_design, html=html)diff --git a/products/workflows/backend/services/hog_flow_email_design.py b/products/workflows/backend/services/hog_flow_email_design.pynew file mode 100644index 000000000000..cd2f2ed6b59b--- /dev/null+++ b/products/workflows/backend/services/hog_flow_email_design.py@@ -0,0 +1,31 @@+from uuid import UUID++from products.messaging.backend.api.design_operations import apply_design_operations+from products.messaging.backend.api.design_validation import validate_design+from products.messaging.backend.models import MessageTemplate+from products.messaging.backend.unlayer import UnlayerNotConfiguredError, UnlayerRenderError, render_design_html+from products.workflows.backend.facade.contracts import (+ EditedEmailDesign,+ EmailDesignRenderFailed,+ EmailDesignRenderingNotConfigured,+)+++def get_email_template_content(team_id: int, template_id: UUID) -> dict | None:+ template = MessageTemplate.objects.filter(team_id=team_id, id=template_id, deleted=False).first()+ email_content = (template.content or {}).get("email") if template else None+ return email_content if isinstance(email_content, dict) else None+++def apply_email_design_operations(design: dict, operations: list[dict]) -> EditedEmailDesign:+ new_design = apply_design_operations(design, operations)+ return EditedEmailDesign(design=new_design, warnings=tuple(validate_design(new_design)))+++def render_email_design_html(design: dict) -> str:+ try:+ return render_design_html(design)+ except UnlayerNotConfiguredError as e:+ raise EmailDesignRenderingNotConfigured() from e+ except UnlayerRenderError as e:+ raise EmailDesignRenderFailed(str(e)) from ediff --git a/products/workflows/backend/tests/api/test_hog_flow_action_email.py b/products/workflows/backend/tests/api/test_hog_flow_action_email.pyindex 249ab9644d2a..89d9a0d695b8 100644--- a/products/workflows/backend/tests/api/test_hog_flow_action_email.py+++ b/products/workflows/backend/tests/api/test_hog_flow_action_email.py@@ -15,7 +15,7 @@ from products.workflows.backend.models.hog_flow_revision import HogFlowRevisionwebhook_template = MOCK_NODE_TEMPLATES[0]-RENDER_PATH = "products.workflows.backend.presentation.views.hog_flow.render_design_html"+RENDER_PATH = "products.workflows.backend.services.hog_flow_email_design.render_design_html"RENDERED_HTML = "<html>rendered</html>"diff --git a/pyproject.toml b/pyproject.tomlindex 0dffb1dc3874..5852e3dbfb74 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -717,11 +717,7 @@ ignore_imports = [# parse -> facade -> serialize and delete each entry. backend:contract-check stays off until# this list is empty. Only delete entries here: a new import from these views goes through# the facade.- "products.workflows.backend.presentation.views.hog_flow -> products.messaging.backend.api.design_operations",- "products.workflows.backend.presentation.views.hog_flow -> products.messaging.backend.api.design_validation","products.workflows.backend.presentation.views.hog_flow -> products.messaging.backend.api.message_templates",- "products.workflows.backend.presentation.views.hog_flow -> products.messaging.backend.models",- "products.workflows.backend.presentation.views.hog_flow -> products.messaging.backend.unlayer","products.workflows.backend.presentation.views.hog_flow -> products.workflows.backend.models.hog_flow.hog_flow","products.workflows.backend.presentation.views.hog_flow -> products.workflows.backend.models.hog_flow_revision","products.workflows.backend.presentation.views.hog_flow -> products.workflows.backend.models.workflow_proposal",