@celery_app.task(name='froide.foirequest.tasks.ocr_pdf_task', time_limit=(60 * 5), soft_time_limit=(60 * 4))
def ocr_pdf_task(att_id, target_id, can_approve=True):
    from filingcabinet.pdf_utils import run_ocr
    try:
        attachment = FoiAttachment.objects.get(pk=att_id)
    except FoiAttachment.DoesNotExist:
        return
    try:
        target = FoiAttachment.objects.get(pk=target_id)
    except FoiAttachment.DoesNotExist:
        return
    try:
        pdf_bytes = run_ocr(attachment.file.path, language=(settings.TESSERACT_LANGUAGE if settings.TESSERACT_LANGUAGE else settings.LANGUAGE_CODE), timeout=180)
    except SoftTimeLimitExceeded:
        pdf_bytes = None
    if (pdf_bytes is None):
        attachment.can_approve = can_approve
        attachment.save()
        target.delete()
        return
    new_file = ContentFile(pdf_bytes)
    target.size = new_file.size
    target.file.save(target.name, new_file)
    target.save()
@celery_app.task(name='froide.foirequest.tasks.ocr_pdf_task', time_limit=(60 * 5), soft_time_limit=(60 * 4))
def ocr_pdf_task(att_id, target_id, can_approve=True):
    from filingcabinet.pdf_utils import run_ocr
    try:
        attachment = FoiAttachment.objects.get(pk=att_id)
    except FoiAttachment.DoesNotExist:
        return
    try:
        target = FoiAttachment.objects.get(pk=target_id)
    except FoiAttachment.DoesNotExist:
        return
    try:
        pdf_bytes = run_ocr(attachment.file.path, language=(settings.TESSERACT_LANGUAGE if settings.TESSERACT_LANGUAGE else settings.LANGUAGE_CODE), timeout=180)
    except SoftTimeLimitExceeded:
        pdf_bytes = None
    if (pdf_bytes is None):
        attachment.can_approve = can_approve
        attachment.save()
        target.delete()
        return
    new_file = ContentFile(pdf_bytes)
    target.size = new_file.size
    target.file.save(target.name, new_file)
    target.save()