Skip to content
This repository has been archived by the owner on Apr 29, 2022. It is now read-only.

Issue 823 improve textcha for registration #1391

Open
wants to merge 3 commits into
base: ep2021
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions conference/accounts.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import random
import re

from django import forms
from django.conf import settings
Expand Down Expand Up @@ -217,6 +218,7 @@ class NewAccountForm(forms.Form):

# Additional captcha field with simple python questions
# https://github.com/EuroPython/epcon/issues/703
# https://github.com/EuroPython/epcon/issues/823
captcha_question = forms.CharField(widget=forms.HiddenInput)
captcha_answer = forms.CharField()

Expand All @@ -243,8 +245,10 @@ def get_random_captcha_question(self):

def clean_captcha_answer(self):
question = self.cleaned_data['captcha_question']
cq = CaptchaQuestion.objects.get(question=question)
if cq.answer.strip() != self.cleaned_data['captcha_answer'].strip():
answer = self.cleaned_data['captcha_answer'].strip()
correct_answers = CaptchaQuestion.objects.get(question=question).answer
answer_regx = re.compile(correct_answers, re.IGNORECASE)
if not answer_regx.match(answer):
raise forms.ValidationError("Sorry, that's a wrong answer")
return self.cleaned_data['captcha_question']

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.2.24 on 2021-07-04 10:59

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('conference', '0031_update_streamset_help_text'),
]

operations = [
migrations.AlterField(
model_name='captchaquestion',
name='answer',
field=models.CharField(max_length=255, verbose_name='answer (use a regular expression to capture possible answers e.g. "(python|the python programming language)" case is ignored)'),
),
]
7 changes: 6 additions & 1 deletion conference/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1344,7 +1344,12 @@ class NoQuestionsAvailable(Exception):
pass

question = models.CharField(max_length=255)
answer = models.CharField(max_length=255)
answer = models.CharField(max_length=255,
verbose_name='answer (use a regular expression'
' to capture possible answers e.g.'
' "(python|the python programming language)"'
' case is ignored)'
)
enabled = models.BooleanField(default=True)

objects = CaptchaQuestionManager()
Expand Down
8 changes: 5 additions & 3 deletions tests/test_user_login_and_registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,10 @@ def test_703_test_captcha_questions(client):
"""

QUESTION = "Can you foo in Python?"
ANSWER = "Yes you can"
CaptchaQuestion.objects.create(question=QUESTION, answer=ANSWER)
ANSWER = "(Yes|yeah|Definitely|CERTAINLY)"
CaptchaQuestion.objects.create(
question=QUESTION, answer=ANSWER
)
Email.objects.create(code="verify-account")

sign_up_url = reverse("accounts:signup_step_1_create_account")
Expand Down Expand Up @@ -246,7 +248,7 @@ def test_703_test_captcha_questions(client):
"password1": "password",
"password2": "password",
"captcha_question": QUESTION,
"captcha_answer": ANSWER,
"captcha_answer": "YEAH",
"i_accept_privacy_policy": True,
},
)
Expand Down