gremmie@1: """Contains models for the accounts application""" gremmie@1: gremmie@1: import datetime gremmie@1: import random gremmie@1: import string gremmie@1: import hashlib gremmie@1: import base64 gremmie@1: gremmie@1: from django.db import models bgneal@259: from django.contrib.auth.models import User gremmie@1: from django.conf import settings gremmie@1: gremmie@1: gremmie@1: class IllegalUsername(models.Model): gremmie@1: """model to represent the list of illegal usernames""" bgneal@259: username = models.CharField(max_length=30, db_index=True) gremmie@1: gremmie@1: def __unicode__(self): gremmie@1: return self.username gremmie@1: gremmie@1: class Meta: gremmie@1: ordering = ('username', ) gremmie@1: gremmie@1: gremmie@1: class IllegalEmail(models.Model): gremmie@1: """model to represent the list of illegal/restricted email addresses""" bgneal@259: email = models.EmailField(db_index=True) gremmie@1: gremmie@1: def __unicode__(self): gremmie@1: return self.email gremmie@1: gremmie@1: class Meta: gremmie@1: ordering = ('email', ) gremmie@1: gremmie@1: gremmie@1: class PendingUserManager(models.Manager): gremmie@1: """user manager for PendingUser model""" gremmie@1: gremmie@1: create_count = 0 gremmie@1: gremmie@1: def create_pending_user(self, username, email, password): gremmie@1: '''creates a new pending user and saves it to the database''' gremmie@1: bgneal@259: temp_user = User() gremmie@1: temp_user.set_password(password) gremmie@1: bgneal@623: now = datetime.datetime.now() bgneal@623: pending_user = self.model(None, bgneal@623: username, bgneal@623: email, bgneal@623: temp_user.password, bgneal@623: now, gremmie@1: self._make_key()) gremmie@1: gremmie@1: pending_user.save() gremmie@1: self.create_count += 1 gremmie@1: return pending_user gremmie@1: gremmie@1: gremmie@1: def purge_expired(self): bgneal@259: expire_time = datetime.datetime.now() - datetime.timedelta(days=1) bgneal@259: expired_pending_users = self.filter(date_joined__lt=expire_time) gremmie@1: expired_pending_users.delete() gremmie@1: gremmie@1: gremmie@1: def _make_key(self): gremmie@1: s = ''.join(random.sample(string.printable, 8)) gremmie@1: delta = datetime.date.today() - datetime.date(1846, 12, 28) gremmie@1: days = base64.urlsafe_b64encode(str(delta * 10)) gremmie@1: key = hashlib.sha1(settings.SECRET_KEY + gremmie@1: unicode(self.create_count) + gremmie@1: unicode(s) + gremmie@1: unicode(days)).hexdigest()[::2] gremmie@1: return key gremmie@1: gremmie@1: gremmie@1: class PendingUser(models.Model): gremmie@1: """model for holding users while they go through the email registration cycle""" gremmie@1: bgneal@623: username = models.CharField(max_length=30, db_index=True, unique=True) gremmie@1: email = models.EmailField() bgneal@259: password = models.CharField(max_length=128) bgneal@259: date_joined = models.DateTimeField(default=datetime.datetime.now, db_index=True) bgneal@623: key = models.CharField(max_length=20) gremmie@1: gremmie@1: objects = PendingUserManager() gremmie@1: gremmie@1: def __unicode__(self): gremmie@1: return self.username gremmie@1: