From 7ef055d6eb448766fda0c03377e2da53bfaa4b31 Mon Sep 17 00:00:00 2001 From: Jazzzny Date: Mon, 30 Dec 2024 21:56:10 -0500 Subject: [PATCH] Add hCaptcha backend Satisfy lint --- judge/utils/captcha.py | 20 +++++++++++++++++++ judge/utils/recaptcha.py | 11 ---------- judge/views/register.py | 10 +++++++--- templates/registration/registration_form.html | 2 +- 4 files changed, 28 insertions(+), 15 deletions(-) create mode 100644 judge/utils/captcha.py delete mode 100644 judge/utils/recaptcha.py diff --git a/judge/utils/captcha.py b/judge/utils/captcha.py new file mode 100644 index 0000000000..be6ddcf301 --- /dev/null +++ b/judge/utils/captcha.py @@ -0,0 +1,20 @@ +try: + from snowpenguin.django.recaptcha2.fields import ReCaptchaField + from snowpenguin.django.recaptcha2.widgets import ReCaptchaWidget +except ImportError: + ReCaptchaField = None + ReCaptchaWidget = None +try: + from hcaptcha_field import hCaptchaField +except ImportError: + hCaptchaField = None + +from django.conf import settings + +if not hasattr(settings, 'RECAPTCHA_PRIVATE_KEY') and ReCaptchaField is not None: + ReCaptchaField = None + ReCaptchaWidget = None +elif not hasattr(settings, 'HCAPTCHA_SECRET') and hCaptchaField is not None: + hCaptchaField = None + +CaptchaField = ReCaptchaField or hCaptchaField diff --git a/judge/utils/recaptcha.py b/judge/utils/recaptcha.py deleted file mode 100644 index 331a12d0b7..0000000000 --- a/judge/utils/recaptcha.py +++ /dev/null @@ -1,11 +0,0 @@ -try: - from snowpenguin.django.recaptcha2.fields import ReCaptchaField - from snowpenguin.django.recaptcha2.widgets import ReCaptchaWidget -except ImportError: - ReCaptchaField = None - ReCaptchaWidget = None -else: - from django.conf import settings - if not hasattr(settings, 'RECAPTCHA_PRIVATE_KEY'): - ReCaptchaField = None - ReCaptchaWidget = None diff --git a/judge/views/register.py b/judge/views/register.py index f5cdd64029..9c34972521 100644 --- a/judge/views/register.py +++ b/judge/views/register.py @@ -12,8 +12,8 @@ from sortedm2m.forms import SortedMultipleChoiceField from judge.models import Language, Organization, Profile, TIMEZONE +from judge.utils.captcha import CaptchaField, ReCaptchaWidget from judge.utils.mail import validate_email_domain -from judge.utils.recaptcha import ReCaptchaField, ReCaptchaWidget from judge.utils.subscription import Subscription, newsletter_id from judge.widgets import Select2MultipleWidget, Select2Widget @@ -33,8 +33,12 @@ class CustomRegistrationForm(RegistrationForm): if newsletter_id is not None: newsletter = forms.BooleanField(label=_('Subscribe to newsletter?'), initial=True, required=False) - if ReCaptchaField is not None: - captcha = ReCaptchaField(widget=ReCaptchaWidget()) + if CaptchaField is not None: + if ReCaptchaWidget is not None: + captcha = CaptchaField(widget=ReCaptchaWidget()) + is_recaptcha = True + else: + captcha = CaptchaField() def clean_email(self): if User.objects.filter(email=self.cleaned_data['email']).exists(): diff --git a/templates/registration/registration_form.html b/templates/registration/registration_form.html index 494d8def50..8153f5ccd8 100644 --- a/templates/registration/registration_form.html +++ b/templates/registration/registration_form.html @@ -126,7 +126,7 @@ } catch (e) {} }); - {% if form.captcha %} + {% if form.captcha and form.is_recaptcha|default:false %} {{ recaptcha_init(LANGUAGE_CODE) }} {% endif %} {% endblock %}