add outgoingemail model, add mailhelper function and worker

This commit is contained in:
Stephan Telling 2017-04-23 22:04:58 +02:00
parent 698698a96b
commit 1f4df68304
No known key found for this signature in database
GPG key ID: D4892289F36ADA9B
5 changed files with 97 additions and 11 deletions

View file

@ -1,4 +1,4 @@
from utils.email import _send_email
from utils.email import add_outgoing_email
import logging
logger = logging.getLogger("bornhack.%s" % __name__)
@ -9,7 +9,7 @@ def send_add_membership_email(membership):
'camp': membership.team.camp.title
}
return _send_email(
return add_outgoing_email(
text_template='emails/add_membership_email.txt',
html_template='emails/add_membership_email.html',
recipient=membership.user.email,
@ -31,7 +31,7 @@ def send_remove_membership_email(membership):
text_template = 'emails/unapproved_membership_email.txt',
html_template = 'emails/unapproved_membership_email.html'
return _send_email(
return add_outgoing_email(
text_template=text_template,
html_template=html_template,
recipient=membership.user.email,
@ -46,10 +46,12 @@ def send_new_membership_email(membership):
'camp': membership.team.camp.title
}
return _send_email(
return add_outgoing_email(
text_template='emails/new_membership_email.txt',
html_template='emails/new_membership_email.html',
recipient=[resp.email for resp in membership.team.responsible],
recipients=', '.join(
[resp.email for resp in membership.team.responsible]
),
formatdict=formatdict,
subject='New membership request for {} at {}'.format(
membership.team.name,

8
src/utils/admin.py Normal file
View file

@ -0,0 +1,8 @@
from django.contrib import admin
from .models import OutgoingEmail
@admin.register(OutgoingEmail)
class OutgoingEmailAdmin(admin.ModelAdmin):
pass

View file

@ -1,6 +1,8 @@
from django.core.mail import EmailMultiAlternatives
from django.conf import settings
from django.template.loader import render_to_string
from django.core.validators import validate_email
from .models import OutgoingEmail
import logging
logger = logging.getLogger("bornhack.%s" % __name__)
@ -8,12 +10,11 @@ logger = logging.getLogger("bornhack.%s" % __name__)
def _send_email(
text_template,
recipient,
formatdict,
subject,
html_template=None,
html_template='',
sender='BornHack <info@bornhack.dk>',
attachment=None,
attachment_filename=None
attachment='',
attachment_filename=''
):
if not isinstance(recipient, list):
recipient = [recipient]
@ -22,7 +23,7 @@ def _send_email(
# put the basic email together
msg = EmailMultiAlternatives(
subject,
render_to_string(text_template, formatdict),
text_template,
sender,
recipient,
[settings.ARCHIVE_EMAIL]
@ -31,7 +32,7 @@ def _send_email(
# is there a html version of this email?
if html_template:
msg.attach_alternative(
render_to_string(html_template, formatdict),
html_template,
'text/html'
)
@ -47,3 +48,39 @@ def _send_email(
msg.send()
return True
def add_outgoing_email(
text_template,
recipients,
formatdict,
subject,
html_template='',
sender='BornHack <info@bornhack.dk>',
attachment='',
attachment_filename=''
):
""" adds an email to the outgoing queue
recipients is either just a str email or a str commaseperated emails
"""
text_template = render_to_string(text_template, formatdict)
if html_template:
html_template = render_to_string(html_template, formatdict)
if ',' in recipients:
for recipient in recipients.split(','):
validate_email(recipient.strip())
else:
validate_email(recipients)
OutgoingEmail.objects.create(
text_template=text_template,
html_template=html_template,
subject=subject,
sender=sender,
recipient=recipients,
attachment=attachment,
attachment_filename=attachment_filename
)
return True

View file

@ -68,3 +68,14 @@ class CampRelatedModel(CreatedUpdatedModel):
raise ValidationError('This camp is in read only mode.')
super().delete(**kwargs)
class OutgoingEmail(CreatedUpdatedModel):
subject = models.CharField(max_length=500)
text_template = models.TextField()
html_template = models.TextField(blank=True)
recipient = models.CharField(max_length=500)
sender = models.CharField(max_length=500)
attachment = models.CharField(max_length=500, blank=True)
attachment_filename = models.CharField(max_length=500, blank=True)
processed = models.BooleanField(default=False)

View file

@ -0,0 +1,28 @@
from .models import OutgoingEmail
from .email import _send_email
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger('bornhack.%s' % __name__)
def do_work():
"""
The outgoing email worker sends emails added to the OutgoingEmail
queue.
"""
not_processed_email = OutgoingEmail.objects.filter(processed=False)
for email in not_processed_email:
if ',' in email.recipient:
recipient = email.recipient.split(',')
else:
recipient = [email.recipient]
_send_email(
text_template=email.text_template,
recipient=recipient,
subject=email.subject,
html_template=email.html_template,
attachment=email.attachment,
attachment_filename=email.attachment_filename
)