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 gremmie@1: from django.contrib import auth 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""" gremmie@1: 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""" gremmie@1: 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: gremmie@1: temp_user = auth.models.User() gremmie@1: temp_user.set_password(password) gremmie@1: gremmie@1: now = datetime.datetime.now() gremmie@1: pending_user = self.model(None, gremmie@1: username, gremmie@1: email, gremmie@1: temp_user.password, gremmie@1: 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): gremmie@1: expire_time = datetime.datetime.now() - datetime.timedelta(days = 1) gremmie@1: 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: gremmie@1: username = models.CharField(max_length = 30, db_index = True) gremmie@1: email = models.EmailField() gremmie@1: password = models.CharField(max_length = 128) gremmie@1: date_joined = models.DateTimeField(default = datetime.datetime.now, db_index = True) gremmie@1: key = models.CharField(max_length = 20, editable = True) gremmie@1: gremmie@1: objects = PendingUserManager() gremmie@1: gremmie@1: def __unicode__(self): gremmie@1: return self.username gremmie@1: