view email_list/models.py @ 76:e4faf6f90710

Added Tribal Fires quotes on the home page.
author Brian Neal <bgneal@gmail.com>
date Sat, 28 Jul 2012 22:22:02 -0500
parents e2868ad47a1e
children
line wrap: on
line source
"""
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())