Skip to content

Commit f297d56

Browse files
claude[bot]claudemarcoacierno
committed
Abstract body_file/body check pattern with properties
Add html_body_content and text_body_content properties to SentEmail model to centralize the logic for checking body_file vs body fallback. This eliminates code duplication across tasks.py, admin/views.py, and tests. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]> Co-Authored-By: Marco Acierno <[email protected]>
1 parent df2a1e4 commit f297d56

File tree

4 files changed

+19
-18
lines changed

4 files changed

+19
-18
lines changed

backend/notifications/admin/views.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,4 @@ def view_email_template(request, object_id):
1515

1616
def view_sent_email(request, object_id):
1717
sent_email = cast(SentEmail, SentEmail.objects.get(id=object_id))
18-
return HttpResponse(
19-
sent_email.body_file.read().decode("utf-8")
20-
if sent_email.body_file
21-
else sent_email.body
22-
)
18+
return HttpResponse(sent_email.html_body_content)

backend/notifications/models.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,18 @@ def is_delivered(self):
384384
def is_opened(self):
385385
return self.events.filter(event=SentEmailEvent.Event.opened).exists()
386386

387+
@property
388+
def html_body_content(self):
389+
if self.body_file:
390+
return self.body_file.read().decode("utf-8")
391+
return self.body
392+
393+
@property
394+
def text_body_content(self):
395+
if self.text_body_file:
396+
return self.text_body_file.read().decode("utf-8")
397+
return self.text_body
398+
387399
def mark_as_sent(self, message_id: str):
388400
self.status = self.Status.sent
389401
self.sent_at = timezone.now()

backend/notifications/tasks.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,8 @@ def send_pending_email(self, sent_email_id: int):
6363
def send_email(sent_email, email_backend_connection):
6464
logger.info(f"Sending sent_email_id={sent_email.id}")
6565

66-
if sent_email.body_file:
67-
html_body = sent_email.body_file.read().decode("utf-8")
68-
else:
69-
html_body = sent_email.body
70-
71-
if sent_email.text_body_file:
72-
text_body = sent_email.text_body_file.read().decode("utf-8")
73-
else:
74-
text_body = sent_email.text_body
66+
html_body = sent_email.html_body_content
67+
text_body = sent_email.text_body_content
7568

7669
email_message = EmailMultiAlternatives(
7770
subject=sent_email.subject,

backend/notifications/tests/test_models.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def test_send_email_template_to_recipient_email(
7474
assert sent_email.recipient_email == "[email protected]"
7575

7676
assert sent_email.subject == "Subject abc"
77-
assert "Body abc" in sent_email.body_file.read().decode("utf-8")
77+
assert "Body abc" in sent_email.html_body_content
7878
assert sent_email.preview_text == "Preview abc"
7979
assert sent_email.reply_to == "[email protected]"
8080

@@ -102,8 +102,8 @@ def test_send_email_template_to_recipient_user():
102102
assert sent_email.recipient_email == user.email
103103

104104
assert sent_email.subject == "Subject abc"
105-
assert "Body abc" in sent_email.body_file.read().decode("utf-8")
106-
assert "Body abc" in sent_email.text_body_file.read().decode("utf-8")
105+
assert "Body abc" in sent_email.html_body_content
106+
assert "Body abc" in sent_email.text_body_content
107107
assert sent_email.preview_text == "Preview abc"
108108
assert sent_email.reply_to == "[email protected]"
109109

@@ -138,7 +138,7 @@ def test_send_system_template_email(settings):
138138
assert sent_email.from_email == "[email protected]"
139139

140140
assert sent_email.subject == "Subject abc"
141-
assert "Body abc" in sent_email.body_file.read().decode("utf-8")
141+
assert "Body abc" in sent_email.html_body_content
142142
assert sent_email.preview_text == "Preview abc"
143143
assert sent_email.reply_to == "[email protected]"
144144

0 commit comments

Comments
 (0)