Skip to content

Commit

Permalink
Change the code around rendering mails, maybe yet without a good reas…
Browse files Browse the repository at this point in the history
…on to
  • Loading branch information
matthiask committed Nov 7, 2018
1 parent 1a76937 commit 75be315
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 17 deletions.
4 changes: 2 additions & 2 deletions spark/spark_mails/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ def rendered(self, instance):
try:
return format_html(
'<div style="white-space:pre-wrap; max-width:40rem">'
"<code>{}\n\n{}</code></div>",
*instance.render(spark_mails_context(instance)),
"<code>{subject}\n\n{body}</code></div>",
**instance.render(spark_mails_context(instance)),
)
except Exception:
return format_html('<div style="color:red">INVALID</div>')
Expand Down
15 changes: 5 additions & 10 deletions spark/spark_mails/api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging

from django.core.mail import EmailMessage
from django.core.mail import EmailMultiAlternatives

from spark.spark_mails.models import Mail

Expand All @@ -15,20 +15,15 @@ def process_mail_events(iterable, *, defaults=None, fail_silently=False):

def mails_from_events(iterable, *, defaults=None):
mails = Mail.objects.as_mails()
defaults = {} if defaults is None else defaults

for e in iterable:
if "spark_mail" not in e["context"]:
continue
kwargs = dict(defaults) if defaults else {}
kwargs.update(e["context"]["spark_mail"])
kwargs = {**defaults, **e["context"]["spark_mail"]}
if e["group"] in mails:
try:
subject, body = mails[e["group"]].render(e["context"])
kwargs = mails[e["group"]].render(e["context"], **kwargs)
except Exception:
logger.exception("Error while rendering mail subject and body")
else:
if subject:
kwargs["subject"] = subject
if body:
kwargs["body"] = body
yield EmailMessage(**kwargs)
yield EmailMultiAlternatives(**kwargs)
7 changes: 5 additions & 2 deletions spark/spark_mails/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def clean_fields(self, exclude=None):
def template(self):
return Template(self.template_source)

def render(self, context):
def render(self, context, **kwargs):
lines = iter(
line.rstrip()
for line in self.template.render(Context(context)).splitlines()
Expand All @@ -54,4 +54,7 @@ def render(self, context):
break
except StopIteration: # if lines is empty
pass
return subject, "\n".join(lines).strip("\n")
body = "\n".join(lines).strip("\n")
kwargs["subject"] = subject or kwargs.get("subject", "")
kwargs["body"] = body or kwargs.get("body", "")
return kwargs
6 changes: 3 additions & 3 deletions tests/testapp/test_mails.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ def test_mails(self):

stuff = Stuff.objects.create(key="asdf")

subject, body = m.render(
kwargs = m.render(
{"user": user, "key": stuff.key, "key_length": len(stuff.key)}
)

self.assertEqual(subject, "Dear test")
self.assertEqual(body, "This is the key: asdf (4)\n\nBest regards")
self.assertEqual(kwargs["subject"], "Dear test")
self.assertEqual(kwargs["body"], "This is the key: asdf (4)\n\nBest regards")

events = [
{
Expand Down

0 comments on commit 75be315

Please sign in to comment.