Repository: ocadotechnology/codeforlife-portal

Issue:
Check security item 3 validity



Discussion:

Resolving patch:
diff --git a/portal/forms/play.py b/portal/forms/play.py
--- a/portal/forms/play.py
+++ b/portal/forms/play.py
@@ -168,6 +168,8 @@ class StudentSignupForm(forms.Form):
         label='Confirm Password',
         widget=forms.PasswordInput)
 
+    captcha = ReCaptchaField()
+
     def clean_name(self):
         name = self.cleaned_data.get('name', None)
         if re.match(re.compile('^[\w ]+$'), name) is None:
diff --git a/portal/forms/teach.py b/portal/forms/teach.py
--- a/portal/forms/teach.py
+++ b/portal/forms/teach.py
@@ -95,6 +95,8 @@ class TeacherSignupForm(forms.Form):
         widget=forms.PasswordInput()
     )
 
+    captcha = ReCaptchaField()
+
     def clean_teacher_password(self):
         password = self.cleaned_data.get('teacher_password', None)
 
diff --git a/portal/views/home.py b/portal/views/home.py
--- a/portal/views/home.py
+++ b/portal/views/home.py
@@ -167,6 +167,9 @@ def configure_login_form_captcha(form, render_dict, render_dict_captcha_key):
 @ratelimit('name', labeller=play_name_labeller, ip=False, periods=['1m'], increment=lambda req, res: hasattr(res, 'count') and res.count)
 def render_signup_form(request):
     invalid_form = False
+    limits = getattr(request, 'limits', {'ip': [0]})
+    captcha_limit = 5
+    should_use_captcha = (limits['ip'][0] >= captcha_limit) and captcha.CAPTCHA_ENABLED
 
     teacher_signup_form = TeacherSignupForm(prefix='teacher_signup')
     student_signup_form = StudentSignupForm(prefix='student_signup')
@@ -174,12 +177,20 @@ def render_signup_form(request):
     if request.method == 'POST':
         if 'teacher_signup' in request.POST:
             teacher_signup_form = TeacherSignupForm(request.POST, prefix='teacher_signup')
+
+            if not should_use_captcha:
+                remove_captcha_from_forms(teacher_signup_form)
+
             if teacher_signup_form.is_valid():
                 data = teacher_signup_form.cleaned_data
                 return process_signup_form(request, data)
 
         else:
             student_signup_form = StudentSignupForm(request.POST, prefix='student_signup')
+
+            if not should_use_captcha:
+                remove_captcha_from_forms(student_signup_form)
+
             if student_signup_form.is_valid():
                 data = student_signup_form.cleaned_data
                 return process_student_signup_form(request, data)
@@ -187,6 +198,8 @@ def render_signup_form(request):
     res = render(request, 'portal/register.html', {
         'teacher_signup_form': teacher_signup_form,
         'student_signup_form': student_signup_form,
+        'captcha': should_use_captcha,
+
     })
 
     res.count = invalid_form
@@ -324,9 +337,7 @@ def redirect_user_to_correct_page(request, teacher):
 @ratelimit('ip', periods=['1m'], increment=lambda req, res: hasattr(res, 'count') and res.count)
 def contact(request):
     increment_count = False
-    limits = getattr(request, 'limits', {'ip': [0]})
-    captcha_limit = 5
-    should_use_captcha = (limits['ip'][0] >= captcha_limit) and captcha.CAPTCHA_ENABLED
+    should_use_captcha = captcha.CAPTCHA_ENABLED
 
     anchor = ''
 


Test patch: