annotate gpp/accounts/forms.py @ 102:e67c4dd98db5

Forums: new topic form sprouts boolean fields for sticky and locking if the user has rights. Implemented the locked logic. Fixed a bug where topics where getting out of order (the view_count was bumping the update_date because of auto_now).
author Brian Neal <bgneal@gmail.com>
date Wed, 16 Sep 2009 02:01:57 +0000
parents df56795771a6
children ef93dc9f1992
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@74 20 username = forms.RegexField(max_length = 30, regex = r'^\w+$',
bgneal@74 21 error_messages = {'invalid' : 'Your username must be 30 characters or less and contain only letters, numbers and underscores.'})
bgneal@74 22 email = forms.EmailField()
bgneal@74 23 password1 = forms.CharField(label = "Password", widget = forms.PasswordInput)
bgneal@74 24 password2 = forms.CharField(label = "Password confirmation", widget = forms.PasswordInput)
bgneal@74 25 agree_tos = forms.BooleanField(required = True, label = 'I agree to the Terms of Service',
bgneal@74 26 error_messages = {'required' : 'You have not agreed to our Terms of Service'})
bgneal@74 27 agree_privacy = forms.BooleanField(required = True, label = 'I agree to the Privacy Policy',
bgneal@74 28 error_messages = {'required' : 'You have not agreed to our Privacy Policy'})
gremmie@1 29
bgneal@74 30 def __init__(self, *args, **kwargs):
bgneal@74 31 self.ip = kwargs.pop('ip', '?')
bgneal@74 32 super(RegisterForm, self).__init__(*args, **kwargs)
bgneal@74 33
bgneal@74 34 def clean_username(self):
bgneal@74 35 username = self.cleaned_data['username']
bgneal@74 36 try:
bgneal@74 37 User.objects.get(username = username)
bgneal@74 38 except User.DoesNotExist:
gremmie@1 39 try:
bgneal@74 40 PendingUser.objects.get(username = username)
bgneal@74 41 except PendingUser.DoesNotExist:
bgneal@74 42 try:
bgneal@74 43 IllegalUsername.objects.get(username = username)
bgneal@74 44 except IllegalUsername.DoesNotExist:
bgneal@74 45 return username
bgneal@74 46 self._validation_error("That username is not allowed.", username)
bgneal@74 47 self._validation_error("A pending user with that username already exists.", username)
bgneal@74 48 self._validation_error("A user with that username already exists.", username)
gremmie@1 49
bgneal@74 50 def clean_email(self):
bgneal@74 51 email = self.cleaned_data['email']
bgneal@74 52 try:
bgneal@74 53 User.objects.get(email = email)
bgneal@74 54 except User.DoesNotExist:
gremmie@1 55 try:
bgneal@74 56 PendingUser.objects.get(email = email)
bgneal@74 57 except PendingUser.DoesNotExist:
bgneal@74 58 try:
bgneal@74 59 IllegalEmail.objects.get(email = email)
bgneal@74 60 except IllegalEmail.DoesNotExist:
bgneal@74 61 return email
bgneal@74 62 self._validation_error("That email address is not allowed.", email)
bgneal@74 63 self._validation_error("A pending user with that email address already exists.", email)
bgneal@74 64 self._validation_error("A user with that email address already exists.", email)
gremmie@1 65
bgneal@74 66 def clean_password2(self):
bgneal@74 67 password1 = self.cleaned_data.get("password1", "")
bgneal@74 68 password2 = self.cleaned_data["password2"]
bgneal@74 69 if password1 != password2:
bgneal@74 70 self._validation_error("The two password fields didn't match.")
bgneal@74 71 return password2
gremmie@1 72
bgneal@74 73 def save(self):
bgneal@74 74 pending_user = PendingUser.objects.create_pending_user(self.cleaned_data['username'],
bgneal@74 75 self.cleaned_data['email'],
bgneal@74 76 self.cleaned_data['password1'])
gremmie@1 77
bgneal@74 78 # Send the confirmation email
gremmie@1 79
bgneal@74 80 site = Site.objects.get_current()
bgneal@74 81 admin_email = settings.ADMINS[0][1]
gremmie@1 82
bgneal@74 83 activation_link = 'http://%s%s' % (site.domain, reverse('accounts.views.register_confirm',
bgneal@74 84 kwargs = {'username' : pending_user.username, 'key' : pending_user.key}))
gremmie@1 85
bgneal@74 86 msg = render_to_string('accounts/registration_email.txt',
bgneal@74 87 {
bgneal@74 88 'site_name' : site.name,
bgneal@74 89 'site_domain' : site.domain,
bgneal@74 90 'user_email' : pending_user.email,
bgneal@74 91 'activation_link' : activation_link,
bgneal@74 92 'username' : pending_user.username,
bgneal@74 93 'admin_email' : admin_email,
bgneal@74 94 })
gremmie@1 95
bgneal@74 96 subject = 'Registration Confirmation for ' + site.name
bgneal@74 97 send_mail(subject, msg, admin_email, [self.cleaned_data['email']])
bgneal@74 98 logging.info('Accounts/registration conf. email sent to %s for user %s; IP = %s' % \
bgneal@74 99 (self.cleaned_data['email'], pending_user.username, self.ip))
gremmie@1 100
bgneal@74 101 return pending_user
gremmie@1 102
bgneal@74 103 def _validation_error(self, msg, param=None):
bgneal@74 104 logging.error('Accounts/registration [%s]: %s (%s)' % (self.ip, msg, param))
bgneal@74 105 raise forms.ValidationError(msg)