Skip to content

Commit

Permalink
Add hCaptcha backend
Browse files Browse the repository at this point in the history
  • Loading branch information
Jazzzny committed Dec 31, 2024
1 parent 8cef2ca commit aa29e2c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 15 deletions.
20 changes: 20 additions & 0 deletions judge/utils/captcha.py
Original file line number Diff line number Diff line change
@@ -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
11 changes: 0 additions & 11 deletions judge/utils/recaptcha.py

This file was deleted.

10 changes: 7 additions & 3 deletions judge/views/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from judge.models import Language, Organization, Profile, TIMEZONE
from judge.utils.mail import validate_email_domain
from judge.utils.recaptcha import ReCaptchaField, ReCaptchaWidget
from judge.utils.captcha import CaptchaField, ReCaptchaWidget
from judge.utils.subscription import Subscription, newsletter_id
from judge.widgets import Select2MultipleWidget, Select2Widget

Expand All @@ -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():
Expand Down
2 changes: 1 addition & 1 deletion templates/registration/registration_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@
} catch (e) {}
});
</script>
{% if form.captcha %}
{% if form.captcha and form.is_recaptcha|default:false %}
{{ recaptcha_init(LANGUAGE_CODE) }}
{% endif %}
{% endblock %}
Expand Down

0 comments on commit aa29e2c

Please sign in to comment.