From 7927ab43186bc04d0ffe45f1d815d847618efb84 Mon Sep 17 00:00:00 2001 From: ethan Date: Fri, 7 May 2021 20:54:11 +0200 Subject: [PATCH 1/3] Added textcha functionality --- conference/accounts.py | 8 ++++++-- .../migrations/0030_auto_20210507_2047.py | 18 ++++++++++++++++++ conference/models.py | 7 ++++++- 3 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 conference/migrations/0030_auto_20210507_2047.py diff --git a/conference/accounts.py b/conference/accounts.py index 643d302d8..d3d01dc6f 100644 --- a/conference/accounts.py +++ b/conference/accounts.py @@ -1,4 +1,5 @@ import random +import re from django import forms from django.conf import settings @@ -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() @@ -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'] diff --git a/conference/migrations/0030_auto_20210507_2047.py b/conference/migrations/0030_auto_20210507_2047.py new file mode 100644 index 000000000..082069787 --- /dev/null +++ b/conference/migrations/0030_auto_20210507_2047.py @@ -0,0 +1,18 @@ +# Generated by Django 2.2.19 on 2021-05-07 18:47 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('conference', '0029_talk_availability'), + ] + + operations = [ + migrations.AlterField( + model_name='captchaquestion', + name='answer', + field=models.CharField(max_length=255, verbose_name='answer (use a regular expression\n to capture possible answers e.g.\n "(python|the python programming language)"\n case is ignored)\n '), + ), + ] diff --git a/conference/models.py b/conference/models.py index 5095e1c9b..78b60b80b 100644 --- a/conference/models.py +++ b/conference/models.py @@ -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() From 314acba0b7a5330593b5a560104a2578b2de9ba4 Mon Sep 17 00:00:00 2001 From: ethan Date: Fri, 7 May 2021 21:03:23 +0200 Subject: [PATCH 2/3] Built a test to reflect new textcha format --- tests/test_user_login_and_registration.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/test_user_login_and_registration.py b/tests/test_user_login_and_registration.py index 22623353b..abcc03e26 100644 --- a/tests/test_user_login_and_registration.py +++ b/tests/test_user_login_and_registration.py @@ -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") @@ -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, }, ) From 39432c2121d9c17cf0b0ecc2cdf458e2e10a8191 Mon Sep 17 00:00:00 2001 From: ethan Date: Sun, 4 Jul 2021 13:25:15 +0200 Subject: [PATCH 3/3] renamed migration; reformatted CaptchaQuestion answer verbose_name --- .../migrations/0030_auto_20210507_2047.py | 18 ------------------ ...uctions_to_textcha_question_verbose_name.py | 18 ++++++++++++++++++ conference/models.py | 10 +++++----- 3 files changed, 23 insertions(+), 23 deletions(-) delete mode 100644 conference/migrations/0030_auto_20210507_2047.py create mode 100644 conference/migrations/0032_add_instructions_to_textcha_question_verbose_name.py diff --git a/conference/migrations/0030_auto_20210507_2047.py b/conference/migrations/0030_auto_20210507_2047.py deleted file mode 100644 index 082069787..000000000 --- a/conference/migrations/0030_auto_20210507_2047.py +++ /dev/null @@ -1,18 +0,0 @@ -# Generated by Django 2.2.19 on 2021-05-07 18:47 - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('conference', '0029_talk_availability'), - ] - - operations = [ - migrations.AlterField( - model_name='captchaquestion', - name='answer', - field=models.CharField(max_length=255, verbose_name='answer (use a regular expression\n to capture possible answers e.g.\n "(python|the python programming language)"\n case is ignored)\n '), - ), - ] diff --git a/conference/migrations/0032_add_instructions_to_textcha_question_verbose_name.py b/conference/migrations/0032_add_instructions_to_textcha_question_verbose_name.py new file mode 100644 index 000000000..6aee86c5b --- /dev/null +++ b/conference/migrations/0032_add_instructions_to_textcha_question_verbose_name.py @@ -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)'), + ), + ] diff --git a/conference/models.py b/conference/models.py index 78b60b80b..f63e5ebcc 100644 --- a/conference/models.py +++ b/conference/models.py @@ -1345,11 +1345,11 @@ class NoQuestionsAvailable(Exception): question = 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) - ''') + 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()