@celery_app.task(name='froide.foirequest.tasks.redact_attachment_task', time_limit=(60 * 6), soft_time_limit=(60 * 5))
def redact_attachment_task(att_id, target_id, instructions):
    from filingcabinet.pdf_utils import run_ocr
    from froide.helper.redaction import redact_file
    try:
        attachment = FoiAttachment.objects.get(pk=att_id)
    except FoiAttachment.DoesNotExist:
        return
    if (att_id != target_id):
        try:
            target = FoiAttachment.objects.get(pk=target_id)
        except FoiAttachment.DoesNotExist:
            return
    else:
        target = attachment
    logger.info('Trying redaction of %s', attachment.id)
    try:
        pdf_bytes = redact_file(attachment.file, instructions)
    except Exception:
        logger.error('PDF redaction error', exc_info=True)
        pdf_bytes = None
    if (pdf_bytes is None):
        logger.info('Redaction failed %s', attachment.id)
        if attachment.redacted:
            attachment.redacted = None
        if attachment.is_redacted:
            attachment.approved = True
            attachment.can_approve = True
        attachment.pending = False
        attachment.save()
        if (not target.file):
            target.delete()
        return
    logger.info('Redaction successful %s', attachment.id)
    pdf_file = ContentFile(pdf_bytes)
    target.size = pdf_file.size
    target.file.save(target.name, pdf_file, save=False)
    logger.info('Trying OCR %s', target.id)
    try:
        pdf_bytes = run_ocr(target.file.path, language=(settings.TESSERACT_LANGUAGE if settings.TESSERACT_LANGUAGE else settings.LANGUAGE_CODE), timeout=(60 * 4))
    except SoftTimeLimitExceeded:
        pdf_bytes = None
    if (pdf_bytes is not None):
        logger.info('OCR successful %s', target.id)
        pdf_file = ContentFile(pdf_bytes)
        target.size = pdf_file.size
        target.file.save(target.name, pdf_file, save=False)
    else:
        logger.info('OCR failed %s', target.id)
    target.can_approve = True
    target.pending = False
    target.approve_and_save()
    FoiAttachment.attachment_published.send(sender=target, user=None)
@celery_app.task(name='froide.foirequest.tasks.redact_attachment_task', time_limit=(60 * 6), soft_time_limit=(60 * 5))
def redact_attachment_task(att_id, target_id, instructions):
    from filingcabinet.pdf_utils import run_ocr
    from froide.helper.redaction import redact_file
    try:
        attachment = FoiAttachment.objects.get(pk=att_id)
    except FoiAttachment.DoesNotExist:
        return
    if (att_id != target_id):
        try:
            target = FoiAttachment.objects.get(pk=target_id)
        except FoiAttachment.DoesNotExist:
            return
    else:
        target = attachment
    logger.info('Trying redaction of %s', attachment.id)
    try:
        pdf_bytes = redact_file(attachment.file, instructions)
    except Exception:
        logger.error('PDF redaction error', exc_info=True)
        pdf_bytes = None
    if (pdf_bytes is None):
        logger.info('Redaction failed %s', attachment.id)
        if attachment.redacted:
            attachment.redacted = None
        if attachment.is_redacted:
            attachment.approved = True
            attachment.can_approve = True
        attachment.pending = False
        attachment.save()
        if (not target.file):
            target.delete()
        return
    logger.info('Redaction successful %s', attachment.id)
    pdf_file = ContentFile(pdf_bytes)
    target.size = pdf_file.size
    target.file.save(target.name, pdf_file, save=False)
    logger.info('Trying OCR %s', target.id)
    try:
        pdf_bytes = run_ocr(target.file.path, language=(settings.TESSERACT_LANGUAGE if settings.TESSERACT_LANGUAGE else settings.LANGUAGE_CODE), timeout=(60 * 4))
    except SoftTimeLimitExceeded:
        pdf_bytes = None
    if (pdf_bytes is not None):
        logger.info('OCR successful %s', target.id)
        pdf_file = ContentFile(pdf_bytes)
        target.size = pdf_file.size
        target.file.save(target.name, pdf_file, save=False)
    else:
        logger.info('OCR failed %s', target.id)
    target.can_approve = True
    target.pending = False
    target.approve_and_save()
    FoiAttachment.attachment_published.send(sender=target, user=None)