import json import os import argparse from pathlib import Path from notifications_delivery import config from notifications_delivery.utils import email_utils, template_renderer # TODO: please store one of each type of template data as needed in the future ANALYTICS_DIGEST_NOTIFICATION = "analytics_digest" SPIKE_DETECTOR_NOTIFICATION = "spike_detector" DIGITAL_REJECTION_NOTIFICATION = "digital_rejection" DIGITAL_APPROVAL_NOTIFICATION = "digital_approval" USER_PERMISSIONS_UPDATED = "user_permissions_updated" DEFAULT_BRAND = "orchard" DEFAULT_LANG = "en" def generate_email(notification_type: str, lang: str, brand: str, send_email: bool): base_path = Path(os.path.dirname(os.path.realpath("__file__"))) / "example_messages" / notification_type getstream_message_path = base_path / "getstream_message.json" queue_message_path = base_path / "queue_message.json" if getstream_message_path.exists(): notification_data = json.loads(getstream_message_path.read_text()) template_data = notification_data[0]["new"][0] elif queue_message_path.exists(): template_data = json.loads(queue_message_path.read_text())['variables'] else: raise FileNotFoundError(f'No payload found for {notification_type}') if brand and notification_type not in (DIGITAL_APPROVAL_NOTIFICATION, DIGITAL_REJECTION_NOTIFICATION): template_name = f"branded_{notification_type}" else: template_name = notification_type template_data["default_brand"] = brand template_data["preview_text"] = email_utils.get_preview_text(notification_type, template_data, lang) rendered_html = template_renderer.render_template(template_name, template_data, lang) email_utils.generate_html_file(template_name, rendered_html) # Just to be 100% sure we are not sending emails in prod even if this is a standalone script if send_email and config.ENVIRONMENT != config.ENVIRONMENT_PROD: subject = email_utils.get_email_subject(notification_type, template_data, lang) email_utils.send_smtp_email(subject, rendered_html) if __name__ == '__main__': parser = argparse.ArgumentParser() # TODO: add choices for notification_type, brand, lang parser.add_argument("notification_type", default=ANALYTICS_DIGEST_NOTIFICATION, nargs="?", help="Notification type to render") parser.add_argument("-s", "--send-email", action="store_true", default=False, help="Send email after render") parser.add_argument("-b", "--brand", default=DEFAULT_BRAND, help="Brand to use for email style") parser.add_argument("-l", "--lang", default=DEFAULT_LANG, help="Language of email") # TODO: eventually all templates should become branded parser.add_argument("-ub", "--unbranded", dest="branded", action="store_false", default=True, help="Use unbranded template") args = parser.parse_args() generate_email( notification_type=args.notification_type, lang=args.lang, brand=args.brand if args.branded else None, send_email=args.send_email ) print("Template rendered successfully!")