diff accounts/models.py @ 581:ee87ea74d46b

For Django 1.4, rearranged project structure for new manage.py.
author Brian Neal <bgneal@gmail.com>
date Sat, 05 May 2012 17:10:48 -0500
parents gpp/accounts/models.py@75ea1a8be7f2
children 1555b2c3c7a0
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/accounts/models.py	Sat May 05 17:10:48 2012 -0500
@@ -0,0 +1,90 @@
+"""Contains models for the accounts application"""
+
+import datetime
+import random
+import string
+import hashlib
+import base64
+
+from django.db import models
+from django.contrib.auth.models import User
+from django.conf import settings
+
+
+class IllegalUsername(models.Model):
+   """model to represent the list of illegal usernames"""
+   username = models.CharField(max_length=30, db_index=True)
+
+   def __unicode__(self):
+      return self.username
+
+   class Meta:
+      ordering = ('username', )
+
+
+class IllegalEmail(models.Model):
+   """model to represent the list of illegal/restricted email addresses"""
+   email = models.EmailField(db_index=True)
+
+   def __unicode__(self):
+      return self.email
+
+   class Meta:
+      ordering = ('email', )
+
+
+class PendingUserManager(models.Manager):
+   """user manager for PendingUser model"""
+
+   create_count = 0
+
+   def create_pending_user(self, username, email, password):
+      '''creates a new pending user and saves it to the database'''
+
+      temp_user = User()
+      temp_user.set_password(password)
+
+      now = datetime.datetime.now() 
+      pending_user = self.model(None, 
+            username, 
+            email, 
+            temp_user.password, 
+            now, 
+            self._make_key())
+
+      pending_user.save()
+      self.create_count += 1
+      return pending_user
+
+
+   def purge_expired(self):
+      expire_time = datetime.datetime.now() - datetime.timedelta(days=1)
+      expired_pending_users = self.filter(date_joined__lt=expire_time)
+      expired_pending_users.delete()
+
+
+   def _make_key(self):
+      s = ''.join(random.sample(string.printable, 8))
+      delta = datetime.date.today() - datetime.date(1846, 12, 28)
+      days = base64.urlsafe_b64encode(str(delta * 10))
+      key = hashlib.sha1(settings.SECRET_KEY +
+         unicode(self.create_count) +
+         unicode(s) +
+         unicode(days)).hexdigest()[::2]
+      return key
+
+
+class PendingUser(models.Model):
+   """model for holding users while they go through the email registration cycle"""
+
+   username = models.CharField(max_length=30, db_index=True)
+   email = models.EmailField()
+   password = models.CharField(max_length=128)
+   date_joined = models.DateTimeField(default=datetime.datetime.now, db_index=True)
+   key = models.CharField(max_length=20, editable=True)
+
+   objects = PendingUserManager()
+
+   def __unicode__(self):
+      return self.username
+