annotate gpp/accounts/forms.py @ 271:4746df47a538

Follow on to last rev (r292) for #126. Missed updating a shoutbox template. Also the repoze.timeago package uses UTC time by default. Change this to local time for now until we decide to switch over to UTC for everything.
author Brian Neal <bgneal@gmail.com>
date Sun, 26 Sep 2010 17:42:00 +0000
parents e3958aacd8dd
children 767cedc7d12a
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@155 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@74 25 password1 = forms.CharField(label = "Password", widget = forms.PasswordInput)
bgneal@74 26 password2 = forms.CharField(label = "Password confirmation", widget = forms.PasswordInput)
bgneal@155 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@155 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 })
gremmie@1 43
bgneal@74 44 def __init__(self, *args, **kwargs):
bgneal@74 45 self.ip = kwargs.pop('ip', '?')
bgneal@74 46 super(RegisterForm, self).__init__(*args, **kwargs)
bgneal@74 47
bgneal@74 48 def clean_username(self):
bgneal@74 49 username = self.cleaned_data['username']
bgneal@74 50 try:
bgneal@74 51 User.objects.get(username = username)
bgneal@74 52 except User.DoesNotExist:
gremmie@1 53 try:
bgneal@74 54 PendingUser.objects.get(username = username)
bgneal@74 55 except PendingUser.DoesNotExist:
bgneal@74 56 try:
bgneal@74 57 IllegalUsername.objects.get(username = username)
bgneal@74 58 except IllegalUsername.DoesNotExist:
bgneal@74 59 return username
bgneal@74 60 self._validation_error("That username is not allowed.", username)
bgneal@74 61 self._validation_error("A pending user with that username already exists.", username)
bgneal@74 62 self._validation_error("A user with that username already exists.", username)
gremmie@1 63
bgneal@74 64 def clean_email(self):
bgneal@74 65 email = self.cleaned_data['email']
bgneal@74 66 try:
bgneal@74 67 User.objects.get(email = email)
bgneal@74 68 except User.DoesNotExist:
gremmie@1 69 try:
bgneal@74 70 PendingUser.objects.get(email = email)
bgneal@74 71 except PendingUser.DoesNotExist:
bgneal@74 72 try:
bgneal@74 73 IllegalEmail.objects.get(email = email)
bgneal@74 74 except IllegalEmail.DoesNotExist:
bgneal@74 75 return email
bgneal@74 76 self._validation_error("That email address is not allowed.", email)
bgneal@74 77 self._validation_error("A pending user with that email address already exists.", email)
bgneal@74 78 self._validation_error("A user with that email address already exists.", email)
gremmie@1 79
bgneal@74 80 def clean_password2(self):
bgneal@74 81 password1 = self.cleaned_data.get("password1", "")
bgneal@74 82 password2 = self.cleaned_data["password2"]
bgneal@74 83 if password1 != password2:
bgneal@74 84 self._validation_error("The two password fields didn't match.")
bgneal@155 85 if len(password1) < 6:
bgneal@155 86 self._validation_error("Please choose a password of 6 characters or more.")
bgneal@74 87 return password2
gremmie@1 88
bgneal@74 89 def save(self):
bgneal@74 90 pending_user = PendingUser.objects.create_pending_user(self.cleaned_data['username'],
bgneal@74 91 self.cleaned_data['email'],
bgneal@74 92 self.cleaned_data['password1'])
gremmie@1 93
bgneal@74 94 # Send the confirmation email
gremmie@1 95
bgneal@74 96 site = Site.objects.get_current()
bgneal@74 97 admin_email = settings.ADMINS[0][1]
gremmie@1 98
bgneal@74 99 activation_link = 'http://%s%s' % (site.domain, reverse('accounts.views.register_confirm',
bgneal@74 100 kwargs = {'username' : pending_user.username, 'key' : pending_user.key}))
gremmie@1 101
bgneal@74 102 msg = render_to_string('accounts/registration_email.txt',
bgneal@74 103 {
bgneal@74 104 'site_name' : site.name,
bgneal@74 105 'site_domain' : site.domain,
bgneal@74 106 'user_email' : pending_user.email,
bgneal@74 107 'activation_link' : activation_link,
bgneal@74 108 'username' : pending_user.username,
bgneal@74 109 'admin_email' : admin_email,
bgneal@74 110 })
gremmie@1 111
bgneal@74 112 subject = 'Registration Confirmation for ' + site.name
bgneal@252 113 send_mail(subject, msg, admin_email, [self.cleaned_data['email']],
bgneal@252 114 expedite=True)
bgneal@74 115 logging.info('Accounts/registration conf. email sent to %s for user %s; IP = %s' % \
bgneal@74 116 (self.cleaned_data['email'], pending_user.username, self.ip))
gremmie@1 117
bgneal@74 118 return pending_user
gremmie@1 119
bgneal@74 120 def _validation_error(self, msg, param=None):
bgneal@74 121 logging.error('Accounts/registration [%s]: %s (%s)' % (self.ip, msg, param))
bgneal@74 122 raise forms.ValidationError(msg)