Skip to content

Commit

Permalink
Merge pull request #1381 from bcgov/feat/kevin-1224
Browse files Browse the repository at this point in the history
Feat/kevin 1224
  • Loading branch information
prv-proton authored Dec 6, 2024
2 parents bf4110c + 4b28121 commit 5c99b01
Show file tree
Hide file tree
Showing 22 changed files with 176 additions and 30 deletions.
27 changes: 16 additions & 11 deletions backend/lcfs/web/api/email/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ def __init__(self, repo: CHESEmailRepository = Depends()):
self._token_expiry = None
self._validate_configuration()

# Initialize template environment
# Update template directory path to the root templates directory
template_dir = os.path.join(os.path.dirname(__file__), "templates")
self.template_env = Environment(loader=FileSystemLoader(template_dir))
self.template_env = Environment(
loader=FileSystemLoader(template_dir),
autoescape=True # Enable autoescaping for security
)

def _validate_configuration(self):
"""
Expand All @@ -63,7 +66,8 @@ async def send_notification_email(
notification_type, organization_id
)
if not recipient_emails:
logger.info(f"No subscribers for notification type: {notification_type}")
logger.info(f"""No subscribers for notification type: {
notification_type}""")
return False

# Render the email content
Expand All @@ -84,15 +88,16 @@ def _render_email_template(
) -> str:
"""
Render an email template using a predefined mapping of template names to file paths.
Raises an exception if template is not found.
"""
# Fetch template file path from the imported mapping
template_file = TEMPLATE_MAPPING.get(
template_name, TEMPLATE_MAPPING["default"]
)

# Render the template
template = self.template_env.get_template(template_file)
return template.render(context)
try:
template_file = TEMPLATE_MAPPING[template_name]
template = self.template_env.get_template(template_file)
return template.render(**context)
except Exception as e:
logger.error(f"Template rendering error: {str(e)}")
raise ValueError(
f"Failed to render email template for {template_name}")

def _build_email_payload(
self, recipients: List[str], context: Dict[str, Any], body: str
Expand Down
1 change: 0 additions & 1 deletion backend/lcfs/web/api/email/template_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,4 @@
"IDIR_DIRECTOR__COMPLIANCE_REPORT__MANAGER_RECOMMENDATION": "idir_director__compliance_report__manager_recommendation.html",
"IDIR_DIRECTOR__INITIATIVE_AGREEMENT__ANALYST_RECOMMENDATION": "idir_director__initiative_agreement__analyst_recommendation.html",
"IDIR_DIRECTOR__TRANSFER__ANALYST_RECOMMENDATION": "idir_director__transfer__analyst_recommendation.html",
"default": "default.html",
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% extends 'notification_base.html' %}
{% set notification_type = 'Credit Record' %}
{% set url_slug = 'credit-records' %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% extends 'notification_base.html' %}
{% set notification_type = 'Initiative Agreement' %}
{% set url_slug = 'initiative-agreement' %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% extends 'notification_base.html' %}
{% set notification_type = 'Transfer' %}
{% set url_slug = 'transfers' %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% extends 'notification_base.html' %}
{% set notification_type = 'Transfer' %}
{% set url_slug = 'transfers' %}
10 changes: 0 additions & 10 deletions backend/lcfs/web/api/email/templates/default.html

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% extends 'notification_base.html' %}
{% set notification_type = 'Credit Record' %}
{% set url_slug = 'credit-records' %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% extends 'notification_base.html' %}
{% set notification_type = 'Credit Record' %}
{% set url_slug = 'credit-records' %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% extends 'notification_base.html' %}
{% set notification_type = 'Credit Record' %}
{% set url_slug = 'credit-records' %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% extends 'notification_base.html' %}
{% set notification_type = 'Initiative Agreement' %}
{% set url_slug = 'initiative-agreements' %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% extends 'notification_base.html' %}
{% set notification_type = 'Transfer' %}
{% set url_slug = 'transfer-requests' %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% extends 'notification_base.html' %}
{% set notification_type = 'Transfer' %}
{% set url_slug = 'transfer-requests' %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% extends 'notification_base.html' %}
{% set notification_type = 'Transfer' %}
{% set url_slug = 'transfer-requests' %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% extends 'notification_base.html' %}
{% set notification_type = 'Credit Record' %}
{% set url_slug = 'credit-records' %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% extends 'notification_base.html' %}
{% set notification_type = 'Credit Record' %}
{% set url_slug = 'credit-records' %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% extends 'notification_base.html' %}
{% set notification_type = 'Credit Record' %}
{% set url_slug = 'credit-records' %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% extends 'notification_base.html' %}
{% set notification_type = 'Credit Record' %}
{% set url_slug = 'credit-records' %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% extends 'notification_base.html' %}
{% set notification_type = 'Initiative Agreement' %}
{% set url_slug = 'initiative-agreements' %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% extends 'notification_base.html' %}
{% set notification_type = 'Transfer' %}
{% set url_slug = 'transfer-requests' %}
8 changes: 0 additions & 8 deletions backend/lcfs/web/api/email/templates/initiative_approved.html

This file was deleted.

Loading

0 comments on commit 5c99b01

Please sign in to comment.