From bbca1a5114d5dc67c03c9c9272c48ad5b4237a0c Mon Sep 17 00:00:00 2001 From: zty Date: Mon, 8 Jul 2024 18:21:12 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9Epdf=20=E5=88=A0?= =?UTF-8?q?=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/apps/information/serializers.py | 2 +- server/apps/information/views.py | 15 +++++++++++++++ server/utils/pdf2txt.py | 2 -- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/server/apps/information/serializers.py b/server/apps/information/serializers.py index cf07c2f..faeeffa 100644 --- a/server/apps/information/serializers.py +++ b/server/apps/information/serializers.py @@ -67,7 +67,7 @@ class AuditLogSerializer(serializers.ModelSerializer): fields = '__all__' class ParsepdfSerializer(serializers.ModelSerializer): - + dep = serializers.CharField(source='belong_dept.name', read_only=True) class Meta(BaseMeta): model = Parsepdf fields = '__all__' diff --git a/server/apps/information/views.py b/server/apps/information/views.py index c84586e..c72ea46 100644 --- a/server/apps/information/views.py +++ b/server/apps/information/views.py @@ -367,6 +367,21 @@ class ParsePdfViewSet(RbacFilterSet, CreateUpdateCustomMixin, ModelViewSet): filterset_fields = ['belong_dept', 'annual'] perms_map = {'get': '*', 'post': 'pdf_create'} + def destroy(self, request, *args, **kwargs): + # 判断执行状态,如果是“执行中”,则不允许删除 + if self.get_object().status == "执行中": + return Response({'message': '执行中的数据不能删除'}, status=status.HTTP_400_BAD_REQUEST) + # 删除文件 + file_path = os.path.join(settings.BASE_DIR, self.get_object().parse_excel) + if os.path.exists(file_path): + os.remove(file_path) + pdf_path = os.path.join(settings.BASE_DIR, self.get_object().pdf_path) + if os.path.exists(pdf_path): + os.remove(pdf_path) + + # 删除数据库记录 + return super().destroy(request, *args, **kwargs) + #解析pdf到excel @action(detail=False, methods=['post'], perms_map={'post':'pdf_create'}) @transaction.atomic diff --git a/server/utils/pdf2txt.py b/server/utils/pdf2txt.py index dd9b50a..97c41c6 100644 --- a/server/utils/pdf2txt.py +++ b/server/utils/pdf2txt.py @@ -345,7 +345,6 @@ def get_index(text, pattern, span): return remain_text def run(pdf_path, excel_path, id): - print("-----------ids", id) # 提取PDF文本 text = extract_text_from_pdf(pdf_path) with open("pdf2txt.txt", "w", encoding="utf-8") as f: @@ -353,7 +352,6 @@ def run(pdf_path, excel_path, id): for pattern, local in RE_LIST: # 使用正则表达式匹配文本 matches = match_text_with_regex(text, pattern, 1) - print(matches,"------") fill_excel(matches, excel_path, local) # 特殊处理的 for p, l, s in SPECIALLIST: