|
| 1 | +import json |
| 2 | +import os |
| 3 | +from typing import Dict |
| 4 | + |
| 5 | +import boto3 |
| 6 | +from botocore.exceptions import ClientError |
| 7 | + |
| 8 | +TEMPLATES_FILE = "app/utilities/ses/ses_templates.json" |
| 9 | +TEMPLATES_DIR = "app/utilities/ses/template_files" |
| 10 | + |
| 11 | +ses_client = boto3.client( |
| 12 | + "ses", |
| 13 | + region_name=os.getenv("AWS_REGION"), |
| 14 | + aws_access_key_id=os.getenv("AWS_ACCESS_KEY"), |
| 15 | + aws_secret_access_key=os.getenv("AWS_SECRET_KEY"), |
| 16 | +) |
| 17 | + |
| 18 | + |
| 19 | +def load_templates_metadata(file_path: str) -> Dict: |
| 20 | + try: |
| 21 | + with open(file_path, "r") as file: |
| 22 | + return json.load(file) |
| 23 | + except FileNotFoundError: |
| 24 | + print(f"Error: {file_path} not found.") |
| 25 | + return [] |
| 26 | + except json.JSONDecodeError as e: |
| 27 | + print(f"Error parsing {file_path}: {e}") |
| 28 | + return [] |
| 29 | + |
| 30 | + |
| 31 | +def load_file_content(file_path: str) -> str: |
| 32 | + """Reads the content of a file.""" |
| 33 | + try: |
| 34 | + with open(file_path, "r") as file: |
| 35 | + return file.read() |
| 36 | + except FileNotFoundError: |
| 37 | + print(f"Error: File '{file_path}' not found.") |
| 38 | + return "" |
| 39 | + |
| 40 | + |
| 41 | +templates_metadata = load_templates_metadata(TEMPLATES_FILE) |
| 42 | + |
| 43 | + |
| 44 | +# Function to create SES template |
| 45 | +def create_ses_template(template_metadata): |
| 46 | + name = template_metadata["TemplateName"] |
| 47 | + try: |
| 48 | + text_part = load_file_content(template_metadata["TextPart"]) |
| 49 | + html_part = load_file_content(template_metadata["HtmlPart"]) |
| 50 | + if not text_part or not html_part: |
| 51 | + print(f"Skipping template '{name}' missing content.") |
| 52 | + return |
| 53 | + |
| 54 | + template = { |
| 55 | + "TemplateName": template_metadata["TemplateName"], |
| 56 | + "SubjectPart": template_metadata["SubjectPart"], |
| 57 | + "TextPart": text_part, |
| 58 | + "HtmlPart": html_part, |
| 59 | + } |
| 60 | + ses_client.create_template(Template=template) |
| 61 | + print(f"SES template '{name}' created successfully!") |
| 62 | + except ClientError as e: |
| 63 | + if e.response["Error"]["Code"] == "TemplateAlreadyExists": |
| 64 | + print(f"SES template '{name}' already exists.") |
| 65 | + else: |
| 66 | + print(f"An error occurred while creating the SES template: {e}") |
| 67 | + |
| 68 | + |
| 69 | +# Ensure SES templates are available at app startup |
| 70 | +def ensure_ses_templates(): |
| 71 | + for template_metadata in templates_metadata: |
| 72 | + name = template_metadata["TemplateName"] |
| 73 | + try: |
| 74 | + # Check if the template exists |
| 75 | + ses_client.get_template(TemplateName=name) |
| 76 | + print(f"SES template '{name}' already exists.") |
| 77 | + except ClientError as e: |
| 78 | + if e.response["Error"]["Code"] == "TemplateDoesNotExist": |
| 79 | + print(f"SES template '{name}' does not exist. Creating template...") |
| 80 | + create_ses_template(template_metadata) |
| 81 | + else: |
| 82 | + print(f"An error occurred while checking the SES template: {e}") |
0 commit comments