Skip to content

Commit c89a7c0

Browse files
authored
Merge pull request #16 from MOwsianowski/improve_turn_off_recaptcha
RECAPTCHA_DISABLE improve
2 parents cc38802 + 69219ca commit c89a7c0

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

snowpenguin/django/recaptcha3/fields.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515

1616
class ReCaptchaField(forms.CharField):
1717
def __init__(self, attrs=None, *args, **kwargs):
18-
self._private_key = kwargs.pop('private_key', settings.RECAPTCHA_PRIVATE_KEY)
19-
self._score_threshold = kwargs.pop('score_threshold', settings.RECAPTCHA_SCORE_THRESHOLD)
18+
if os.environ.get('RECAPTCHA_DISABLE', None) is None:
19+
self._private_key = kwargs.pop('private_key', settings.RECAPTCHA_PRIVATE_KEY)
20+
self._score_threshold = kwargs.pop('score_threshold', settings.RECAPTCHA_SCORE_THRESHOLD)
2021

2122
if 'widget' not in kwargs:
2223
kwargs['widget'] = ReCaptchaHiddenInput()

snowpenguin/django/recaptcha3/templatetags/recaptcha3.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import os
2+
13
from django import template
24
from django.conf import settings
5+
from django.template.loader import get_template
36

47
register = template.Library()
58

@@ -9,7 +12,6 @@ def recaptcha_key():
912
return settings.RECAPTCHA_PUBLIC_KEY
1013

1114

12-
@register.inclusion_tag('snowpenguin/recaptcha/recaptcha_init.html')
1315
def recaptcha_init(public_key=None):
1416

1517
return {
@@ -19,7 +21,6 @@ def recaptcha_init(public_key=None):
1921
}
2022

2123

22-
@register.inclusion_tag('snowpenguin/recaptcha/recaptcha_ready.html')
2324
def recaptcha_ready(public_key=None, action_name=None, custom_callback=None):
2425
return {
2526
'public_key': public_key or settings.RECAPTCHA_PUBLIC_KEY,
@@ -28,10 +29,23 @@ def recaptcha_ready(public_key=None, action_name=None, custom_callback=None):
2829
}
2930

3031

31-
@register.inclusion_tag('snowpenguin/recaptcha/recaptcha_execute.html')
3232
def recaptcha_execute(public_key=None, action_name=None, custom_callback=None):
3333
return {
3434
'public_key': public_key or settings.RECAPTCHA_PUBLIC_KEY,
3535
'action_name': action_name or settings.RECAPTCHA_DEFAULT_ACTION,
3636
'custom_callback': custom_callback
3737
}
38+
39+
40+
def return_empty_context(*args, **kwargs):
41+
return ''
42+
43+
44+
if not os.environ.get('RECAPTCHA_DISABLE', None):
45+
register.inclusion_tag(get_template('snowpenguin/recaptcha/recaptcha_init.html'))(recaptcha_init)
46+
register.inclusion_tag(get_template('snowpenguin/recaptcha/recaptcha_ready.html'))(recaptcha_ready)
47+
register.inclusion_tag(get_template('snowpenguin/recaptcha/recaptcha_execute.html'))(recaptcha_execute)
48+
else:
49+
register.simple_tag(return_empty_context, name='recaptcha_init')
50+
register.simple_tag(return_empty_context, name='recaptcha_ready')
51+
register.simple_tag(return_empty_context, name='recaptcha_execute')

0 commit comments

Comments
 (0)