Skip to content

Commit

Permalink
Add hCaptcha backend
Browse files Browse the repository at this point in the history
  • Loading branch information
Jazzzny committed Jan 20, 2024
1 parent 61318c7 commit e56fdda
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
10 changes: 10 additions & 0 deletions judge/utils/hcaptcha.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
try:
from hcaptcha_field import hCaptchaField, hCaptchaWidget
except ImportError:
hCaptchaField = None
hCaptchaWidget = None
else:
from django.conf import settings
if not hasattr(settings, 'HCAPTCHA_SECRET'):
hCaptchaField = None
hCaptchaWidget = None
6 changes: 5 additions & 1 deletion judge/views/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,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.hcaptcha import hCaptchaField, hCaptchaWidget
from judge.utils.subscription import Subscription, newsletter_id
from judge.widgets import Select2MultipleWidget, Select2Widget

Expand All @@ -34,7 +35,10 @@ class CustomRegistrationForm(RegistrationForm):
newsletter = forms.BooleanField(label=_('Subscribe to newsletter?'), initial=True, required=False)

if ReCaptchaField is not None:
captcha = ReCaptchaField(widget=ReCaptchaWidget())
recaptcha = ReCaptchaField(widget=ReCaptchaWidget())

if hCaptchaField is not None:
hcaptcha = hCaptchaField()

def clean_email(self):
if User.objects.filter(email=self.cleaned_data['email']).exists():
Expand Down
17 changes: 12 additions & 5 deletions 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.recaptcha %}
{{ recaptcha_init(LANGUAGE_CODE) }}
{% endif %}
{% endblock %}
Expand Down Expand Up @@ -196,10 +196,17 @@
</div>
{% endif %}

{% if form.captcha %}
<div style="margin-top: 0.5em">{{ form.captcha }}</div>
{% if form.captcha.errors %}
<div class="form-field-error">{{ form.captcha.errors }}</div>
{% if form.recaptcha %}
<div style="margin-top: 0.5em">{{ form.recaptcha }}</div>
{% if form.recaptcha.errors %}
<div class="form-field-error">{{ form.recaptcha.errors }}</div>
{% endif %}
{% endif %}

{% if form.hcaptcha %}
<div style="margin-top: 0.5em">{{ form.hcaptcha }}</div>
{% if form.hcaptcha.errors %}
<div class="form-field-error">{{ form.hcaptcha.errors }}</div>
{% endif %}
{% endif %}

Expand Down

0 comments on commit e56fdda

Please sign in to comment.