Skip to content

Commit

Permalink
update BaseBatchRelationshipEmails to allow sending multiple emails p…
Browse files Browse the repository at this point in the history
…er user
  • Loading branch information
jontsai committed Oct 18, 2017
1 parent 15e6669 commit 851d556
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 38 deletions.
7 changes: 4 additions & 3 deletions apps/accounts/emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ def get_recipients(self):
)
return users

def send_email(self, recipient):
"""Sends an activation reminder email to `recipient`
def send_email(self, recipient, **kwargs):
"""Sends an activation reminder email to `recipient`, a Django User
"""
recipient.profile.send_activation_reminder_email()
user = recipient
user.profile.send_activation_reminder_email()

def activation_email(user_email, use_https=False, domain=None, template=None, subject=None, sender=None):
"""Sends an activation/confirmation email for user to confirm email address
Expand Down
80 changes: 45 additions & 35 deletions emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,71 +38,81 @@ def __init__(self, cooldown_class=None, template=None):
else:
raise TemplateDoesNotExist('Unspecified template')

def get_users(self):
users = self.get_recipients()
return users

def get_recipients(self):
"""Returns a list or QuerySet of User objects
Should be overridden
"""
users = self.get_users()
users = super(BaseTask, self).get_users()
return users

def get_subject(self, recipient):
def execute(self, user):
"""Send out emails for `user`
One `user` may receive one or many emails
"""
recipient = user
email_batches_data = self.get_recipient_email_batches_data(recipient)
for email_batch_data in email_batches_data:
self.send_email(recipient, **email_batch_data)

def get_recipient_email_batches_data(self, recipient):
"""Gets data about each email for the `recipient`
Use cases. A recipient can receive multiple emails if:
- recipient has multiple "sub-accounts"
Returns a list of dictionaries
"""
return [{}]

def send_email(self, recipient, **kwargs):
"""Workhorse function called by `self.send_emails` for
sending to one `recipient`
Can be overridden
"""
email_params = self._craft_email_params(recipient, **kwargs)
context = email_params.get('context', {})
send_email(
template=email_params.get('template'),
subject=context.get('subject'),
to=email_params.get('recipients', []),
context=context
)

def get_subject(self, recipient, **kwargs):
"""Returns the email subject line for `recipient`
Should be overridden
"""
subject = ''
return subject

def get_email_context(self, recipient):
def get_email_context(self, recipient, **kwargs):
"""Returns a dictionary for the email context
"""
context = {}
return context

def _craft_email_params(self, recipient):
def _craft_email_params(self, recipient, **kwargs):
"""Build the email params for rendering this BatchRelationshipEmail
"""
recipients = [recipient.email,]
subject = self.get_subject(recipient)
subject = self.get_subject(recipient, **kwargs)

context = {
'user' : recipient,
'subject' : subject,
}
context.update(self.get_email_context(recipient))
context.update(self.get_email_context(recipient, **kwargs))
email_params = {
'template' : self.template,
'recipients' : recipients,
'context' : context,
}
return email_params

def send_email(self, recipient):
"""Workhorse function called by `self.send_emails` for
sending to one `recipient`
Can be overridden
"""
email_params = self._craft_email_params(recipient)
context = email_params.get('context', {})
send_email(
template=email_params.get('template'),
subject=context.get('subject'),
to=email_params.get('recipients', []),
context=context
)

def send_emails(self):
"""Send the batch of emails
"""
recipients = self.get_recipients()
for recipient in recipients:
if self.has_cooldown(recipient):
# cooldown has not elapsed yet, don't send mail too frequently
pass
else:
self.send_email(recipient)
# cache right after we send, not before
# since each send operation costs a non-zero overhead
self.reset_cooldown(recipient)

0 comments on commit 851d556

Please sign in to comment.