
from indico.core.celery import celery
from indico.core.db import db
from indico.modules.attachments.models.attachments import Attachment
from indico.modules.files.models.files import File


@celery.task(ignore_result=False)
def generate_materials_package(attachment_ids, event):
    from indico.modules.attachments.controllers.event_package import AttachmentPackageGeneratorMixin
    attachments = Attachment.query.filter(Attachment.id.in_(attachment_ids)).all()
    attachment_package_mixin = AttachmentPackageGeneratorMixin()
    attachment_package_mixin.event = event
    generated_zip = attachment_package_mixin._generate_zip_file(attachments, return_file=True)
    f = File(filename='material-package.zip', content_type='application/zip', meta={'event_id': event.id})
    context = ('event', event.id, 'attachment-package')
    f.save(context, generated_zip)
    db.session.add(f)
    db.session.commit()
    return f.signed_download_url


import os

from indico.core.celery import celery
from indico.core.db import db
from indico.modules.attachments.models.attachments import Attachment
from indico.modules.files.models.files import File


@celery.task(ignore_result=False)
def generate_materials_package(attachment_ids, event):
    from indico.modules.attachments.controllers.event_package import AttachmentPackageGeneratorMixin
    attachments = Attachment.query.filter(Attachment.id.in_(attachment_ids)).all()
    attachment_package_mixin = AttachmentPackageGeneratorMixin()
    attachment_package_mixin.event = event
    generated_zip = attachment_package_mixin._generate_zip_file(attachments, return_file=True)
    f = File(filename='material-package.zip', content_type='application/zip', meta={'event_id': event.id})
    context = ('event', event.id, 'attachment-package')
    f.save(context, generated_zip)
    db.session.add(f)
    db.session.commit()
    os.unlink(generated_zip.name)
    return f.signed_download_url

Delete material package temp file after creation
Delete material package temp file after creation

+ 
+ import os
  
  from indico.core.celery import celery
  from indico.core.db import db
  from indico.modules.attachments.models.attachments import Attachment
  from indico.modules.files.models.files import File
  
  
  @celery.task(ignore_result=False)
  def generate_materials_package(attachment_ids, event):
      from indico.modules.attachments.controllers.event_package import AttachmentPackageGeneratorMixin
      attachments = Attachment.query.filter(Attachment.id.in_(attachment_ids)).all()
      attachment_package_mixin = AttachmentPackageGeneratorMixin()
      attachment_package_mixin.event = event
      generated_zip = attachment_package_mixin._generate_zip_file(attachments, return_file=True)
      f = File(filename='material-package.zip', content_type='application/zip', meta={'event_id': event.id})
      context = ('event', event.id, 'attachment-package')
      f.save(context, generated_zip)
      db.session.add(f)
      db.session.commit()
+     os.unlink(generated_zip.name)
      return f.signed_download_url
  
Delete material package temp file after creation
## Code Before:

from indico.core.celery import celery
from indico.core.db import db
from indico.modules.attachments.models.attachments import Attachment
from indico.modules.files.models.files import File


@celery.task(ignore_result=False)
def generate_materials_package(attachment_ids, event):
    from indico.modules.attachments.controllers.event_package import AttachmentPackageGeneratorMixin
    attachments = Attachment.query.filter(Attachment.id.in_(attachment_ids)).all()
    attachment_package_mixin = AttachmentPackageGeneratorMixin()
    attachment_package_mixin.event = event
    generated_zip = attachment_package_mixin._generate_zip_file(attachments, return_file=True)
    f = File(filename='material-package.zip', content_type='application/zip', meta={'event_id': event.id})
    context = ('event', event.id, 'attachment-package')
    f.save(context, generated_zip)
    db.session.add(f)
    db.session.commit()
    return f.signed_download_url

## Instruction:
Delete material package temp file after creation
## Code After:

import os

from indico.core.celery import celery
from indico.core.db import db
from indico.modules.attachments.models.attachments import Attachment
from indico.modules.files.models.files import File


@celery.task(ignore_result=False)
def generate_materials_package(attachment_ids, event):
    from indico.modules.attachments.controllers.event_package import AttachmentPackageGeneratorMixin
    attachments = Attachment.query.filter(Attachment.id.in_(attachment_ids)).all()
    attachment_package_mixin = AttachmentPackageGeneratorMixin()
    attachment_package_mixin.event = event
    generated_zip = attachment_package_mixin._generate_zip_file(attachments, return_file=True)
    f = File(filename='material-package.zip', content_type='application/zip', meta={'event_id': event.id})
    context = ('event', event.id, 'attachment-package')
    f.save(context, generated_zip)
    db.session.add(f)
    db.session.commit()
    os.unlink(generated_zip.name)
    return f.signed_download_url
