diff 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
line wrap: on
line diff
--- a/gpp/accounts/forms.py	Mon Feb 28 01:58:38 2011 +0000
+++ b/gpp/accounts/forms.py	Mon Feb 28 03:53:04 2011 +0000
@@ -17,13 +17,13 @@
 
 class RegisterForm(forms.Form):
     """Form used to register with the website"""
-    username = forms.RegexField(max_length=30, regex = r'^\w+$',
+    username = forms.RegexField(max_length=30, regex=r'^\w+$',
         error_messages={
             'invalid': 'Your username must be 30 characters or less and ' \
                     'contain only letters, numbers and underscores.'})
     email = forms.EmailField()
-    password1 = forms.CharField(label = "Password", widget = forms.PasswordInput)
-    password2 = forms.CharField(label = "Password confirmation", widget = forms.PasswordInput)
+    password1 = forms.CharField(label="Password", widget=forms.PasswordInput)
+    password2 = forms.CharField(label="Password confirmation", widget=forms.PasswordInput)
     agree_age = forms.BooleanField(required=True,
         label='I certify that I am over the age of 13',
         error_messages={
@@ -40,6 +40,7 @@
         error_messages={
             'required': 'You have not agreed to our Privacy Policy.',
             })
+    question1 = forms.CharField(label="What number appears in the site name?")
 
     def __init__(self, *args, **kwargs):
         self.ip = kwargs.pop('ip', '?')
@@ -86,6 +87,20 @@
             self._validation_error("Please choose a password of 6 characters or more.")
         return password2
 
+    def clean_question1(self):
+        answer = self.cleaned_data.get('question1')
+        success = False
+        if answer:
+            try:
+                val = int(answer)
+            except ValueError:
+                pass
+            else:
+                success = val == 101
+        if not success:
+            self._validation_error("Incorrect answer to our anti-spam question.", answer)
+        return answer
+
     def save(self):
         pending_user = PendingUser.objects.create_pending_user(self.cleaned_data['username'],
                 self.cleaned_data['email'],