Example #1
0
def populate_export_download_task(export_instances,
                                  filters,
                                  download_id,
                                  filename=None,
                                  expiry=10 * 60):
    """
    :param expiry:  Time period for the export to be available for download in minutes
    """
    domain = export_instances[0].domain
    with TransientTempfile() as temp_path, datadog_track_errors(
            'populate_export_download_task'):
        export_file = get_export_file(
            export_instances,
            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,
            )

    email_requests = EmailExportWhenDoneRequest.objects.filter(
        domain=domain, download_id=download_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()
Example #2
0
def process_bounced_emails():
    if settings.RETURN_PATH_EMAIL and settings.RETURN_PATH_EMAIL_PASSWORD:
        try:
            with BouncedEmailManager(
                    delete_processed_messages=True
            ) as bounced_manager, datadog_track_errors(
                    'process_bounced_emails_task'):
                bounced_manager.process_aws_notifications()
                bounced_manager.process_daemon_messages()
        except Exception as e:
            notify_exception(
                None,
                message="Encountered error while processing bounced emails",
                details={
                    'error': e,
                })
Example #3
0
def populate_export_download_task(domain,
                                  export_ids,
                                  exports_type,
                                  username,
                                  filters,
                                  download_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:
            datadog_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, datadog_track_errors(
            'populate_export_download_task'):
        export_file = get_export_file(
            export_instances,
            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,
            )

    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()