annotate gpp/accounts/forms.py @ 346:efa3b4901777

As part of #165 add a security question to the registration form.
author Brian Neal <bgneal@gmail.com>
date Mon, 28 Feb 2011 03:53:04 +0000
parents 767cedc7d12a
children 69d0306a6fe7
rev   line source
gremmie@1 1 """forms for the accounts application"""
gremmie@1 2
bgneal@74 3 import logging
bgneal@74 4
gremmie@1 5 from django import forms
gremmie@1 6 from django.contrib.auth.models import User
gremmie@1 7 from django.core.urlresolvers import reverse
gremmie@1 8 from django.template.loader import render_to_string
gremmie@1 9 from django.contrib.sites.models import Site
bgneal@6 10 from django.conf import settings
gremmie@1 11
gremmie@1 12 from core.functions import send_mail
gremmie@1 13 from accounts.models import PendingUser
gremmie@1 14 from accounts.models import IllegalUsername
gremmie@1 15 from accounts.models import IllegalEmail
gremmie@1 16
gremmie@1 17
gremmie@1 18 class RegisterForm(forms.Form):
bgneal@74 19 """Form used to register with the website"""
bgneal@346 20 username = forms.RegexField(max_length=30, regex=r'^\w+$',
bgneal@155 21 error_messages={
bgneal@155 22 'invalid': 'Your username must be 30 characters or less and ' \
bgneal@155 23 'contain only letters, numbers and underscores.'})
bgneal@74 24 email = forms.EmailField()
bgneal@346 25 password1 = forms.CharField(label="Password", widget=forms.PasswordInput)
bgneal@346 26 password2 = forms.CharField(label="Password confirmation", widget=forms.PasswordInput)
bgneal@316 27 agree_age = forms.BooleanField(required=True,
bgneal@155 28 label='I certify that I am over the age of 13',
bgneal@155 29 error_messages={
bgneal@155 30 'required': 'Sorry, but you must be over the age of 13 to ' \
bgneal@155 31 'register at our site.',
bgneal@155 32 })
bgneal@316 33 agree_tos = forms.BooleanField(required=True,
bgneal@155 34 label='I agree to the Terms of Service',
bgneal@155 35 error_messages={
bgneal@155 36 'required': 'You have not agreed to our Terms of Service.',
bgneal@155 37 })
bgneal@155 38 agree_privacy = forms.BooleanField(required=True,
bgneal@155 39 label='I agree to the Privacy Policy',
bgneal@155 40 error_messages={
bgneal@155 41 'required': 'You have not agreed to our Privacy Policy.',
bgneal@155 42 })
bgneal@346 43 question1 = forms.CharField(label="What number appears in the site name?")
gremmie@1 44
bgneal@74 45 def __init__(self, *args, **kwargs):
bgneal@74 46 self.ip = kwargs.pop('ip', '?')
bgneal@74 47 super(RegisterForm, self).__init__(*args, **kwargs)
bgneal@74 48
bgneal@74 49 def clean_username(self):
bgneal@74 50 username = self.cleaned_data['username']
bgneal@74 51 try:
bgneal@74 52 User.objects.get(username = username)
bgneal@74 53 except User.DoesNotExist:
gremmie@1 54 try:
bgneal@74 55 PendingUser.objects.get(username = username)
bgneal@74 56 except PendingUser.DoesNotExist:
bgneal@74 57 try:
bgneal@74 58 IllegalUsername.objects.get(username = username)
bgneal@74 59 except IllegalUsername.DoesNotExist:
bgneal@74 60 return username
bgneal@74 61 self._validation_error("That username is not allowed.", username)
bgneal@74 62 self._validation_error("A pending user with that username already exists.", username)
bgneal@74 63 self._validation_error("A user with that username already exists.", username)
gremmie@1 64
bgneal@74 65 def clean_email(self):
bgneal@74 66 email = self.cleaned_data['email']
bgneal@74 67 try:
bgneal@74 68 User.objects.get(email = email)
bgneal@74 69 except User.DoesNotExist:
gremmie@1 70 try:
bgneal@74 71 PendingUser.objects.get(email = email)
bgneal@74 72 except PendingUser.DoesNotExist:
bgneal@74 73 try:
bgneal@74 74 IllegalEmail.objects.get(email = email)
bgneal@74 75 except IllegalEmail.DoesNotExist:
bgneal@74 76 return email
bgneal@74 77 self._validation_error("That email address is not allowed.", email)
bgneal@74 78 self._validation_error("A pending user with that email address already exists.", email)
bgneal@74 79 self._validation_error("A user with that email address already exists.", email)
gremmie@1 80
bgneal@74 81 def clean_password2(self):
bgneal@74 82 password1 = self.cleaned_data.get("password1", "")
bgneal@74 83 password2 = self.cleaned_data["password2"]
bgneal@74 84 if password1 != password2:
bgneal@74 85 self._validation_error("The two password fields didn't match.")
bgneal@155 86 if len(password1) < 6:
bgneal@155 87 self._validation_error("Please choose a password of 6 characters or more.")
bgneal@74 88 return password2
gremmie@1 89
bgneal@346 90 def clean_question1(self):
bgneal@346 91 answer = self.cleaned_data.get('question1')
bgneal@346 92 success = False
bgneal@346 93 if answer:
bgneal@346 94 try:
bgneal@346 95 val = int(answer)
bgneal@346 96 except ValueError:
bgneal@346 97 pass
bgneal@346 98 else:
bgneal@346 99 success = val == 101
bgneal@346 100 if not success:
bgneal@346 101 self._validation_error("Incorrect answer to our anti-spam question.", answer)
bgneal@346 102 return answer
bgneal@346 103
bgneal@74 104 def save(self):
bgneal@74 105 pending_user = PendingUser.objects.create_pending_user(self.cleaned_data['username'],
bgneal@74 106 self.cleaned_data['email'],
bgneal@74 107 self.cleaned_data['password1'])
gremmie@1 108
bgneal@74 109 # Send the confirmation email
gremmie@1 110
bgneal@74 111 site = Site.objects.get_current()
bgneal@74 112 admin_email = settings.ADMINS[0][1]
gremmie@1 113
bgneal@316 114 activation_link = 'http://%s%s' % (site.domain, reverse('accounts.views.register_confirm',
bgneal@74 115 kwargs = {'username' : pending_user.username, 'key' : pending_user.key}))
gremmie@1 116
bgneal@74 117 msg = render_to_string('accounts/registration_email.txt',
bgneal@74 118 {
bgneal@74 119 'site_name' : site.name,
bgneal@74 120 'site_domain' : site.domain,
bgneal@74 121 'user_email' : pending_user.email,
bgneal@74 122 'activation_link' : activation_link,
bgneal@74 123 'username' : pending_user.username,
bgneal@74 124 'admin_email' : admin_email,
bgneal@74 125 })
gremmie@1 126
bgneal@74 127 subject = 'Registration Confirmation for ' + site.name
bgneal@252 128 send_mail(subject, msg, admin_email, [self.cleaned_data['email']],
bgneal@252 129 expedite=True)
bgneal@316 130 logging.info('Accounts/registration conf. email sent to %s for user %s; IP = %s',
bgneal@316 131 self.cleaned_data['email'], pending_user.username, self.ip)
gremmie@1 132
bgneal@74 133 return pending_user
gremmie@1 134
bgneal@74 135 def _validation_error(self, msg, param=None):
bgneal@316 136 logging.error('Accounts/registration [%s]: %s (%s)', self.ip, msg, param)
bgneal@74 137 raise forms.ValidationError(msg)