fix: template
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -54,3 +54,5 @@ class TemplateInfo:
|
||||
is_cloud: bool = False
|
||||
# 创建用户
|
||||
user_id: str = ""
|
||||
# draft content 存储在数据库中的内容
|
||||
draft_content: Dict[str, Any] = None
|
||||
Reference in New Issue
Block a user