comparison gpp/accounts/forms.py @ 1:dbd703f7d63a

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