Source

django-petition / petition / models.py

Full commit
from django.db import models
from petition.fields import CountryField
import hashlib
from django.conf import settings
from django.dispatch.dispatcher import receiver
from django.db.models.signals import post_save
from django.core.mail import send_mail
from django.template.loader import render_to_string

class Signature(models.Model):
    first_name = models.CharField(max_length=60)
    last_name = models.CharField(max_length=60)
    email = models.EmailField(unique=True, error_messages={'unique':'This email address has already signed.', 'blank':'Please fill in your email address.', 'invalid':'You entered in an invalid email address.'})
    state = models.CharField(max_length=40)
    country = CountryField(default='US')
    date_signed = models.DateTimeField(auto_now_add=True)
    confirmed = models.BooleanField()
    class Meta:
        unique_together = ('first_name', 'last_name')
    def __unicode__(self):
        return u"%s %s" % (self.first_name, self.last_name)
    @models.permalink
    def get_absolute_url(self):
        return ('signatures', [])
    def get_email_hash(self):
        return hashlib.md5(self.email+settings.SECRET_KEY).hexdigest()
    @models.permalink
    def get_confirm_link(self):
        return ('confirm-email', [str(self.pk), self.get_email_hash()])

@receiver(post_save, sender=Signature)
def send_confirm_email(sender, instance, created, **kwargs):
    if created:
        subject = render_to_string('petition/email_subject.txt', {'signature':instance})
        body = render_to_string('petition/email_confirm.txt', {'signature':instance})
        send_mail(subject, body, settings.PETITION_FROM, [instance.email], fail_silently=True)