(domain, export_ids, exports_type, username,
                                  es_filters, download_id, owner_id,
                                  filename=None, expiry=10 * 60)
:param expiry:  Time period for the export to be available for download in minutes
:param expiry:  Time period for the export to be available for download in minutes
: param expiry : Time period for the export to be available for download in minutes
def populate_export_download_task(domain, export_ids, exports_type, username,
                                  es_filters, download_id, owner_id,
                                  filename=None, expiry=10 * 60):
    """
    :param expiry:  Time period for the export to be available for download in minutes
    """

    email_requests = EmailExportWhenDoneRequest.objects.filter(
        domain=domain,
        download_id=download_id
    )

    if settings.STALE_EXPORT_THRESHOLD is not None and not email_requests.count():
        delay = get_task_time_to_start(populate_export_download_task.request.id)
        if delay.total_seconds() > settings.STALE_EXPORT_THRESHOLD:
            metrics_counter('commcare.exports.rejected_unfresh_export')
            raise RejectedStaleExport()

    export_instances = [get_export(exports_type, domain, export_id, username)
                        for export_id in export_ids]
    with TransientTempfile() as temp_path, metrics_track_errors('populate_export_download_task'):
        export_file = get_export_file(
            export_instances,
            es_filters,
            temp_path,
            # We don't have a great way to calculate progress if it's a bulk download,
            # so only track the progress for single instance exports.
            progress_tracker=populate_export_download_task if len(export_instances) == 1 else None
        )

        file_format = Format.from_format(export_file.format)
        filename = filename or export_instances[0].name

        with export_file as file_:
            db = get_blob_db()
            db.put(
                file_,
                domain=domain,
                parent_id=domain,
                type_code=CODES.data_export,
                key=download_id,
                timeout=expiry,
            )

            expose_blob_download(
                download_id,
                expiry=expiry * 60,
                mimetype=file_format.mimetype,
                content_disposition=safe_filename_header(filename, file_format.extension),
                download_id=download_id,
                owner_ids=[owner_id],
            )

    for email_request in email_requests:
        try:
            couch_user = CouchUser.get_by_user_id(email_request.user_id, domain=domain)
        except CouchUser.AccountTypeError:
            pass
        else:
            if couch_user is not None:
                process_email_request(domain, download_id, couch_user.get_email())
    email_requests.delete()
def populate_export_download_task ( domain , export_ids , exports_type , username , es_filters , download_id , owner_id , filename = None , expiry = 10 * 60 ) : email_requests = EmailExportWhenDoneRequest . objects . filter ( domain = domain , download_id = download_id ) if settings . STALE_EXPORT_THRESHOLD is not None and not email_requests . count ( ) : delay = get_task_time_to_start ( populate_export_download_task . request . id ) if delay . total_seconds ( ) > settings . STALE_EXPORT_THRESHOLD : metrics_counter ( 'commcare.exports.rejected_unfresh_export' ) raise RejectedStaleExport ( ) export_instances = [ get_export ( exports_type , domain , export_id , username ) for export_id in export_ids ] with TransientTempfile ( ) as temp_path , metrics_track_errors ( 'populate_export_download_task' ) : export_file = get_export_file ( export_instances , es_filters , temp_path , # We don't have a great way to calculate progress if it's a bulk download, # so only track the progress for single instance exports. progress_tracker = populate_export_download_task if len ( export_instances ) == 1 else None ) file_format = Format . from_format ( export_file . format ) filename = filename or export_instances [ 0 ] . name with export_file as file_ : db = get_blob_db ( ) db . put ( file_ , domain = domain , parent_id = domain , type_code = CODES . data_export , key = download_id , timeout = expiry , ) expose_blob_download ( download_id , expiry = expiry * 60 , mimetype = file_format . mimetype , content_disposition = safe_filename_header ( filename , file_format . extension ) , download_id = download_id , owner_ids = [ owner_id ] , ) for email_request in email_requests : try : couch_user = CouchUser . get_by_user_id ( email_request . user_id , domain = domain ) except CouchUser . AccountTypeError : pass else : if couch_user is not None : process_email_request ( domain , download_id , couch_user . get_email ( ) ) email_requests . delete ( )