Beispiel #1
0
def generate(report_context: ReportContext) -> None:
    """Generates and uploads the HTML file and the file attachment for the identified recipient's email.

    Receives the full user data, applies it to the HTML template and stores the result in Cloud Storage.

    Uploads the attachment content to Cloud Storage as a .txt file.

    Args:
        report_context: The context for a single recipient
    """

    prepared_data = report_context.get_prepared_data()

    html_content = generate_html_content(report_context)
    attachment_content = prepared_data["attachment_content"]

    html_path = utils.get_html_filepath(
        prepared_data[utils.KEY_BATCH_ID],
        prepared_data[utils.KEY_EMAIL_ADDRESS],
    )
    upload_file_contents_to_gcs(file_path=html_path,
                                file_contents=html_content,
                                content_type="text/html")

    if attachment_content:
        attachment_path = utils.get_attachment_filepath(
            prepared_data[utils.KEY_BATCH_ID],
            prepared_data[utils.KEY_EMAIL_ADDRESS],
        )

        upload_file_contents_to_gcs(
            file_path=attachment_path,
            file_contents=attachment_content,
            content_type="text/plain",
        )
Beispiel #2
0
def generate_html_content(report_context: ReportContext) -> str:
    """Generates the HTML file for the identified recipient's email."""
    try:
        return report_context.render_html()
    except KeyError as err:
        logging.error(
            "Attribute required for HTML template missing from recipient data: "
            "batch id = %s, email address = %s, attribute = %s",
            report_context.get_batch_id(),
            report_context.get_email_address(),
            err,
        )
        raise
    except Exception:
        logging.error(
            "Unexpected error during templating. Recipient = %s",
            report_context.get_email_address(),
        )
        raise
Beispiel #3
0
def generate(report_context: ReportContext) -> None:
    """Generates an email for the identified recipient.

    Receives the full user data, applies it to the HTML template and stores the result in Cloud Storage.

    Args:
        report_context: The context for a single recipient
    """
    prepared_data = report_context.get_prepared_data()
    check_for_required_keys(prepared_data)

    try:
        with open(
                report_context.get_html_template_filepath()) as html_template:
            template = Template(html_template.read())
            final_email = template.substitute(prepared_data)
    except KeyError as err:
        logging.error(
            "Attribute required for HTML template missing from recipient data: "
            "batch id = %s, email address = %s, attribute = %s",
            prepared_data[utils.KEY_BATCH_ID],
            prepared_data[utils.KEY_EMAIL_ADDRESS], err)
        raise
    except Exception:
        logging.error(
            "Unexpected error during templating. Recipient data = %s",
            prepared_data)
        raise

    html_bucket = utils.get_html_bucket_name()
    html_filename = ''
    try:
        html_filename = utils.get_html_filename(
            prepared_data[utils.KEY_BATCH_ID],
            prepared_data[utils.KEY_EMAIL_ADDRESS])
        utils.upload_string_to_storage(html_bucket, html_filename, final_email,
                                       "text/html")
    except Exception:
        logging.error("Error while attempting upload of %s/%s", html_bucket,
                      html_filename)
        raise
Beispiel #4
0
def start_chart_generation(report_context: ReportContext) -> None:
    """Starts chart generation for a recipient.

    Uses Pub/Sub to send a message to the chart function. The message contains all of the recipient's data since the
    chart function will pass it along to the next step in the generation process.

    Args:
        report_context: The report context containing the data and chart type
    """
    publisher = pubsub_v1.PublisherClient()

    prepared_data = report_context.get_prepared_data()
    payload = json.dumps(
        prepared_data
    )  # no error checking here since we already validated the JSON previously
    # TODO(#3260): Generalize this with report context
    topic = utils.get_chart_topic()
    publisher.publish(topic, payload.encode("utf-8"))