File size: 3,817 Bytes
f8b03c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Models for document recognition."""

from __future__ import annotations

from typing import ClassVar

from django.db import models
from django.utils.translation import gettext_lazy as _


class DocumentRecognitionStatus(models.TextChoices):
    """文书识别任务状态。"""

    PENDING = "pending", _("待处理")
    PROCESSING = "processing", _("识别中")
    SUCCESS = "success", _("成功")
    FAILED = "failed", _("失败")


class DocumentRecognitionTool(models.Model):
    """Admin entry model for document recognition."""

    id: int
    name = models.CharField(max_length=64, default="Document Recognition")

    class Meta:
        managed = False
        verbose_name = _("文书智能识别")
        verbose_name_plural = _("文书智能识别")


class DocumentRecognitionTask(models.Model):
    """文书识别任务。"""

    id: int
    file_path = models.CharField(max_length=1024, verbose_name=_("文件路径"))
    original_filename = models.CharField(max_length=256, verbose_name=_("原始文件名"))
    status = models.CharField(
        max_length=32,
        choices=DocumentRecognitionStatus.choices,
        default=DocumentRecognitionStatus.PENDING,
        verbose_name=_("任务状态"),
    )
    document_type = models.CharField(max_length=32, null=True, blank=True, verbose_name=_("文书类型"))
    case_number = models.CharField(max_length=128, null=True, blank=True, verbose_name=_("案号"))
    key_time = models.DateTimeField(null=True, blank=True, verbose_name=_("关键时间"))
    confidence = models.FloatField(null=True, blank=True, verbose_name=_("置信度"))
    extraction_method = models.CharField(max_length=32, null=True, blank=True, verbose_name=_("提取方式"))
    raw_text = models.TextField(null=True, blank=True, verbose_name=_("原始文本"))
    renamed_file_path = models.CharField(max_length=1024, null=True, blank=True, verbose_name=_("重命名后路径"))
    binding_success = models.BooleanField(null=True, verbose_name=_("绑定成功"))
    case = models.ForeignKey(
        "cases.Case",
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        related_name="recognition_tasks",
        verbose_name=_("关联案件"),
    )
    case_log = models.ForeignKey(
        "cases.CaseLog",
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        related_name="recognition_tasks",
        verbose_name=_("案件日志"),
    )
    binding_message = models.CharField(max_length=512, null=True, blank=True, verbose_name=_("绑定消息"))
    binding_error_code = models.CharField(max_length=64, null=True, blank=True, verbose_name=_("绑定错误码"))
    error_message = models.TextField(null=True, blank=True, verbose_name=_("错误信息"))
    notification_sent = models.BooleanField(default=False, verbose_name=_("通知已发送"))
    notification_sent_at = models.DateTimeField(null=True, blank=True, verbose_name=_("通知发送时间"))
    notification_error = models.TextField(null=True, blank=True, verbose_name=_("通知错误信息"))
    notification_file_sent = models.BooleanField(default=False, verbose_name=_("文件已发送"))
    created_at = models.DateTimeField(auto_now_add=True, verbose_name=_("创建时间"))
    started_at = models.DateTimeField(null=True, blank=True, verbose_name=_("开始时间"))
    finished_at = models.DateTimeField(null=True, blank=True, verbose_name=_("完成时间"))

    class Meta:
        managed = False
        db_table = "automation_documentrecognitiontask"
        verbose_name = _("文书识别任务")
        verbose_name_plural = _("文书识别任务")
        ordering: ClassVar[list[str]] = ["-created_at"]

    def __str__(self) -> str:
        return f"识别任务 #{self.id} - {self.get_status_display()}"