annotate gpp/accounts/forms.py @ 51:e5d078ebe870

Send email based on setting in local_settings.py.
author Brian Neal <bgneal@gmail.com>
date Sun, 21 Jun 2009 22:07:43 +0000
parents 53b7c681d80b
children df56795771a6
rev   line source
gremmie@1 1 """forms for the accounts application"""
gremmie@1 2
gremmie@1 3 from django import forms
gremmie@1 4 from django.contrib.auth.models import User
gremmie@1 5 from django.core.urlresolvers import reverse
gremmie@1 6 from django.template.loader import render_to_string
gremmie@1 7 from django.contrib.sites.models import Site
bgneal@6 8 from django.conf import settings
gremmie@1 9
gremmie@1 10 from core.functions import send_mail
gremmie@1 11 from accounts.models import PendingUser
gremmie@1 12 from accounts.models import IllegalUsername
gremmie@1 13 from accounts.models import IllegalEmail
gremmie@1 14
gremmie@1 15
gremmie@1 16 class RegisterForm(forms.Form):
gremmie@1 17 """Form used to register with the website"""
gremmie@1 18 username = forms.RegexField(max_length = 30, regex = r'^\w+$',
gremmie@1 19 error_messages = {'invalid' : 'Your username must be 30 characters or less and contain only letters, numbers and underscores.'})
gremmie@1 20 email = forms.EmailField()
gremmie@1 21 password1 = forms.CharField(label = "Password", widget = forms.PasswordInput)
gremmie@1 22 password2 = forms.CharField(label = "Password confirmation", widget = forms.PasswordInput)
gremmie@1 23 agree_tos = forms.BooleanField(required = True, label = 'I agree to the Terms of Service',
gremmie@1 24 error_messages = {'required' : 'You have not agreed to our Terms of Service'})
gremmie@1 25 agree_privacy = forms.BooleanField(required = True, label = 'I agree to the Privacy Policy',
gremmie@1 26 error_messages = {'required' : 'You have not agreed to our Privacy Policy'})
gremmie@1 27
gremmie@1 28 def clean_username(self):
gremmie@1 29 username = self.cleaned_data['username']
gremmie@1 30 try:
gremmie@1 31 User.objects.get(username = username)
gremmie@1 32 except User.DoesNotExist:
gremmie@1 33 try:
gremmie@1 34 PendingUser.objects.get(username = username)
gremmie@1 35 except PendingUser.DoesNotExist:
gremmie@1 36 try:
gremmie@1 37 IllegalUsername.objects.get(username = username)
gremmie@1 38 except IllegalUsername.DoesNotExist:
gremmie@1 39 return username
gremmie@1 40 raise forms.ValidationError("That username is not allowed.")
gremmie@1 41 raise forms.ValidationError("A pending user with that username already exists.")
gremmie@1 42 raise forms.ValidationError("A user with that username already exists.")
gremmie@1 43
gremmie@1 44 def clean_email(self):
gremmie@1 45 email = self.cleaned_data['email']
gremmie@1 46 try:
gremmie@1 47 User.objects.get(email = email)
gremmie@1 48 except User.DoesNotExist:
gremmie@1 49 try:
gremmie@1 50 PendingUser.objects.get(email = email)
gremmie@1 51 except PendingUser.DoesNotExist:
gremmie@1 52 try:
gremmie@1 53 IllegalEmail.objects.get(email = email)
gremmie@1 54 except IllegalEmail.DoesNotExist:
gremmie@1 55 return email
gremmie@1 56 raise forms.ValidationError("That email address is not allowed.")
gremmie@1 57 raise forms.ValidationError("A pending user with that email already exists.")
gremmie@1 58 raise forms.ValidationError("A user with that email already exists.")
gremmie@1 59
gremmie@1 60 def clean_password2(self):
gremmie@1 61 password1 = self.cleaned_data.get("password1", "")
gremmie@1 62 password2 = self.cleaned_data["password2"]
gremmie@1 63 if password1 != password2:
gremmie@1 64 raise forms.ValidationError("The two password fields didn't match.")
gremmie@1 65 return password2
gremmie@1 66
gremmie@1 67 def save(self):
gremmie@1 68 pending_user = PendingUser.objects.create_pending_user(self.cleaned_data['username'],
gremmie@1 69 self.cleaned_data['email'],
gremmie@1 70 self.cleaned_data['password1'])
gremmie@1 71
gremmie@1 72 # Send the confirmation email
gremmie@1 73
gremmie@1 74 site = Site.objects.get_current()
bgneal@2 75 admin_email = settings.ADMINS[0][1]
gremmie@1 76
gremmie@1 77 activation_link = 'http://%s%s' % (site.domain, reverse('accounts.views.register_confirm',
gremmie@1 78 kwargs = {'username' : pending_user.username, 'key' : pending_user.key}))
gremmie@1 79
gremmie@1 80 msg = render_to_string('accounts/registration_email.txt',
gremmie@1 81 {
gremmie@1 82 'site_name' : site.name,
gremmie@1 83 'site_domain' : site.domain,
gremmie@1 84 'user_email' : pending_user.email,
gremmie@1 85 'activation_link' : activation_link,
gremmie@1 86 'username' : pending_user.username,
bgneal@2 87 'admin_email' : admin_email,
gremmie@1 88 })
gremmie@1 89
gremmie@1 90 subject = 'Registration Confirmation for ' + site.name
bgneal@2 91 send_mail(subject, msg, admin_email, [self.cleaned_data['email']])
gremmie@1 92
gremmie@1 93 return pending_user
gremmie@1 94