mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-06-12 07:24:48 +02:00
Review feedback: error tolerance, logging, etc.
This commit is contained in:
parent
a314b905d3
commit
f0ecae08c5
3 changed files with 43 additions and 23 deletions
|
@ -49,6 +49,9 @@ env_base_url = env.str("DJANGO_BASE_URL")
|
||||||
secret_login_key = b64decode(secret("DJANGO_SECRET_LOGIN_KEY", ""))
|
secret_login_key = b64decode(secret("DJANGO_SECRET_LOGIN_KEY", ""))
|
||||||
secret_key = secret("DJANGO_SECRET_KEY")
|
secret_key = secret("DJANGO_SECRET_KEY")
|
||||||
|
|
||||||
|
secret_aws_ses_key_id = secret("AWS_ACCESS_KEY_ID", None)
|
||||||
|
secret_aws_ses_key = secret("AWS_SECRET_ACCESS_KEY", None)
|
||||||
|
|
||||||
|
|
||||||
# region: Basic Django Config-----------------------------------------------###
|
# region: Basic Django Config-----------------------------------------------###
|
||||||
|
|
||||||
|
@ -214,8 +217,8 @@ AUTH_USER_MODEL = "registrar.User"
|
||||||
# region: Email-------------------------------------------------------------###
|
# region: Email-------------------------------------------------------------###
|
||||||
|
|
||||||
# Configuration for accessing AWS SES
|
# Configuration for accessing AWS SES
|
||||||
AWS_ACCESS_KEY_ID = env.str("AWS_ACCESS_KEY_ID", None)
|
AWS_ACCESS_KEY_ID = secret_aws_ses_key_id
|
||||||
AWS_SECRET_ACCESS_KEY = env.str("AWS_SECRET_ACCESS_KEY", None)
|
AWS_SECRET_ACCESS_KEY = secret_aws_ses_key
|
||||||
AWS_REGION = "us-gov-west-1"
|
AWS_REGION = "us-gov-west-1"
|
||||||
|
|
||||||
# email address to use for various automated correspondence
|
# email address to use for various automated correspondence
|
||||||
|
|
|
@ -8,7 +8,7 @@ from django.db import models
|
||||||
from django_fsm import FSMField, transition # type: ignore
|
from django_fsm import FSMField, transition # type: ignore
|
||||||
|
|
||||||
from .utility.time_stamped_model import TimeStampedModel
|
from .utility.time_stamped_model import TimeStampedModel
|
||||||
from ..utility.email import send_templated_email
|
from ..utility.email import send_templated_email, EmailSendingError
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
@ -478,11 +478,14 @@ class DomainApplication(TimeStampedModel):
|
||||||
if self.submitter is None or self.submitter.email is None:
|
if self.submitter is None or self.submitter.email is None:
|
||||||
logger.warn("Cannot send confirmation email, no submitter email address.")
|
logger.warn("Cannot send confirmation email, no submitter email address.")
|
||||||
return
|
return
|
||||||
send_templated_email(
|
try:
|
||||||
"emails/submission_confirmation.txt",
|
send_templated_email(
|
||||||
self.submitter.email,
|
"emails/submission_confirmation.txt",
|
||||||
context={"id": self.id, "domain_name": self.requested_domain.name},
|
self.submitter.email,
|
||||||
)
|
context={"id": self.id, "domain_name": self.requested_domain.name},
|
||||||
|
)
|
||||||
|
except EmailSendingError:
|
||||||
|
logger.warning("Failed to send confirmation email", exc_info=True)
|
||||||
|
|
||||||
@transition(field="status", source=STARTED, target=SUBMITTED)
|
@transition(field="status", source=STARTED, target=SUBMITTED)
|
||||||
def submit(self):
|
def submit(self):
|
||||||
|
|
|
@ -6,6 +6,13 @@ from django.conf import settings
|
||||||
from django.template.loader import get_template
|
from django.template.loader import get_template
|
||||||
|
|
||||||
|
|
||||||
|
class EmailSendingError(RuntimeError):
|
||||||
|
|
||||||
|
"""Local error for handling all failures when sending email."""
|
||||||
|
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
def send_templated_email(template_name: str, to_address: str, context={}):
|
def send_templated_email(template_name: str, to_address: str, context={}):
|
||||||
"""Send an email built from a template to one email address.
|
"""Send an email built from a template to one email address.
|
||||||
|
|
||||||
|
@ -16,19 +23,26 @@ def send_templated_email(template_name: str, to_address: str, context={}):
|
||||||
template = get_template(template_name)
|
template = get_template(template_name)
|
||||||
email_body = template.render(context=context)
|
email_body = template.render(context=context)
|
||||||
|
|
||||||
ses_client = boto3.client(
|
try:
|
||||||
"sesv2",
|
ses_client = boto3.client(
|
||||||
region_name=settings.AWS_REGION,
|
"sesv2",
|
||||||
aws_access_key_id=settings.AWS_ACCESS_KEY_ID,
|
region_name=settings.AWS_REGION,
|
||||||
aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY,
|
aws_access_key_id=settings.AWS_ACCESS_KEY_ID,
|
||||||
)
|
aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY,
|
||||||
ses_client.send_email(
|
)
|
||||||
FromEmailAddress=settings.DEFAULT_FROM_EMAIL,
|
except Exception as exc:
|
||||||
Destination={"ToAddresses": [to_address]},
|
raise EmailSendingError("Could not access the SES client.") from exc
|
||||||
Content={
|
|
||||||
"Simple": {
|
try:
|
||||||
"Subject": {"Data": "Thank you for applying for a .gov domain"},
|
ses_client.send_email(
|
||||||
"Body": {"Text": {"Data": email_body}},
|
FromEmailAddress=settings.DEFAULT_FROM_EMAIL,
|
||||||
|
Destination={"ToAddresses": [to_address]},
|
||||||
|
Content={
|
||||||
|
"Simple": {
|
||||||
|
"Subject": {"Data": "Thank you for applying for a .gov domain"},
|
||||||
|
"Body": {"Text": {"Data": email_body}},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
)
|
||||||
)
|
except Exception as exc:
|
||||||
|
raise EmailSendingError("Could not send SES email.") from exc
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue