added file highlighting

This commit is contained in:
Alexander Karpov 2022-08-27 14:30:23 +03:00
parent 1d859084d1
commit 1e6c23477e
5 changed files with 39 additions and 18 deletions

View File

@ -36,7 +36,6 @@ class RetireDocxSerializer(APIView):
class ListCreateWordDocxApiView(generics.ListCreateAPIView): class ListCreateWordDocxApiView(generics.ListCreateAPIView):
parser_classes = [FormParser, MultiPartParser]
serializer_class = WordDocxSerializer serializer_class = WordDocxSerializer
queryset = WordDocx.objects.all() queryset = WordDocx.objects.all()

View File

@ -11,20 +11,4 @@ class Migration(migrations.Migration):
dependencies = [] dependencies = []
operations = [ operations = [
migrations.CreateModel(
name="Docx",
fields=[
(
"uuid",
models.UUIDField(
default=uuid.uuid4,
editable=False,
primary_key=True,
serialize=False,
unique=True,
),
),
("file", models.FileField(upload_to="")),
],
),
] ]

View File

@ -2,13 +2,14 @@ from django.db.models.signals import post_save
from django.dispatch import receiver from django.dispatch import receiver
from checker.models import Docx, WordDocx from checker.models import Docx, WordDocx
from checker.tasks import process_file, process_word from checker.tasks import process_file, process_word, highlight_file
@receiver(post_save, sender=Docx) @receiver(post_save, sender=Docx)
def create_docs(sender, instance, created, **kwargs): def create_docs(sender, instance, created, **kwargs):
if created: if created:
process_file.apply_async(kwargs={"pk": instance.pk}) process_file.apply_async(kwargs={"pk": instance.pk})
highlight_file.apply_async(kwargs={"pk": instance.pk})
return return

View File

@ -1,6 +1,8 @@
import docx2txt import docx2txt
import requests import requests
from celery import shared_task from celery import shared_task
from docx import Document
from docx.enum.text import WD_COLOR_INDEX
from checker.models import Paragraph, Docx, WordDocx, WordParagraph from checker.models import Paragraph, Docx, WordDocx, WordParagraph
from checker.services.file import process_paragraphs, process_word_paragraphs from checker.services.file import process_paragraphs, process_word_paragraphs
@ -77,3 +79,37 @@ def process_word(pk: int):
return f"ok, {pk}" return f"ok, {pk}"
@shared_task
def highlight_file(pk: int):
c = 0
title = True
file = Docx.objects.get(pk=pk)
document = Document(file.file.path)
for paragraph in document.paragraphs:
if title:
if (
paragraph.text
and len(paragraph.text) > 2
and paragraph.text[:2] == "1."
):
title = False
else:
if paragraph.text:
x = requests.post(
"http://109.248.175.223:5000/api", json={1: paragraph.text}
)
if x.status_code == 200:
el_id, dat = x.json()["1"]
if dat < 50:
text = paragraph.text
paragraph.clear()
run = paragraph.add_run()
run.font.highlight_color = WD_COLOR_INDEX.RED
run.add_text(text)
c += 1
else:
print("AI ERROR")
document.save(file.file.path)
return f"highlighted {c}, {pk}"

View File

@ -15,4 +15,5 @@ psutil
dj-database-url dj-database-url
uuid uuid
docx2txt docx2txt
python-docx
requests-async requests-async