Fix linting. Revert to original email sending logic

This commit is contained in:
Erin Song 2024-09-26 14:51:50 -07:00
parent b0fe698af2
commit d71069606b
No known key found for this signature in database
2 changed files with 40 additions and 17 deletions

View file

@ -31,6 +31,7 @@ class OutsideOrgMemberError(ValueError):
pass pass
class ActionNotAllowed(Exception): class ActionNotAllowed(Exception):
"""User accessed an action that is not """User accessed an action that is not
allowed by the current state""" allowed by the current state"""

View file

@ -35,9 +35,9 @@ from registrar.utility.errors import (
NameserverErrorCodes as nsErrorCodes, NameserverErrorCodes as nsErrorCodes,
DsDataError, DsDataError,
DsDataErrorCodes, DsDataErrorCodes,
OutsideOrgMemberError,
SecurityEmailError, SecurityEmailError,
SecurityEmailErrorCodes, SecurityEmailErrorCodes,
OutsideOrgMemberError,
) )
from registrar.models.utility.contact_error import ContactError from registrar.models.utility.contact_error import ContactError
from registrar.views.utility.permission_views import UserDomainRolePermissionDeleteView from registrar.views.utility.permission_views import UserDomainRolePermissionDeleteView
@ -859,6 +859,10 @@ class DomainAddUserView(DomainFormBaseView):
messages.success(self.request, f"{email} has been invited to this domain.") messages.success(self.request, f"{email} has been invited to this domain.")
except Exception: except Exception:
logger.error("An error occured") logger.error("An error occured")
except OutsideOrgMemberError:
logger.error(
"Could not send email. Can not invite member of a .gov organization to a different organization."
)
except EmailSendingError as exc: except EmailSendingError as exc:
logger.warn( logger.warn(
"Could not sent email invitation to %s for domain %s", "Could not sent email invitation to %s for domain %s",
@ -868,6 +872,29 @@ class DomainAddUserView(DomainFormBaseView):
) )
raise EmailSendingError("Could not send email invitation.") from exc raise EmailSendingError("Could not send email invitation.") from exc
try:
send_templated_email(
"emails/domain_invitation.txt",
"emails/domain_invitation_subject.txt",
to_address=email,
context={
"domain_url": self._domain_abs_url(),
"domain": self.object,
"requestor_email": requestor_email,
},
)
except EmailSendingError as exc:
logger.warn(
"Could not sent email invitation to %s for domain %s",
email,
self.object,
exc_info=True,
)
raise EmailSendingError("Could not send email invitation.") from exc
else:
if add_success:
messages.success(self.request, f"{email} has been invited to this domain.")
def _make_invitation(self, email_address: str, requestor: User): def _make_invitation(self, email_address: str, requestor: User):
"""Make a Domain invitation for this email and redirect with a message.""" """Make a Domain invitation for this email and redirect with a message."""
try: try:
@ -896,14 +923,6 @@ class DomainAddUserView(DomainFormBaseView):
self._send_domain_invitation_email( self._send_domain_invitation_email(
requested_email, requestor, requested_user=requested_user, add_success=False requested_email, requestor, requested_user=requested_user, add_success=False
) )
UserDomainRole.objects.create(
user=requested_user,
domain=self.object,
role=UserDomainRole.Roles.MANAGER,
)
messages.success(self.request, f"Added user {requested_email}.")
except EmailSendingError: except EmailSendingError:
logger.warn( logger.warn(
"Could not send email invitation (EmailSendingError)", "Could not send email invitation (EmailSendingError)",
@ -911,12 +930,6 @@ class DomainAddUserView(DomainFormBaseView):
exc_info=True, exc_info=True,
) )
messages.warning(self.request, "Could not send email invitation.") messages.warning(self.request, "Could not send email invitation.")
except OutsideOrgMemberError:
logger.warn(
"Could not send email invitation to a user in a different org.",
self.object,
exc_info=True,
)
except Exception: except Exception:
logger.warn( logger.warn(
"Could not send email invitation (Other Exception)", "Could not send email invitation (Other Exception)",
@ -924,8 +937,17 @@ class DomainAddUserView(DomainFormBaseView):
exc_info=True, exc_info=True,
) )
messages.warning(self.request, "Could not send email invitation.") messages.warning(self.request, "Could not send email invitation.")
except IntegrityError: else:
messages.warning(self.request, f"{requested_email} is already a manager for this domain") try:
UserDomainRole.objects.create(
user=requested_user,
domain=self.object,
role=UserDomainRole.Roles.MANAGER,
)
except IntegrityError:
messages.warning(self.request, f"{requested_email} is already a manager for this domain")
else:
messages.success(self.request, f"Added user {requested_email}.")
return redirect(self.get_success_url()) return redirect(self.get_success_url())