Mercurial > public > madeira
diff madeira/email_list/models.py @ 51:13b2561c909d
For issue #7, create a mailing list application.
Still need to test that emails are being sent.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Wed, 28 Mar 2012 21:13:05 -0500 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/madeira/email_list/models.py Wed Mar 28 21:13:05 2012 -0500 @@ -0,0 +1,69 @@ +""" +Models for the email_list application. + +""" +import base64 +import datetime +import hashlib + +from django.db import models +from django.conf import settings + + +class Subscriber(models.Model): + status_codes = [('P', 'Pending'), ('A', 'Active'), ('L', 'Leaving')] + key_length = 28 + + name = models.CharField(max_length=64, blank=True) + email = models.EmailField(db_index=True) + location = models.CharField(max_length=64, blank=True) + status = models.CharField(max_length=1, choices=status_codes, default='A') + key = models.CharField(max_length=key_length, editable=False, blank=True, + db_index=True) + status_date = models.DateTimeField(editable=False) + + class Meta: + ordering = ['name', 'email'] + + def __unicode__(self): + if self.name: + return u'%s <%s>' % (self.name, self.email) + return self.email + + def save(self, *args, **kwargs): + if not self.pk and not self.status_date: + self.status_date = datetime.datetime.now() + + super(Subscriber, self).save(*args, **kwargs) + + def set_pending(self): + self.status = 'P' + self.status_date = datetime.datetime.now() + self.gen_key() + + def set_active(self): + self.status = 'A' + self.status_date = datetime.datetime.now() + self.key = '' + + def set_leaving(self): + self.status = 'L' + self.status_date = datetime.datetime.now() + self.gen_key() + + def is_pending(self): + return self.status == 'P' + + def is_leaving(self): + return self.status == 'L' + + def is_active(self): + return self.status == 'A' + + def gen_key(self): + source = (settings.SECRET_KEY + self.email + self.name + self.location + + self.status + self.status_date.isoformat()) + + sha = hashlib.sha1() + sha.update(source) + self.key = base64.urlsafe_b64encode(sha.digest())