mirror of
https://github.com/langgenius/dify.git
synced 2026-01-14 06:07:33 +08:00
chore(api): fix tests
This commit is contained in:
parent
74b6b48f40
commit
01325c543f
@ -1,17 +1,46 @@
|
||||
import logging
|
||||
import time
|
||||
from collections.abc import Mapping
|
||||
from typing import Any
|
||||
|
||||
import click
|
||||
from celery import shared_task
|
||||
from flask import render_template_string
|
||||
from jinja2.runtime import Context
|
||||
from jinja2.sandbox import ImmutableSandboxedEnvironment
|
||||
|
||||
from configs import dify_config
|
||||
from configs.feature import TemplateMode
|
||||
from extensions.ext_mail import mail
|
||||
from libs.email_i18n import get_email_i18n_service
|
||||
from libs.email_template_renderer import render_email_template
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class SandboxedEnvironment(ImmutableSandboxedEnvironment):
|
||||
def __init__(self, timeout: int, *args: Any, **kwargs: Any):
|
||||
self._timeout_time = time.time() + timeout
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
def call(self, context: Context, obj: Any, *args: Any, **kwargs: Any) -> Any:
|
||||
if time.time() > self._timeout_time:
|
||||
raise TimeoutError("Template rendering timeout")
|
||||
return super().call(context, obj, *args, **kwargs)
|
||||
|
||||
|
||||
def _render_template_with_strategy(body: str, substitutions: Mapping[str, str]) -> str:
|
||||
mode = dify_config.MAIL_TEMPLATING_MODE
|
||||
timeout = dify_config.MAIL_TEMPLATING_TIMEOUT
|
||||
if mode == TemplateMode.UNSAFE:
|
||||
return render_template_string(body, **substitutions)
|
||||
if mode == TemplateMode.SANDBOX:
|
||||
tmpl = SandboxedEnvironment(timeout=timeout).from_string(body)
|
||||
return tmpl.render(substitutions)
|
||||
if mode == TemplateMode.DISABLED:
|
||||
return body
|
||||
raise ValueError(f"Unsupported mail templating mode: {mode}")
|
||||
|
||||
|
||||
@shared_task(queue="mail")
|
||||
def send_inner_email_task(to: list[str], subject: str, body: str, substitutions: Mapping[str, str]):
|
||||
if not mail.is_inited():
|
||||
@ -21,7 +50,7 @@ def send_inner_email_task(to: list[str], subject: str, body: str, substitutions:
|
||||
start_at = time.perf_counter()
|
||||
|
||||
try:
|
||||
html_content = render_email_template(body, substitutions)
|
||||
html_content = _render_template_with_strategy(body, substitutions)
|
||||
|
||||
email_service = get_email_i18n_service()
|
||||
email_service.send_raw_email(to=to, subject=subject, html_content=html_content)
|
||||
|
||||
@ -22,6 +22,7 @@ from core.workflow.nodes.human_input.entities import (
|
||||
TimeoutUnit,
|
||||
UserAction,
|
||||
)
|
||||
from core.workflow.nodes.human_input.enums import HumanInputFormStatus
|
||||
from libs.datetime_utils import naive_utc_now
|
||||
from models.human_input import (
|
||||
EmailExternalRecipientPayload,
|
||||
@ -181,6 +182,7 @@ class _DummyForm:
|
||||
submission_user_id: str | None = None
|
||||
submission_end_user_id: str | None = None
|
||||
completed_by_recipient_id: str | None = None
|
||||
status: HumanInputFormStatus = HumanInputFormStatus.WAITING
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
|
||||
@ -12,7 +12,7 @@ from core.workflow.graph_events import (
|
||||
GraphRunSucceededEvent,
|
||||
NodeRunSucceededEvent,
|
||||
)
|
||||
from core.workflow.nodes.base.entities import VariableSelector
|
||||
from core.workflow.nodes.base.entities import OutputVariableEntity
|
||||
from core.workflow.nodes.end.end_node import EndNode
|
||||
from core.workflow.nodes.end.entities import EndNodeData
|
||||
from core.workflow.nodes.human_input.entities import HumanInputNodeData, UserAction
|
||||
@ -91,7 +91,6 @@ def _build_human_input_graph(
|
||||
graph_init_params=params,
|
||||
graph_runtime_state=runtime_state,
|
||||
)
|
||||
start_node.init_node_data(start_data.model_dump())
|
||||
|
||||
human_data = HumanInputNodeData(
|
||||
title="human",
|
||||
@ -108,12 +107,11 @@ def _build_human_input_graph(
|
||||
graph_runtime_state=runtime_state,
|
||||
form_repository=form_repository,
|
||||
)
|
||||
human_node.init_node_data(human_data.model_dump())
|
||||
|
||||
end_data = EndNodeData(
|
||||
title="end",
|
||||
outputs=[
|
||||
VariableSelector(variable="result", value_selector=["human", "action_id"]),
|
||||
OutputVariableEntity(variable="result", value_selector=["human", "action_id"]),
|
||||
],
|
||||
desc=None,
|
||||
)
|
||||
@ -123,7 +121,6 @@ def _build_human_input_graph(
|
||||
graph_init_params=params,
|
||||
graph_runtime_state=runtime_state,
|
||||
)
|
||||
end_node.init_node_data(end_data.model_dump())
|
||||
|
||||
return (
|
||||
Graph.new()
|
||||
|
||||
@ -15,6 +15,7 @@ from core.workflow.nodes.human_input.entities import (
|
||||
)
|
||||
from core.workflow.nodes.human_input.enums import (
|
||||
FormInputType,
|
||||
HumanInputFormStatus,
|
||||
TimeoutUnit,
|
||||
)
|
||||
from models.account import Account
|
||||
|
||||
Loading…
Reference in New Issue
Block a user