comparison email_list/models.py @ 71:e2868ad47a1e

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