@celery_app.task(name='froide.foirequest.tasks.convert_images_to_pdf_task', time_limit=(60 * 5), soft_time_limit=(60 * 4))
def convert_images_to_pdf_task(att_ids, target_id, instructions, can_approve=True):
    from filingcabinet.pdf_utils import convert_images_to_ocred_pdf
    att_qs = FoiAttachment.objects.filter(id__in=att_ids)
    att_map = {a.id: a for a in att_qs}
    atts = [att_map[a_id] for a_id in att_ids]
    try:
        target = FoiAttachment.objects.get(id=target_id)
    except FoiAttachment.DoesNotExist:
        return
    paths = [a.file.path for a in atts]
    try:
        pdf_bytes = convert_images_to_ocred_pdf(paths, instructions=instructions)
    except SoftTimeLimitExceeded:
        pdf_bytes = None
    if (pdf_bytes is None):
        att_qs.update(can_approve=can_approve)
        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.convert_images_to_pdf_task', time_limit=(60 * 5), soft_time_limit=(60 * 4))
def convert_images_to_pdf_task(att_ids, target_id, instructions, can_approve=True):
    from filingcabinet.pdf_utils import convert_images_to_ocred_pdf
    att_qs = FoiAttachment.objects.filter(id__in=att_ids)
    att_map = {a.id: a for a in att_qs}
    atts = [att_map[a_id] for a_id in att_ids]
    try:
        target = FoiAttachment.objects.get(id=target_id)
    except FoiAttachment.DoesNotExist:
        return
    paths = [a.file.path for a in atts]
    try:
        pdf_bytes = convert_images_to_ocred_pdf(paths, instructions=instructions)
    except SoftTimeLimitExceeded:
        pdf_bytes = None
    if (pdf_bytes is None):
        att_qs.update(can_approve=can_approve)
        target.delete()
        return
    new_file = ContentFile(pdf_bytes)
    target.size = new_file.size
    target.file.save(target.name, new_file)
    target.save()