File size: 6,003 Bytes
b4f2238 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | """
初始化文书生成系统配置
创建示例替换词数据.
Usage:
python manage.py init_document_system
python manage.py init_document_system --with-samples
"""
from typing import Any
from django.conf import settings
from django.core.management.base import BaseCommand
from apps.core.utils.path import Path
from apps.documents.models import Placeholder
class Command(BaseCommand):
help: str = "初始化文书生成系统配置"
def add_arguments(self, parser: Any) -> None:
parser.add_argument("--with-samples", action="store_true", help="创建示例替换词")
parser.add_argument("--force", action="store_true", help="强制覆盖已存在的配置")
def handle(self, *args: Any, **options: Any) -> None:
with_samples = options["with_samples"]
force = options["force"]
self.stdout.write("初始化文书生成系统...")
self._create_template_directories()
if with_samples:
self._create_sample_placeholders(force)
self.stdout.write(self.style.SUCCESS("文书生成系统初始化完成!"))
def _create_template_directories(self) -> None:
"""创建模板存储目录"""
directories = [
Path(settings.MEDIA_ROOT) / "document_templates",
Path(settings.MEDIA_ROOT) / "document_templates" / "versions",
Path(settings.MEDIA_ROOT) / "generated_documents",
]
for directory in directories:
if not directory.exists():
directory.mkdir(parents=True, exist_ok=True)
self.stdout.write(f" 创建目录: {directory}")
else:
self.stdout.write(f" 目录已存在: {directory}")
def _create_sample_placeholders(self, force: bool) -> None:
"""创建示例替换词"""
sample_placeholders = [
{
"key": "case_name",
"display_name": "案件名称",
"example_value": "张三诉李四合同纠纷案",
"description": "案件的完整名称",
},
{
"key": "case_number",
"display_name": "案号",
"example_value": "(2024)京0101民初12345号",
"description": "法院分配的案件编号",
},
{
"key": "case_type",
"display_name": "案件类型",
"example_value": "民事纠纷",
"description": "案件类型(民事、刑事等)",
},
{
"key": "filing_date",
"display_name": "立案日期",
"example_value": "2024年1月15日",
"description": "案件立案日期",
},
{
"key": "plaintiff_name",
"display_name": "原告姓名",
"example_value": "张三",
"description": "原告的姓名或名称",
},
{
"key": "plaintiff_id_number",
"display_name": "原告身份证号",
"example_value": "110101199001011234",
"description": "原告的身份证号码",
},
{
"key": "defendant_name",
"display_name": "被告姓名",
"example_value": "李四",
"description": "被告的姓名或名称",
},
{
"key": "contract_amount",
"display_name": "合同金额",
"example_value": "¥100,000.00",
"description": "合同总金额",
},
{
"key": "contract_date",
"display_name": "合同签订日期",
"example_value": "2023年12月1日",
"description": "合同签订日期",
},
{
"key": "lawyer_name",
"display_name": "代理律师",
"example_value": "王律师",
"description": "代理律师姓名",
},
{
"key": "law_firm_name",
"display_name": "律师事务所",
"example_value": "北京某某律师事务所",
"description": "律师事务所名称",
},
{
"key": "court_name",
"display_name": "法院名称",
"example_value": "北京市东城区人民法院",
"description": "受理法院名称",
},
{
"key": "current_date",
"display_name": "当前日期",
"example_value": "2024年1月1日",
"description": "文书生成时的当前日期",
},
]
created_count = 0
updated_count = 0
for placeholder_data in sample_placeholders:
placeholder, created = Placeholder.objects.get_or_create(
key=placeholder_data["key"],
defaults={
"display_name": placeholder_data["display_name"],
"example_value": placeholder_data.get("example_value", ""),
"description": placeholder_data.get("description", ""),
},
)
if created:
created_count += 1
self.stdout.write(f" 创建替换词: {placeholder.key}")
elif force:
for field, value in placeholder_data.items():
if field != "key":
setattr(placeholder, field, value)
placeholder.save()
updated_count += 1
self.stdout.write(f" 更新替换词: {placeholder.key}")
else:
self.stdout.write(f" 跳过已存在: {placeholder.key}")
self.stdout.write(f"示例替换词: 创建 {created_count} 个, 更新 {updated_count} 个")
|