Skip to content

Commit

Permalink
invisible recaptcha as a template tag instead of a widget.
Browse files Browse the repository at this point in the history
  • Loading branch information
kbytesys committed Aug 20, 2018
1 parent 7a28f5e commit 369a392
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 34 deletions.
6 changes: 4 additions & 2 deletions README.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
This integration app implements a recaptcha field for Google reCaptcha v2
with explicit rendering and multiple recaptcha support.
This integration app implements a reCAPTCHA field for Google reCAPTCHA v2
with explicit rendering and multiple reCAPTCHA support.

It supports also the invisible reCAPTCHA with the automatic render mode.
28 changes: 28 additions & 0 deletions samples/invisible_recaptcha.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{% load recaptcha2 %}
<html>
<head>
{% recaptcha_init %}
</head>
<body>
<form id='myform' action="?" method="POST">
{% csrf_token %}
{{ form }}
{% recaptcha_invisible_button submit_label='Submit' %}
</form>
<form id='myform2' action="?" method="POST">
{% csrf_token %}
{{ form }}
{% recaptcha_invisible_button submit_label='Submit' form_id='myform2' %}
</form>
<form id='myform3' action="?" method="POST">
{% csrf_token %}
{{ form }}
{% recaptcha_invisible_button submit_label='Submit' custom_callback='mycallback' %}
<script>
function mycallback(token) {
document.getElementById("myform3").submit();
}
</script>
</form>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
<button id="recaptcha-invisible-{{ generated_id }}" class="g-recaptcha{% if extra_css_classes %}{{ extra_css_classes }}{% endif %}" data-sitekey="{{ public_key }}" data-callback="recaptcha_onSubmit_{{ generated_id }}">{{ submit_label }}</button>
<script>
<button id="recaptcha-invisible-{{ generated_id }}" class="g-recaptcha{% if extra_css_classes %}{{ extra_css_classes }}{% endif %}" data-sitekey="{{ public_key }}" data-callback="{% if custom_callback %}{{ custom_callback }}{% endif %}{% if not custom_callback %}recaptcha_onSubmit_{{ generated_id }}{% endif %}">{{ submit_label }}</button>
{% if not custom_callback %}<script>
function recaptcha_onSubmit_{{ generated_id }}() {
{% if form_id %}
document.getElementById("{{ form_id }}").submit();
{% endif %}
{% if form_id %}document.getElementById("{{ form_id }}").submit();{% endif %}
{% if not form_id %}
var form_element = document.getElementById("recaptcha-invisible-{{ generated_id }}").closest("form");
if (form_element) { form_element.submit(); }
{% endif %}
}
</script>
</script>{% endif %}
18 changes: 18 additions & 0 deletions snowpenguin/django/recaptcha2/templatetags/recaptcha2.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from random import randint

from django import template
from django.conf import settings
from django.utils.translation import ugettext_lazy as _

register = template.Library()

Expand All @@ -22,3 +25,18 @@ def recaptcha_explicit_init(language=None):
@register.inclusion_tag('snowpenguin/recaptcha/recaptcha_explicit_support.html')
def recaptcha_explicit_support():
return {}


@register.inclusion_tag('snowpenguin/recaptcha/recaptcha_invisible_button.html')
def recaptcha_invisible_button(public_key=None, submit_label=None, extra_css_classes=None, form_id=None,
custom_callback=None):
generated_id = '%s' % randint(10000, 99999)

return {
'generated_id': generated_id,
'public_key': public_key or settings.RECAPTCHA_PUBLIC_KEY,
'form_id': form_id,
'submit_label': submit_label or _('Submit'),
'extra_css_classes': extra_css_classes,
'custom_callback': custom_callback
}
30 changes: 5 additions & 25 deletions snowpenguin/django/recaptcha2/widgets.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from random import randint

from django.conf import settings
from django.forms.widgets import Widget
from django.forms.widgets import Widget, Input
from django.template.loader import render_to_string
from django.utils.safestring import mark_safe
from django.utils.translation import gettext as _


class ReCaptchaWidget(Widget):
Expand Down Expand Up @@ -49,30 +48,11 @@ def value_from_datadict(self, data, files, name):
return [data.get('g-recaptcha-response', None)]


class ReCaptchaInvisibleWidget(Widget):
def __init__(self, public_key=None, submit_label=_('Submit'), extra_css_classes=None, form_id=None, attrs={},
*args, **kwargs):
super(ReCaptchaInvisibleWidget, self).__init__(*args, **kwargs)
self.attrs = attrs
self._public_key = public_key
self.submit_label = submit_label
self.extra_css_classes = extra_css_classes
self.form_id = form_id

def render(self, name, value, attrs=None, *args, **kwargs):
template = 'snowpenguin/recaptcha/recaptcha_invisible_automatic.html'

generated_id = '%s' % randint(10000, 99999)
class ReCaptchaInvisibleWidget(Input):
input_type = 'hidden'

return mark_safe(
render_to_string(template, {
'generated_id': generated_id,
'public_key': self._public_key or settings.RECAPTCHA_PUBLIC_KEY,
'form_id': self.form_id,
'submit_label': self.submit_label,
'extra_css_classes': self.extra_css_classes
})
)
def render(self, name, value, attrs=None, renderer=None):
return ''

def value_from_datadict(self, data, files, name):
return [data.get('g-recaptcha-response', None)]
4 changes: 3 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ basepython =
py35: python3.5
py36: python3.6
py37: python3.7
commands=python setup.py test
commands=
pip install git+git://github.com/kbytesys/django-setuptest.git@feature/pep8_config
python setup.py test
deps =
1.8: Django>=1.8,<1.9
1.9: Django>=1.9,<1.10
Expand Down

0 comments on commit 369a392

Please sign in to comment.