annotate accounts/models.py @ 887:9a15f7c27526

Actually save model object upon change. This commit was tested on the comments model. Additional logging added. Added check for Markdown image references. Added TODOs after observing behavior on comments.
author Brian Neal <bgneal@gmail.com>
date Tue, 03 Feb 2015 21:09:44 -0600
parents 1555b2c3c7a0
children
rev   line source
gremmie@1 1 """Contains models for the accounts application"""
gremmie@1 2
gremmie@1 3 import datetime
gremmie@1 4 import random
gremmie@1 5 import string
gremmie@1 6 import hashlib
gremmie@1 7 import base64
gremmie@1 8
gremmie@1 9 from django.db import models
bgneal@259 10 from django.contrib.auth.models import User
gremmie@1 11 from django.conf import settings
gremmie@1 12
gremmie@1 13
gremmie@1 14 class IllegalUsername(models.Model):
gremmie@1 15 """model to represent the list of illegal usernames"""
bgneal@259 16 username = models.CharField(max_length=30, db_index=True)
gremmie@1 17
gremmie@1 18 def __unicode__(self):
gremmie@1 19 return self.username
gremmie@1 20
gremmie@1 21 class Meta:
gremmie@1 22 ordering = ('username', )
gremmie@1 23
gremmie@1 24
gremmie@1 25 class IllegalEmail(models.Model):
gremmie@1 26 """model to represent the list of illegal/restricted email addresses"""
bgneal@259 27 email = models.EmailField(db_index=True)
gremmie@1 28
gremmie@1 29 def __unicode__(self):
gremmie@1 30 return self.email
gremmie@1 31
gremmie@1 32 class Meta:
gremmie@1 33 ordering = ('email', )
gremmie@1 34
gremmie@1 35
gremmie@1 36 class PendingUserManager(models.Manager):
gremmie@1 37 """user manager for PendingUser model"""
gremmie@1 38
gremmie@1 39 create_count = 0
gremmie@1 40
gremmie@1 41 def create_pending_user(self, username, email, password):
gremmie@1 42 '''creates a new pending user and saves it to the database'''
gremmie@1 43
bgneal@259 44 temp_user = User()
gremmie@1 45 temp_user.set_password(password)
gremmie@1 46
bgneal@623 47 now = datetime.datetime.now()
bgneal@623 48 pending_user = self.model(None,
bgneal@623 49 username,
bgneal@623 50 email,
bgneal@623 51 temp_user.password,
bgneal@623 52 now,
gremmie@1 53 self._make_key())
gremmie@1 54
gremmie@1 55 pending_user.save()
gremmie@1 56 self.create_count += 1
gremmie@1 57 return pending_user
gremmie@1 58
gremmie@1 59
gremmie@1 60 def purge_expired(self):
bgneal@259 61 expire_time = datetime.datetime.now() - datetime.timedelta(days=1)
bgneal@259 62 expired_pending_users = self.filter(date_joined__lt=expire_time)
gremmie@1 63 expired_pending_users.delete()
gremmie@1 64
gremmie@1 65
gremmie@1 66 def _make_key(self):
gremmie@1 67 s = ''.join(random.sample(string.printable, 8))
gremmie@1 68 delta = datetime.date.today() - datetime.date(1846, 12, 28)
gremmie@1 69 days = base64.urlsafe_b64encode(str(delta * 10))
gremmie@1 70 key = hashlib.sha1(settings.SECRET_KEY +
gremmie@1 71 unicode(self.create_count) +
gremmie@1 72 unicode(s) +
gremmie@1 73 unicode(days)).hexdigest()[::2]
gremmie@1 74 return key
gremmie@1 75
gremmie@1 76
gremmie@1 77 class PendingUser(models.Model):
gremmie@1 78 """model for holding users while they go through the email registration cycle"""
gremmie@1 79
bgneal@623 80 username = models.CharField(max_length=30, db_index=True, unique=True)
gremmie@1 81 email = models.EmailField()
bgneal@259 82 password = models.CharField(max_length=128)
bgneal@259 83 date_joined = models.DateTimeField(default=datetime.datetime.now, db_index=True)
bgneal@623 84 key = models.CharField(max_length=20)
gremmie@1 85
gremmie@1 86 objects = PendingUserManager()
gremmie@1 87
gremmie@1 88 def __unicode__(self):
gremmie@1 89 return self.username
gremmie@1 90