annotate email_list/models.py @ 168:8a93b1f838f9

Update fabric file for Django 1.7 upgrade. cleanup command is now called clearsessions.
author Brian Neal <bgneal@gmail.com>
date Thu, 02 Apr 2015 21:04:08 -0500
parents e2868ad47a1e
children
rev   line source
bgneal@51 1 """
bgneal@51 2 Models for the email_list application.
bgneal@51 3
bgneal@51 4 """
bgneal@51 5 import base64
bgneal@51 6 import datetime
bgneal@51 7 import hashlib
bgneal@51 8
bgneal@51 9 from django.db import models
bgneal@51 10 from django.conf import settings
bgneal@51 11
bgneal@51 12
bgneal@51 13 class Subscriber(models.Model):
bgneal@51 14 status_codes = [('P', 'Pending'), ('A', 'Active'), ('L', 'Leaving')]
bgneal@51 15 key_length = 28
bgneal@51 16
bgneal@51 17 name = models.CharField(max_length=64, blank=True)
bgneal@51 18 email = models.EmailField(db_index=True)
bgneal@51 19 location = models.CharField(max_length=64, blank=True)
bgneal@51 20 status = models.CharField(max_length=1, choices=status_codes, default='A')
bgneal@51 21 key = models.CharField(max_length=key_length, editable=False, blank=True,
bgneal@51 22 db_index=True)
bgneal@51 23 status_date = models.DateTimeField(editable=False)
bgneal@51 24
bgneal@51 25 class Meta:
bgneal@51 26 ordering = ['name', 'email']
bgneal@51 27
bgneal@51 28 def __unicode__(self):
bgneal@51 29 if self.name:
bgneal@51 30 return u'%s <%s>' % (self.name, self.email)
bgneal@51 31 return self.email
bgneal@51 32
bgneal@51 33 def save(self, *args, **kwargs):
bgneal@51 34 if not self.pk and not self.status_date:
bgneal@51 35 self.status_date = datetime.datetime.now()
bgneal@51 36
bgneal@51 37 super(Subscriber, self).save(*args, **kwargs)
bgneal@51 38
bgneal@51 39 def set_pending(self):
bgneal@51 40 self.status = 'P'
bgneal@51 41 self.status_date = datetime.datetime.now()
bgneal@51 42 self.gen_key()
bgneal@51 43
bgneal@51 44 def set_active(self):
bgneal@51 45 self.status = 'A'
bgneal@51 46 self.status_date = datetime.datetime.now()
bgneal@51 47 self.key = ''
bgneal@51 48
bgneal@51 49 def set_leaving(self):
bgneal@51 50 self.status = 'L'
bgneal@51 51 self.status_date = datetime.datetime.now()
bgneal@51 52 self.gen_key()
bgneal@51 53
bgneal@51 54 def is_pending(self):
bgneal@51 55 return self.status == 'P'
bgneal@51 56
bgneal@51 57 def is_leaving(self):
bgneal@51 58 return self.status == 'L'
bgneal@51 59
bgneal@51 60 def is_active(self):
bgneal@51 61 return self.status == 'A'
bgneal@51 62
bgneal@51 63 def gen_key(self):
bgneal@51 64 source = (settings.SECRET_KEY + self.email + self.name + self.location +
bgneal@51 65 self.status + self.status_date.isoformat())
bgneal@51 66
bgneal@51 67 sha = hashlib.sha1()
bgneal@51 68 sha.update(source)
bgneal@51 69 self.key = base64.urlsafe_b64encode(sha.digest())