Pattern for email sending.
- Easy to grep project for where emails get sent.
- Easy to send the same email from multiple places.
- Constructs urls in a reliable way such that you don't get ssl errors for missing
www
.
Pattern for email sending.
www
.from django.dispatch import Signal | |
from django.contrib.sites.models import Site | |
from django.conf import settings | |
from django.http import HttpRequest | |
from django.core.urlresolvers import reverse | |
from fusionbox.mail import send_markdown_mail | |
new_poll_created = Signal(providing_args=['poll']) | |
def build_absolute_uri(path): | |
site = Site.objects.get_current(), | |
return "https://www.{s.domain}{p}".format(s=site, p=path) | |
def send_new_poll_emails(sender, poll, **kwargs): | |
if isinstance(sender, HttpRequest): | |
build_absolute_uri = sender.build_absolute_uri | |
context = { | |
'poll': poll, | |
'poll_url': build_absolute_uri(inquiry.listing.get_absolute_url()), | |
'admin_url': build_absolute_uri(reverse('admin:polls_poll_change', args=[poll.pk])), | |
} | |
send_markdown_mail('polls/mail/new_poll.html', context, to=['[email protected]']) | |
def register_signals(): | |
new_poll_created.connect(send_new_poll_emails) |
#| | |
#| Connect email receivers. This goes here in models.py per | |
#| https://docs.djangoproject.com/en/dev/topics/signals/ | |
#| | |
from polls.emails import register_signals | |
register_signals() | |
#| END email recievers |
from polls.email import new_poll_created | |
class CreatePollView(CreateView): | |
... | |
def form_valid(self, form): | |
... | |
new_poll_created.send(sender=self.request, poll=poll) | |
... |