Commit 43b2f24c authored by jocelyn's avatar jocelyn
Browse files

Refactor email sending

To pave the way for more email sending :-)
parent 7a8ca5d2
import datetime
import pytz
from django.conf import settings
from django.core.mail import send_mail
from django.utils.translation import activate
def _get_contrib_context(contrib, mgmt_token, request=None):
""" Sends an email related to a contrib
"""
activate(settings.LANGUAGE_CODE)
if request:
permalink = contrib.get_absolute_url(request)
else:
permalink = None
ndays = (contrib.expiration_date - datetime.datetime.now(pytz.utc)).days
return {
'contrib': contrib,
'ndays': ndays,
'isp': settings.ISP,
'management_link': contrib.make_management_url(mgmt_token),
'mgmt_token': mgmt_token,
'permalink': permalink,
}
def send_contributor_email(
contrib, subject_template, body_template, mgmt_token, request=None):
context = _get_contrib_context(contrib, mgmt_token, request)
send_mail(
subject_template.render(context),
body_template.render(context),
settings.DEFAULT_FROM_EMAIL,
[contrib.email],
)
return context
def send_moderator_emails(
contrib, subject_template, body_template, mgmt_token, request):
context = _get_contrib_context(contrib, mgmt_token, request)
if len(settings.NOTIFICATION_EMAILS) > 0:
send_mail(
subject_template.render(context),
body_template.render(context),
settings.DEFAULT_FROM_EMAIL,
settings.NOTIFICATION_EMAILS,
)
return context
......@@ -16,6 +16,7 @@ from django.template.loader import get_template
from django.utils.translation import activate
from ...models import Contrib
from ... emails import send_contributor_email
from ...tokens import ContribTokenManager
......@@ -38,7 +39,7 @@ class Command(BaseCommand):
self._warn_no_email(contrib, ndays)
else:
if not dry_run:
self.send_expiration_reminder(contrib, ndays)
self.send_expiration_reminder(contrib)
self._log_sent_email(contrib, ndays)
def _warn_no_email(self, contrib, ndays):
......@@ -52,26 +53,11 @@ class Command(BaseCommand):
"Sent reminder email to for {} expiration in {} days".format(
contrib, ndays))
def send_expiration_reminder(self, contrib, ndays):
def send_expiration_reminder(self, contrib):
subject = get_template(
'contribmap/mails/expiration_reminder.subject')
body = get_template(
'contribmap/mails/expiration_reminder.txt')
mgmt_token = ContribTokenManager().mk_token(contrib)
context = {
'contrib': contrib,
'ndays': ndays,
'isp': settings.ISP,
'management_link': contrib.make_management_url(mgmt_token),
}
# Without that, getting month & day names in english
activate(settings.LANGUAGE_CODE)
send_mail(
subject.render(context),
body.render(context),
settings.DEFAULT_FROM_EMAIL,
[contrib.email],
)
send_contributor_email(contrib, subject, body, mgmt_token)
......@@ -18,6 +18,7 @@ from .forms import ManageActionForm, PublicContribForm
from .models import Contrib
from .decorators import prevent_robots
from .tokens import ContribTokenManager, URLTokenManager, TokenError
from .emails import send_contributor_email, send_moderator_emails
@prevent_robots()
......@@ -30,26 +31,14 @@ def add_contrib(request):
if form.is_valid():
contrib = form.save()
mgmt_token = ContribTokenManager().mk_token(contrib)
context = {
'contrib': contrib,
'management_link': contrib.make_management_url(mgmt_token),
'permalink': contrib.get_absolute_url(request),
'isp': settings.ISP,
}
# Send notification email to site administrator
if len(settings.NOTIFICATION_EMAILS) > 0:
admin_subject = get_template(
'contribmap/mails/new_contrib_moderator_notice.subject')
admin_body = get_template(
admin_subject = get_template(
'contribmap/mails/new_contrib_moderator_notice.subject')
admin_body = get_template(
'contribmap/mails/new_contrib_moderator_notice.txt')
send_mail(
admin_subject.render(context),
admin_body.render(context),
settings.DEFAULT_FROM_EMAIL,
settings.NOTIFICATION_EMAILS,
)
send_moderator_emails(
contrib, admin_subject, admin_body, mgmt_token, request)
# Notification email to the author
if contrib.email:
......@@ -58,12 +47,9 @@ def add_contrib(request):
author_body = get_template(
'contribmap/mails/new_contrib_author_notice.txt')
send_mail(
author_subject.render(context),
author_body.render(context),
settings.DEFAULT_FROM_EMAIL,
[contrib.email],
)
send_contributor_email(
contrib, author_subject, author_body, mgmt_token,
request=request)
return redirect(reverse('thanks', kwargs={
'token': mgmt_token,
......@@ -71,7 +57,7 @@ def add_contrib(request):
return render(request, 'contribmap/wifi-form.html', {
'form': form,
'isp':settings.ISP,
'isp': settings.ISP,
})
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment