comparison 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
comparison
equal deleted inserted replaced
345:f7fbb404241f 346:efa3b4901777
15 from accounts.models import IllegalEmail 15 from accounts.models import IllegalEmail
16 16
17 17
18 class RegisterForm(forms.Form): 18 class RegisterForm(forms.Form):
19 """Form used to register with the website""" 19 """Form used to register with the website"""
20 username = forms.RegexField(max_length=30, regex = r'^\w+$', 20 username = forms.RegexField(max_length=30, regex=r'^\w+$',
21 error_messages={ 21 error_messages={
22 'invalid': 'Your username must be 30 characters or less and ' \ 22 'invalid': 'Your username must be 30 characters or less and ' \
23 'contain only letters, numbers and underscores.'}) 23 'contain only letters, numbers and underscores.'})
24 email = forms.EmailField() 24 email = forms.EmailField()
25 password1 = forms.CharField(label = "Password", widget = forms.PasswordInput) 25 password1 = forms.CharField(label="Password", widget=forms.PasswordInput)
26 password2 = forms.CharField(label = "Password confirmation", widget = forms.PasswordInput) 26 password2 = forms.CharField(label="Password confirmation", widget=forms.PasswordInput)
27 agree_age = forms.BooleanField(required=True, 27 agree_age = forms.BooleanField(required=True,
28 label='I certify that I am over the age of 13', 28 label='I certify that I am over the age of 13',
29 error_messages={ 29 error_messages={
30 'required': 'Sorry, but you must be over the age of 13 to ' \ 30 'required': 'Sorry, but you must be over the age of 13 to ' \
31 'register at our site.', 31 'register at our site.',
38 agree_privacy = forms.BooleanField(required=True, 38 agree_privacy = forms.BooleanField(required=True,
39 label='I agree to the Privacy Policy', 39 label='I agree to the Privacy Policy',
40 error_messages={ 40 error_messages={
41 'required': 'You have not agreed to our Privacy Policy.', 41 'required': 'You have not agreed to our Privacy Policy.',
42 }) 42 })
43 question1 = forms.CharField(label="What number appears in the site name?")
43 44
44 def __init__(self, *args, **kwargs): 45 def __init__(self, *args, **kwargs):
45 self.ip = kwargs.pop('ip', '?') 46 self.ip = kwargs.pop('ip', '?')
46 super(RegisterForm, self).__init__(*args, **kwargs) 47 super(RegisterForm, self).__init__(*args, **kwargs)
47 48
84 self._validation_error("The two password fields didn't match.") 85 self._validation_error("The two password fields didn't match.")
85 if len(password1) < 6: 86 if len(password1) < 6:
86 self._validation_error("Please choose a password of 6 characters or more.") 87 self._validation_error("Please choose a password of 6 characters or more.")
87 return password2 88 return password2
88 89
90 def clean_question1(self):
91 answer = self.cleaned_data.get('question1')
92 success = False
93 if answer:
94 try:
95 val = int(answer)
96 except ValueError:
97 pass
98 else:
99 success = val == 101
100 if not success:
101 self._validation_error("Incorrect answer to our anti-spam question.", answer)
102 return answer
103
89 def save(self): 104 def save(self):
90 pending_user = PendingUser.objects.create_pending_user(self.cleaned_data['username'], 105 pending_user = PendingUser.objects.create_pending_user(self.cleaned_data['username'],
91 self.cleaned_data['email'], 106 self.cleaned_data['email'],
92 self.cleaned_data['password1']) 107 self.cleaned_data['password1'])
93 108