comparison 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
comparison
equal deleted inserted replaced
580:c525f3e0b5d0 581:ee87ea74d46b
1 """Contains models for the accounts application"""
2
3 import datetime
4 import random
5 import string
6 import hashlib
7 import base64
8
9 from django.db import models
10 from django.contrib.auth.models import User
11 from django.conf import settings
12
13
14 class IllegalUsername(models.Model):
15 """model to represent the list of illegal usernames"""
16 username = models.CharField(max_length=30, db_index=True)
17
18 def __unicode__(self):
19 return self.username
20
21 class Meta:
22 ordering = ('username', )
23
24
25 class IllegalEmail(models.Model):
26 """model to represent the list of illegal/restricted email addresses"""
27 email = models.EmailField(db_index=True)
28
29 def __unicode__(self):
30 return self.email
31
32 class Meta:
33 ordering = ('email', )
34
35
36 class PendingUserManager(models.Manager):
37 """user manager for PendingUser model"""
38
39 create_count = 0
40
41 def create_pending_user(self, username, email, password):
42 '''creates a new pending user and saves it to the database'''
43
44 temp_user = User()
45 temp_user.set_password(password)
46
47 now = datetime.datetime.now()
48 pending_user = self.model(None,
49 username,
50 email,
51 temp_user.password,
52 now,
53 self._make_key())
54
55 pending_user.save()
56 self.create_count += 1
57 return pending_user
58
59
60 def purge_expired(self):
61 expire_time = datetime.datetime.now() - datetime.timedelta(days=1)
62 expired_pending_users = self.filter(date_joined__lt=expire_time)
63 expired_pending_users.delete()
64
65
66 def _make_key(self):
67 s = ''.join(random.sample(string.printable, 8))
68 delta = datetime.date.today() - datetime.date(1846, 12, 28)
69 days = base64.urlsafe_b64encode(str(delta * 10))
70 key = hashlib.sha1(settings.SECRET_KEY +
71 unicode(self.create_count) +
72 unicode(s) +
73 unicode(days)).hexdigest()[::2]
74 return key
75
76
77 class PendingUser(models.Model):
78 """model for holding users while they go through the email registration cycle"""
79
80 username = models.CharField(max_length=30, db_index=True)
81 email = models.EmailField()
82 password = models.CharField(max_length=128)
83 date_joined = models.DateTimeField(default=datetime.datetime.now, db_index=True)
84 key = models.CharField(max_length=20, editable=True)
85
86 objects = PendingUserManager()
87
88 def __unicode__(self):
89 return self.username
90