diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/email_list/models.py	Sat Apr 14 16:40:29 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())