""" 测试文档生成功能 - 使用虚拟数据生成初步核实审批表 """ from services.document_service import DocumentService from datetime import datetime, timedelta from minio import Minio import os # 虚拟数据 - 初步核实审批表字段 VIRTUAL_DATA = [ {"fieldCode": "target_name", "fieldValue": "张三"}, {"fieldCode": "target_gender", "fieldValue": "男"}, {"fieldCode": "target_nationality", "fieldValue": "汉族"}, {"fieldCode": "target_date_of_birth", "fieldValue": "198005"}, {"fieldCode": "target_place_of_birth", "fieldValue": "山西太原"}, {"fieldCode": "target_political_status", "fieldValue": "中共党员"}, {"fieldCode": "target_education", "fieldValue": "本科学历"}, {"fieldCode": "target_organization_and_position", "fieldValue": "某公司总经理"}, {"fieldCode": "target_id_number", "fieldValue": "140101198005150123"}, {"fieldCode": "target_contact", "fieldValue": "13800138000"}, {"fieldCode": "target_address", "fieldValue": "山西省太原市小店区某某街道123号"}, {"fieldCode": "clue_source", "fieldValue": "群众举报"}, {"fieldCode": "clue_info", "fieldValue": "被举报人利用职务便利,违规接受他人请托,收受他人财物"}, {"fieldCode": "investigation_reason", "fieldValue": "根据群众举报,反映被核查人存在违规违纪问题"}, {"fieldCode": "investigation_basis", "fieldValue": "《中国共产党纪律检查机关监督执纪工作规则》相关规定"}, ] TEMPLATE_CODE = "PRELIMINARY_VERIFICATION_APPROVAL" FILE_NAME = "初步核实审批表_张三.docx" def print_section(title): """打印章节标题""" print("\n" + "="*60) print(f" {title}") print("="*60) def print_result(success, message): """打印测试结果""" status = "✓" if success else "✗" print(f"{status} {message}") def test_document_generation(): """测试文档生成""" print_section("文档生成测试 - 初步核实审批表") print("\n使用虚拟数据生成文档...") print(f"模板编码: {TEMPLATE_CODE}") print(f"文件名称: {FILE_NAME}") print(f"\n虚拟数据字段数量: {len(VIRTUAL_DATA)}") print("\n字段列表:") for item in VIRTUAL_DATA[:5]: # 只显示前5个 print(f" - {item['fieldCode']}: {item['fieldValue']}") print(f" ... 还有 {len(VIRTUAL_DATA) - 5} 个字段") try: # 创建文档服务实例 doc_service = DocumentService() # 准备文件信息 file_info = { 'fileId': 1, 'fileName': FILE_NAME, 'templateCode': TEMPLATE_CODE } print("\n开始生成文档...") # 生成文档 result = doc_service.generate_document( template_code=TEMPLATE_CODE, input_data=VIRTUAL_DATA, file_info=file_info ) print_result(True, "文档生成成功!") file_path = result.get('filePath', '') print(f"\n生成的文件路径: {file_path}") return file_path except Exception as e: print_result(False, f"文档生成失败: {str(e)}") import traceback traceback.print_exc() return None def generate_download_url(file_path): """生成下载URL""" print_section("生成下载URL") if not file_path: print("⚠ 没有文件路径,无法生成URL") return None try: # 去掉开头的斜杠 object_name = file_path.lstrip('/') # 创建MinIO客户端 minio_config = { 'endpoint': os.getenv('MINIO_ENDPOINT', 'minio.datacubeworld.com:9000'), 'access_key': os.getenv('MINIO_ACCESS_KEY', 'JOLXFXny3avFSzB0uRA5'), 'secret_key': os.getenv('MINIO_SECRET_KEY', 'G1BR8jStNfovkfH5ou39EmPl34E4l7dGrnd3Cz0I'), 'secure': os.getenv('MINIO_SECURE', 'true').lower() == 'true' } bucket_name = os.getenv('MINIO_BUCKET', 'finyx') client = Minio( minio_config['endpoint'], access_key=minio_config['access_key'], secret_key=minio_config['secret_key'], secure=minio_config['secure'] ) print(f"生成预签名URL(7天有效)...") print(f"对象名称: {object_name}") # 生成预签名URL(7天有效期) url = client.presigned_get_object( bucket_name, object_name, expires=timedelta(days=7) ) print_result(True, "URL生成成功!") print(f"\n下载链接(7天有效):") print(f"{url}") return url except Exception as e: print_result(False, f"URL生成失败: {str(e)}") import traceback traceback.print_exc() return None def verify_file_exists(file_path): """验证文件是否存在""" print_section("验证文件是否存在") if not file_path: print("⚠ 没有文件路径") return False try: # 去掉开头的斜杠 object_name = file_path.lstrip('/') # 创建MinIO客户端 minio_config = { 'endpoint': os.getenv('MINIO_ENDPOINT', 'minio.datacubeworld.com:9000'), 'access_key': os.getenv('MINIO_ACCESS_KEY', 'JOLXFXny3avFSzB0uRA5'), 'secret_key': os.getenv('MINIO_SECRET_KEY', 'G1BR8jStNfovkfH5ou39EmPl34E4l7dGrnd3Cz0I'), 'secure': os.getenv('MINIO_SECURE', 'true').lower() == 'true' } bucket_name = os.getenv('MINIO_BUCKET', 'finyx') client = Minio( minio_config['endpoint'], access_key=minio_config['access_key'], secret_key=minio_config['secret_key'], secure=minio_config['secure'] ) # 检查文件是否存在 stat = client.stat_object(bucket_name, object_name) print_result(True, "文件存在") print(f"\n文件信息:") print(f" 文件大小: {stat.size} 字节") print(f" 最后修改: {stat.last_modified}") print(f" 内容类型: {stat.content_type}") return True except Exception as e: print_result(False, f"文件验证失败: {str(e)}") return False def main(): """主函数""" print("\n" + "="*60) print(" 文档生成测试 - 初步核实审批表") print("="*60) # 1. 生成文档 file_path = test_document_generation() if not file_path: print("\n✗ 文档生成失败,无法继续") return # 2. 验证文件存在 verify_file_exists(file_path) # 3. 生成下载URL download_url = generate_download_url(file_path) # 总结 print_section("测试总结") results = { '文档生成': file_path is not None, '文件验证': verify_file_exists(file_path) if file_path else False, 'URL生成': download_url is not None } print("\n测试结果:") for test_name, success in results.items(): status = "✓ 通过" if success else "✗ 失败" print(f" {test_name}: {status}") if download_url: print("\n" + "="*60) print(" 下载链接") print("="*60) print(f"\n{download_url}\n") print("="*60) print("提示:") print(" - 此链接7天内有效") print(" - 可以直接在浏览器中打开下载") print(" - 文件已成功上传到MinIO") print("="*60) if __name__ == '__main__': main()