Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backend/notifications/admin/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ def view_email_template(request, object_id):

def view_sent_email(request, object_id):
sent_email = cast(SentEmail, SentEmail.objects.get(id=object_id))
return HttpResponse(sent_email.body)
return HttpResponse(sent_email.html_body_content)
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 5.1.4 on 2025-03-09 22:29

import notifications.models
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('notifications', '0021_alter_emailtemplate_identifier'),
]

operations = [
migrations.AddField(
model_name='sentemail',
name='body_file',
field=models.FileField(blank=True, null=True, upload_to=notifications.models.sent_email_body_upload_to, verbose_name='body file'),
),
migrations.AddField(
model_name='sentemail',
name='text_body_file',
field=models.FileField(blank=True, null=True, upload_to=notifications.models.sent_email_body_upload_to, verbose_name='text body file'),
),
]
35 changes: 33 additions & 2 deletions backend/notifications/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from model_utils.models import TimeStampedModel
from django.utils.translation import gettext_lazy as _
from django.conf import settings
from django.core.files.base import ContentFile

BASE_PLACEHOLDERS = ["conference"]

Expand Down Expand Up @@ -253,12 +254,20 @@ def send_email(
placeholders=placeholders,
subject=processed_email_template.subject,
preview_text=processed_email_template.preview_text,
body=processed_email_template.html_body,
text_body=processed_email_template.text_body,
body="",
text_body="",
reply_to=self.reply_to,
cc_addresses=self.cc_addresses,
bcc_addresses=self.bcc_addresses,
)
sent_email.body_file.save(
"body.html",
ContentFile(processed_email_template.html_body.encode("utf-8")),
)
sent_email.text_body_file.save(
"text_body.txt",
ContentFile(processed_email_template.text_body.encode("utf-8")),
)

transaction.on_commit(lambda: send_pending_email.delay(sent_email.id))

Expand Down Expand Up @@ -290,6 +299,10 @@ class Meta:
]


def sent_email_body_upload_to(instance, filename):
return f"sent_emails/{instance.id}/{filename}"


class SentEmail(TimeStampedModel):
class Status(models.TextChoices):
pending = "pending", _("Pending")
Expand Down Expand Up @@ -333,7 +346,13 @@ class Status(models.TextChoices):

subject = models.TextField(_("subject"))
body = models.TextField(_("body"))
body_file = models.FileField(
_("body file"), upload_to=sent_email_body_upload_to, blank=True, null=True
)
text_body = models.TextField(_("text body"))
text_body_file = models.FileField(
_("text body file"), upload_to=sent_email_body_upload_to, blank=True, null=True
)
preview_text = models.TextField(_("preview text"), blank=True)

from_email = models.EmailField(_("from email"))
Expand Down Expand Up @@ -365,6 +384,18 @@ def is_delivered(self):
def is_opened(self):
return self.events.filter(event=SentEmailEvent.Event.opened).exists()

@property
def html_body_content(self):
if self.body_file:
return self.body_file.read().decode("utf-8")
return self.body

@property
def text_body_content(self):
if self.text_body_file:
return self.text_body_file.read().decode("utf-8")
return self.text_body

def mark_as_sent(self, message_id: str):
self.status = self.Status.sent
self.sent_at = timezone.now()
Expand Down
7 changes: 5 additions & 2 deletions backend/notifications/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,19 @@ def send_pending_email(self, sent_email_id: int):
def send_email(sent_email, email_backend_connection):
logger.info(f"Sending sent_email_id={sent_email.id}")

html_body = sent_email.html_body_content
text_body = sent_email.text_body_content

email_message = EmailMultiAlternatives(
subject=sent_email.subject,
body=sent_email.text_body,
body=text_body,
from_email=sent_email.from_email,
to=[sent_email.recipient_email],
cc=sent_email.cc_addresses,
bcc=sent_email.bcc_addresses,
reply_to=[sent_email.reply_to],
connection=email_backend_connection,
)
email_message.attach_alternative(sent_email.body, "text/html")
email_message.attach_alternative(html_body, "text/html")
email_message.send()
return email_message.extra_headers.get("message_id", f"local-{uuid4()}")
7 changes: 4 additions & 3 deletions backend/notifications/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def test_send_email_template_to_recipient_email(
assert sent_email.recipient_email == "[email protected]"

assert sent_email.subject == "Subject abc"
assert "Body abc" in sent_email.body
assert "Body abc" in sent_email.html_body_content
assert sent_email.preview_text == "Preview abc"
assert sent_email.reply_to == "[email protected]"

Expand Down Expand Up @@ -102,7 +102,8 @@ def test_send_email_template_to_recipient_user():
assert sent_email.recipient_email == user.email

assert sent_email.subject == "Subject abc"
assert "Body abc" in sent_email.body
assert "Body abc" in sent_email.html_body_content
assert "Body abc" in sent_email.text_body_content
assert sent_email.preview_text == "Preview abc"
assert sent_email.reply_to == "[email protected]"

Expand Down Expand Up @@ -137,7 +138,7 @@ def test_send_system_template_email(settings):
assert sent_email.from_email == "[email protected]"

assert sent_email.subject == "Subject abc"
assert "Body abc" in sent_email.body
assert "Body abc" in sent_email.html_body_content
assert sent_email.preview_text == "Preview abc"
assert sent_email.reply_to == "[email protected]"

Expand Down
5 changes: 5 additions & 0 deletions backend/pycon/storages.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
from django.core.files.storage.memory import InMemoryStorage
from django.core.files.storage import FileSystemStorage
from django.urls import reverse
from django.core.files.storage import storages


def private_storage_getter():
return storages["private"]


@dataclass
Expand Down
6 changes: 1 addition & 5 deletions backend/visa/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from functools import cached_property
from django.db import transaction

from pycon.storages import private_storage_getter
from submissions.models import Submission
from users.models import User
from grants.models import Grant
Expand All @@ -10,7 +11,6 @@
from django.db import models
from django.db.models import UniqueConstraint, Q
from django.utils.translation import gettext_lazy as _
from django.core.files.storage import storages


class InvitationLetterRequestStatus(models.TextChoices):
Expand All @@ -31,10 +31,6 @@ def invitation_letter_upload_to(instance, filename):
return f"invitation_letters/{instance.conference.code}/{instance.id}/{filename}"


def private_storage_getter():
return storages["private"]


class InvitationLetterRequest(TimeStampedModel):
objects = InvitationLetterRequestQuerySet().as_manager()

Expand Down
Loading