2022-08-28 12:37:28 +03:00
|
|
|
import magic
|
|
|
|
|
2022-08-26 20:04:45 +03:00
|
|
|
from django.db.models.signals import post_save
|
2022-08-28 12:37:28 +03:00
|
|
|
from django.core.files import File
|
2022-08-26 20:04:45 +03:00
|
|
|
from django.dispatch import receiver
|
2022-08-28 12:37:28 +03:00
|
|
|
from celery import chain
|
2022-08-26 20:04:45 +03:00
|
|
|
|
2022-08-27 11:59:23 +03:00
|
|
|
from checker.models import Docx, WordDocx
|
2022-08-28 12:37:28 +03:00
|
|
|
from checker.services.file import doc_to_docx, doc_to_odt
|
2022-08-27 14:30:23 +03:00
|
|
|
from checker.tasks import process_file, process_word, highlight_file
|
2022-08-26 20:04:45 +03:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_save, sender=Docx)
|
2022-08-26 23:18:02 +03:00
|
|
|
def create_docs(sender, instance, created, **kwargs):
|
2022-08-26 20:04:45 +03:00
|
|
|
if created:
|
2022-08-28 12:37:28 +03:00
|
|
|
type = magic.from_file(instance.file.path, mime=True)
|
|
|
|
if type == "application/msword":
|
|
|
|
pth = doc_to_docx(instance.file.path)
|
|
|
|
with open(pth, 'rb') as f:
|
|
|
|
instance.file = File(f, name=pth.split("/")[-1])
|
|
|
|
instance.save(update_fields=["file"])
|
|
|
|
elif type == "application/vnd.oasis.opendocument.text":
|
|
|
|
pth = doc_to_odt(instance.file.path)
|
|
|
|
with open(pth, 'rb') as f:
|
|
|
|
instance.file = File(f, name=pth.split("/")[-1])
|
|
|
|
instance.save(update_fields=["file"])
|
|
|
|
|
|
|
|
chain(process_file.s(instance.pk), highlight_file.s()).apply_async()
|
2022-08-26 20:04:45 +03:00
|
|
|
return
|
2022-08-27 11:59:23 +03:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_save, sender=WordDocx)
|
|
|
|
def create_docs(sender, instance, created, **kwargs):
|
|
|
|
if created:
|
|
|
|
process_word.apply_async(kwargs={"pk": instance.pk})
|
|
|
|
return
|
|
|
|
|