From b1754365d51e13dbf63e5a40a8618e64e978735c Mon Sep 17 00:00:00 2001 From: root Date: Sat, 12 Jul 2025 22:12:13 +0800 Subject: [PATCH] fix: template --- python_core/database/template_postgres.py | 92 +++++++++++++++++++++++ python_core/database/types.py | 4 +- 2 files changed, 95 insertions(+), 1 deletion(-) diff --git a/python_core/database/template_postgres.py b/python_core/database/template_postgres.py index c5b52c5..9ece907 100644 --- a/python_core/database/template_postgres.py +++ b/python_core/database/template_postgres.py @@ -700,6 +700,98 @@ PostgreSQL 驱动 psycopg2 未安装。请安装: return template_info + def update_draft_content(self, template_id: str, draft_content: Dict[str, Any]) -> bool: + """ + 更新模板的 draft content + + Args: + template_id: 模板ID + draft_content: draft content 数据 + + Returns: + 更新成功返回True + """ + try: + return self.update_template(template_id, {"draft_content": draft_content}) + except Exception as e: + logger.error(f"Failed to update draft content for template '{template_id}': {e}") + return False + + def get_draft_content(self, template_id: str) -> Optional[Dict[str, Any]]: + """ + 获取模板的 draft content + + Args: + template_id: 模板ID + + Returns: + draft content 数据,如果不存在返回None + """ + try: + template = self.get_template_by_id(template_id) + if template and hasattr(template, 'draft_content'): + return template.draft_content + return None + except Exception as e: + logger.error(f"Failed to get draft content for template '{template_id}': {e}") + return None + + def save_draft_content_from_file(self, template_id: str, draft_file_path: str) -> bool: + """ + 从文件读取 draft content 并保存到数据库 + + Args: + template_id: 模板ID + draft_file_path: draft content 文件路径 + + Returns: + 保存成功返回True + """ + try: + import os + if not os.path.exists(draft_file_path): + logger.error(f"Draft content file not found: {draft_file_path}") + return False + + with open(draft_file_path, 'r', encoding='utf-8') as f: + draft_content = json.load(f) + + return self.update_draft_content(template_id, draft_content) + + except Exception as e: + logger.error(f"Failed to save draft content from file '{draft_file_path}': {e}") + return False + + def export_draft_content_to_file(self, template_id: str, output_path: str) -> bool: + """ + 将数据库中的 draft content 导出到文件 + + Args: + template_id: 模板ID + output_path: 输出文件路径 + + Returns: + 导出成功返回True + """ + try: + draft_content = self.get_draft_content(template_id) + if draft_content is None: + logger.error(f"No draft content found for template: {template_id}") + return False + + import os + os.makedirs(os.path.dirname(output_path), exist_ok=True) + + with open(output_path, 'w', encoding='utf-8') as f: + json.dump(draft_content, f, ensure_ascii=False, indent=2) + + logger.info(f"Draft content exported to: {output_path}") + return True + + except Exception as e: + logger.error(f"Failed to export draft content to file '{output_path}': {e}") + return False + # 创建全局模板表实例 template_table = TemplateTablePostgres() diff --git a/python_core/database/types.py b/python_core/database/types.py index d27d789..5663eda 100644 --- a/python_core/database/types.py +++ b/python_core/database/types.py @@ -53,4 +53,6 @@ class TemplateInfo: # is_cloud false 代表本地资源 那么只加载自己的 如果是 true 就是公用的 is_cloud: bool = False # 创建用户 - user_id: str = "" \ No newline at end of file + user_id: str = "" + # draft content 存储在数据库中的内容 + draft_content: Dict[str, Any] = None \ No newline at end of file