Email logic (needs cleanup)

This commit is contained in:
zandercymatics 2024-07-02 12:56:26 -06:00
parent 34f6b03078
commit a8fa24411e
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7
4 changed files with 27 additions and 16 deletions

View file

@ -1703,12 +1703,21 @@ class DomainRequestAdmin(ListHeaderAdmin, ImportExportModelAdmin):
return super().save_model(request, obj, form, change)
# == Handle non-status changes == #
if obj.action_needed_reason and not obj.action_needed_reason_email:
self._handle_action_needed_reason_email(obj)
should_save = True
# Get the original domain request from the database.
original_obj = models.DomainRequest.objects.get(pk=obj.pk)
if obj.action_needed_reason:
text = self._get_action_needed_reason_default_email_text(obj, obj.action_needed_reason)
body_text = text.get("email_body_text")
if body_text:
body_text.strip().lstrip("\n")
is_default_email = body_text == obj.action_needed_reason_email
reason_changed = obj.action_needed_reason != original_obj.action_needed_reason
if is_default_email and reason_changed:
obj.action_needed_reason_email = body_text
should_save = True
if obj.status == original_obj.status:
# If the status hasn't changed, let the base function take care of it
return super().save_model(request, obj, form, change)
@ -1721,10 +1730,6 @@ class DomainRequestAdmin(ListHeaderAdmin, ImportExportModelAdmin):
if should_save:
return super().save_model(request, obj, form, change)
def _handle_action_needed_reason_email(self, obj):
text = self._get_action_needed_reason_default_email_text(obj, obj.action_needed_reason)
obj.action_needed_reason_email = text.get("email_body_text")
def _handle_status_change(self, request, obj, original_obj):
"""
Checks for various conditions when a status change is triggered.

View file

@ -619,7 +619,7 @@ class DomainRequest(TimeStampedModel):
# We don't send emails out in state "other"
if self.action_needed_reason != self.ActionNeededReasons.OTHER:
_email_content = self.action_needed_reason_email
self._send_action_needed_reason_email(custom_email_content=_email_content)
self._send_action_needed_reason_email(email_content=_email_content)
def sync_yes_no_form_fields(self):
"""Some yes/no forms use a db field to track whether it was checked or not.
@ -857,10 +857,10 @@ class DomainRequest(TimeStampedModel):
# Send out an email if an action needed reason exists
if self.action_needed_reason and self.action_needed_reason != self.ActionNeededReasons.OTHER:
custom_email_content = self.action_needed_reason_email
self._send_action_needed_reason_email(send_email, custom_email_content)
email_content = self.action_needed_reason_email
self._send_action_needed_reason_email(send_email, email_content)
def _send_action_needed_reason_email(self, send_email=True, custom_email_content=None):
def _send_action_needed_reason_email(self, send_email=True, email_content=None):
"""Sends out an automatic email for each valid action needed reason provided"""
# Store the filenames of the template and template subject
@ -871,7 +871,8 @@ class DomainRequest(TimeStampedModel):
# If these hashes differ, then that means that we're sending custom content.
default_email_hash = self._get_action_needed_reason_email_hash()
current_email_hash = convert_string_to_sha256_hash(self.action_needed_reason_email)
if default_email_hash != current_email_hash:
if self.action_needed_reason_email and default_email_hash != current_email_hash:
print(f"sending custom email for: {current_email_hash}")
email_template_name = "custom_email.txt"
# Check for the "type" of action needed reason.
@ -902,7 +903,7 @@ class DomainRequest(TimeStampedModel):
email_template_subject=f"emails/action_needed_reasons/{email_template_subject_name}",
send_email=send_email,
bcc_address=bcc_address,
custom_email_content=custom_email_content,
custom_email_content=email_content,
wrap_email=True,
)
@ -919,7 +920,8 @@ class DomainRequest(TimeStampedModel):
recipient = self.creator if flag_is_active(None, "profile_feature") else self.submitter
# Return the content of the rendered views
context = {"domain_request": self, "recipient": recipient}
body_text = template.render(context=context)
body_text = template.render(context=context).strip().lstrip("\n")
print(f"body: {body_text}")
return convert_string_to_sha256_hash(body_text)
@transition(

View file

@ -324,5 +324,7 @@ def convert_queryset_to_dict(queryset, is_model=True, key="id"):
return request_dict
def convert_string_to_sha256_hash(string_to_convert):
def convert_string_to_sha256_hash(string_to_convert: str):
if not string_to_convert:
return None
return hashlib.sha256(string_to_convert.encode('utf-8')).hexdigest()

View file

@ -45,6 +45,8 @@ def send_templated_email(
template = get_template(template_name)
email_body = template.render(context=context)
if email_body:
email_body.strip().lstrip("\n")
subject_template = get_template(subject_template_name)
subject = subject_template.render(context=context)