Skip to content

Commit 486f463

Browse files
committed
fixup! feat(eap): Add email setup and template
1 parent 4422aaf commit 486f463

File tree

4 files changed

+60
-14
lines changed

4 files changed

+60
-14
lines changed

eap/serializers.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,6 @@ def create(self, validated_data: dict[str, typing.Any]):
182182
transaction.on_commit(
183183
lambda: send_eap_registration_email.delay(
184184
instance.id,
185-
instance.get_eap_type_display(),
186185
)
187186
)
188187
return instance

eap/tasks.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import logging
22

33
from celery import shared_task
4+
from django.conf import settings
45
from django.contrib.auth import get_user_model
56
from django.template.loader import render_to_string
67

78
from eap.models import EAPRegistration
8-
from eap.utils import get_eap_registration_email_context
9+
from eap.utils import (
10+
get_coordinator_emails_by_region,
11+
get_eap_registration_email_context,
12+
)
913
from notifications.notification import send_notification
1014

1115
User = get_user_model()
@@ -14,31 +18,31 @@
1418

1519

1620
@shared_task
17-
def send_eap_registration_email(eap_registration_id: int, eap_type: str):
21+
def send_eap_registration_email(eap_registration_id: int):
1822

19-
if not eap_registration_id:
23+
instance = EAPRegistration.objects.filter(id=eap_registration_id).first()
24+
if not instance:
2025
return None
2126

22-
instance = EAPRegistration.objects.get(id=eap_registration_id)
23-
recipient = instance.ifrc_contact_email
24-
25-
if not recipient:
26-
logger.warning("Email not sent because recipients email not found.")
27-
return None
27+
regional_coordinator_emails = get_coordinator_emails_by_region(instance.country.region)
2828

29+
recipients = [
30+
settings.EMAIL_EAP_DREFF_ANTICIPATORY_PILLAR,
31+
instance.ifrc_contact_email,
32+
]
2933
cc_recipients = [
3034
instance.national_society_contact_email,
31-
# TODO @sudip-khanal: Add partners national society email
32-
# TODO @sudip-khanal: Add Contact email provided by IFRC
35+
*settings.EMAIL_EAP_DREFF_AA_GLOBAL_TEAM,
36+
*regional_coordinator_emails,
3337
]
3438
email_context = get_eap_registration_email_context(instance)
35-
email_subject = f"[{eap_type} IN DEVELOPMENT] {instance.country} {instance.disaster_type}"
39+
email_subject = f"[{instance.get_eap_type_display()} IN DEVELOPMENT] {instance.country} {instance.disaster_type}"
3640
email_body = render_to_string("email/eap/registration.html", email_context)
3741
email_type = "EAP-REGISTRATION"
3842

3943
send_notification(
4044
subject=email_subject,
41-
recipients=recipient,
45+
recipients=recipients,
4246
html=email_body,
4347
mailtype=email_type,
4448
cc_recipients=cc_recipients,

eap/utils.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,31 @@
66
from django.core.exceptions import ValidationError
77
from django.db import models
88

9+
from api.models import Region, RegionName
10+
11+
REGION_EMAIL_MAP = {
12+
RegionName.AFRICA: settings.EMAIL_EAP_AFRICA_COORDINATORS,
13+
RegionName.AMERICAS: settings.EMAIL_EAP_AMERICAS_COORDINATORS,
14+
RegionName.ASIA_PACIFIC: settings.EMAIL_EAP_ASIA_PACIFIC_COORDINATORS,
15+
RegionName.EUROPE: settings.EMAIL_EAP_EUROPE_COORDINATORS,
16+
RegionName.MENA: settings.EMAIL_EAP_MENA_COORDINATORS,
17+
}
18+
19+
20+
def get_coordinator_emails_by_region(region: Region | None) -> list[str]:
21+
"""
22+
This function uses the REGION_EMAIL_MAP dictionary to map Region name to the corresponding list of email addresses.
23+
Args:
24+
region: Region instance for which the coordinator emails are needed.
25+
Returns:
26+
List of email addresses corresponding to the region coordinators.
27+
Returns an empty list if the region is None or not found in the mapping.
28+
"""
29+
if not region:
30+
return []
31+
32+
return REGION_EMAIL_MAP.get(region.name, [])
33+
934

1035
def get_eap_registration_email_context(instance):
1136
from eap.serializers import EAPRegistrationSerializer

main/settings.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@
6868
EMAIL_USER=(str, None),
6969
EMAIL_PASS=(str, None),
7070
DEBUG_EMAIL=(bool, False), # This was 0/1 before
71+
# EAP-EMAILS
72+
EMAIL_EAP_DREFF_ANTICIPATORY_PILLAR=(str, None),
73+
EMAIL_EAP_DREFF_AA_GLOBAL_TEAM=(list, None),
74+
EMAIL_EAP_AFRICA_COORDINATORS=(list, None),
75+
EMAIL_EAP_AMERICAS_COORDINATORS=(list, None),
76+
EMAIL_EAP_ASIA_PACIFIC_COORDINATORS=(list, None),
77+
EMAIL_EAP_EUROPE_COORDINATORS=(list, None),
78+
EMAIL_EAP_MENA_COORDINATORS=(list, None),
7179
# TEST_EMAILS=(list, ['[email protected]']), # maybe later
7280
# Translation
7381
# Translator Available:
@@ -198,6 +206,7 @@ def parse_domain(*env_keys: str) -> str:
198206
ALLOWED_HOSTS = [
199207
"localhost",
200208
"0.0.0.0",
209+
"127.0.0.1",
201210
urlparse(GO_API_URL).hostname,
202211
*env("DJANGO_ADDITIONAL_ALLOWED_HOSTS"),
203212
]
@@ -581,6 +590,15 @@ def parse_domain(*env_keys: str) -> str:
581590
DEBUG_EMAIL = env("DEBUG_EMAIL")
582591
# TEST_EMAILS = env('TEST_EMAILS') # maybe later
583592

593+
# EAP-Email
594+
EMAIL_EAP_DREFF_ANTICIPATORY_PILLAR = env("EMAIL_EAP_DREFF_ANTICIPATORY_PILLAR")
595+
EMAIL_EAP_DREFF_AA_GLOBAL_TEAM = env("EMAIL_EAP_DREFF_AA_GLOBAL_TEAM")
596+
EMAIL_EAP_AFRICA_COORDINATORS = env("EMAIL_EAP_AFRICA_COORDINATORS")
597+
EMAIL_EAP_AMERICAS_COORDINATORS = env("EMAIL_EAP_AMERICAS_COORDINATORS")
598+
EMAIL_EAP_ASIA_PACIFIC_COORDINATORS = env("EMAIL_EAP_ASIA_PACIFIC_COORDINATORS")
599+
EMAIL_EAP_EUROPE_COORDINATORS = env("EMAIL_EAP_EUROPE_COORDINATORS")
600+
EMAIL_EAP_MENA_COORDINATORS = env("EMAIL_EAP_MENA_COORDINATORS")
601+
584602
DATA_UPLOAD_MAX_MEMORY_SIZE = 104857600 # default 2621440, 2.5MB -> 100MB
585603
# default 1000, was not enough for Mozambique Cyclone Idai data
586604
# second 2000, was not enouch for Global COVID Emergency

0 commit comments

Comments
 (0)