KeywordRetriever/retriever/tasks.py

28 lines
1.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# app/tasks.py, 可以复用的task 改这个文件记得重启 Celery
import ast
from celery import shared_task
from .models import RetrieverTask, UploadFile, KeywordParagraph
from .tools.keyword_find import util_keyword_find
@shared_task
def start_retriever_job(task_id):
current_task = RetrieverTask.objects.get(task_uuid=task_id)
task_keywords = ast.literal_eval(current_task.task_keywords) # 将字符串转换为list
# print(task_keywords)
for each in current_task.attachment.all():
if not each.is_checked:
result_dict = util_keyword_find(each.file_path, task_keywords)
UploadFile.objects.filter(file_id=each.file_id).update(is_checked=True) # 更新is_checked字段
KeywordParagraph.objects.bulk_create(
[KeywordParagraph(related_file=each, keyword=para_keyword, paragraph=paragraph) for paragraph, para_keyword in
zip(result_dict['find_list'], result_dict['paragraph_keyword'])]) # 批量创建KeywordParagraph对象
elif each.is_checked:
continue
RetrieverTask.objects.filter(task_uuid=task_id).update(task_status=True)
return task_id