diff --git a/checker/api/views.py b/checker/api/views.py index 5861ce6..9acc2ff 100644 --- a/checker/api/views.py +++ b/checker/api/views.py @@ -36,7 +36,6 @@ class RetireDocxSerializer(APIView): class ListCreateWordDocxApiView(generics.ListCreateAPIView): - parser_classes = [FormParser, MultiPartParser] serializer_class = WordDocxSerializer queryset = WordDocx.objects.all() diff --git a/checker/migrations/0001_initial.py b/checker/migrations/0001_initial.py index 839e5bf..3278440 100644 --- a/checker/migrations/0001_initial.py +++ b/checker/migrations/0001_initial.py @@ -11,20 +11,4 @@ class Migration(migrations.Migration): dependencies = [] 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="")), - ], - ), ] diff --git a/checker/signals.py b/checker/signals.py index a7d02e4..3969077 100644 --- a/checker/signals.py +++ b/checker/signals.py @@ -2,13 +2,14 @@ from django.db.models.signals import post_save from django.dispatch import receiver 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) def create_docs(sender, instance, created, **kwargs): if created: process_file.apply_async(kwargs={"pk": instance.pk}) + highlight_file.apply_async(kwargs={"pk": instance.pk}) return diff --git a/checker/tasks.py b/checker/tasks.py index 3c90719..960c396 100644 --- a/checker/tasks.py +++ b/checker/tasks.py @@ -1,6 +1,8 @@ import docx2txt import requests 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.services.file import process_paragraphs, process_word_paragraphs @@ -77,3 +79,37 @@ def process_word(pk: int): 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}" diff --git a/requirements/base.txt b/requirements/base.txt index 365ca44..3fea1e2 100644 --- a/requirements/base.txt +++ b/requirements/base.txt @@ -15,4 +15,5 @@ psutil dj-database-url uuid docx2txt +python-docx requests-async \ No newline at end of file