Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to save context to the database even if rendering immediately #369

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
14 changes: 9 additions & 5 deletions post_office/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
def create(sender, recipients=None, cc=None, bcc=None, subject='', message='',
html_message='', context=None, scheduled_time=None, expires_at=None, headers=None,
template=None, priority=None, render_on_delivery=False, commit=True,
backend=''):
backend='', save_context=False):
"""
Creates an email from supplied keyword arguments. If template is
specified, email subject and content will be rendered during delivery.
Expand Down Expand Up @@ -73,6 +73,9 @@ def create(sender, recipients=None, cc=None, bcc=None, subject='', message='',
subject = Template(subject).render(_context)
message = Template(message).render(_context)
html_message = Template(html_message).render(_context)

if not save_context:
context = None

email = Email(
from_email=sender,
Expand All @@ -86,7 +89,8 @@ def create(sender, recipients=None, cc=None, bcc=None, subject='', message='',
expires_at=expires_at,
message_id=message_id,
headers=headers, priority=priority, status=status,
backend_alias=backend
backend_alias=backend,
context=context
)

if commit:
Expand All @@ -99,7 +103,7 @@ def send(recipients=None, sender=None, template=None, context=None, subject='',
message='', html_message='', scheduled_time=None, expires_at=None, headers=None,
priority=None, attachments=None, render_on_delivery=False,
log_level=None, commit=True, cc=None, bcc=None, language='',
backend=''):
backend='', save_context=False):
try:
recipients = parse_emails(recipients)
except ValidationError as e:
Expand Down Expand Up @@ -151,7 +155,7 @@ def send(recipients=None, sender=None, template=None, context=None, subject='',

email = create(sender, recipients, cc, bcc, subject, message, html_message,
context, scheduled_time, expires_at, headers, template, priority,
render_on_delivery, commit=commit, backend=backend)
render_on_delivery, commit=commit, backend=backend, save_context=save_context)

if attachments:
attachments = create_attachments(attachments)
Expand Down Expand Up @@ -359,4 +363,4 @@ def send_queued_mail_until_done(lockfile=default_lockfile, processes=1, log_leve
if not get_queued().exists():
break
except FileLocked:
logger.info('Failed to acquire lock, terminating now.')
logger.info('Failed to acquire lock, terminating now.')