From 9296d409fbe0b331be0a6b196b52e62db7a3390b Mon Sep 17 00:00:00 2001 From: marek Date: Mon, 29 Jun 2020 15:34:36 +0200 Subject: [PATCH 1/2] replace tags with JS scripts when recaptcha is disabled - display empty string. Do not require keys for recaptcha when its disabled --- snowpenguin/django/recaptcha3/fields.py | 5 +++-- .../recaptcha3/templatetags/recaptcha3.py | 20 ++++++++++++++++--- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/snowpenguin/django/recaptcha3/fields.py b/snowpenguin/django/recaptcha3/fields.py index a9ce4de..b74873b 100644 --- a/snowpenguin/django/recaptcha3/fields.py +++ b/snowpenguin/django/recaptcha3/fields.py @@ -15,8 +15,9 @@ class ReCaptchaField(forms.CharField): def __init__(self, attrs=None, *args, **kwargs): - self._private_key = kwargs.pop('private_key', settings.RECAPTCHA_PRIVATE_KEY) - self._score_threshold = kwargs.pop('score_threshold', settings.RECAPTCHA_SCORE_THRESHOLD) + if os.environ.get('RECAPTCHA_DISABLE', None) is not None: + self._private_key = kwargs.pop('private_key', settings.RECAPTCHA_PRIVATE_KEY) + self._score_threshold = kwargs.pop('score_threshold', settings.RECAPTCHA_SCORE_THRESHOLD) if 'widget' not in kwargs: kwargs['widget'] = ReCaptchaHiddenInput() diff --git a/snowpenguin/django/recaptcha3/templatetags/recaptcha3.py b/snowpenguin/django/recaptcha3/templatetags/recaptcha3.py index f0c6f61..ccc2d28 100644 --- a/snowpenguin/django/recaptcha3/templatetags/recaptcha3.py +++ b/snowpenguin/django/recaptcha3/templatetags/recaptcha3.py @@ -1,5 +1,8 @@ +import os + from django import template from django.conf import settings +from django.template.loader import get_template register = template.Library() @@ -9,7 +12,6 @@ def recaptcha_key(): return settings.RECAPTCHA_PUBLIC_KEY -@register.inclusion_tag('snowpenguin/recaptcha/recaptcha_init.html') def recaptcha_init(public_key=None): return { @@ -19,7 +21,6 @@ def recaptcha_init(public_key=None): } -@register.inclusion_tag('snowpenguin/recaptcha/recaptcha_ready.html') def recaptcha_ready(public_key=None, action_name=None, custom_callback=None): return { '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): } -@register.inclusion_tag('snowpenguin/recaptcha/recaptcha_execute.html') def recaptcha_execute(public_key=None, action_name=None, custom_callback=None): return { 'public_key': public_key or settings.RECAPTCHA_PUBLIC_KEY, 'action_name': action_name or settings.RECAPTCHA_DEFAULT_ACTION, 'custom_callback': custom_callback } + + +def return_empty_context(*args, **kwargs): + return '' + + +if not os.environ.get('RECAPTCHA_DISABLE', None): + register.inclusion_tag(get_template('snowpenguin/recaptcha/recaptcha_init.html'))(recaptcha_init) + register.inclusion_tag(get_template('snowpenguin/recaptcha/recaptcha_ready.html'))(recaptcha_ready) + register.inclusion_tag(get_template('snowpenguin/recaptcha/recaptcha_execute.html'))(recaptcha_execute) +else: + register.simple_tag(return_empty_context, name='recaptcha_init') + register.simple_tag(return_empty_context, name='recaptcha_ready') + register.simple_tag(return_empty_context, name='recaptcha_execute') From 69219ca34dc834fede05f4e7f107ba17156f8f45 Mon Sep 17 00:00:00 2001 From: marek Date: Tue, 30 Jun 2020 09:55:35 +0200 Subject: [PATCH 2/2] fix if statement for RECAPTCHA DISABLED --- snowpenguin/django/recaptcha3/fields.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snowpenguin/django/recaptcha3/fields.py b/snowpenguin/django/recaptcha3/fields.py index b74873b..0deed54 100644 --- a/snowpenguin/django/recaptcha3/fields.py +++ b/snowpenguin/django/recaptcha3/fields.py @@ -15,7 +15,7 @@ class ReCaptchaField(forms.CharField): def __init__(self, attrs=None, *args, **kwargs): - if os.environ.get('RECAPTCHA_DISABLE', None) is not None: + if os.environ.get('RECAPTCHA_DISABLE', None) is None: self._private_key = kwargs.pop('private_key', settings.RECAPTCHA_PRIVATE_KEY) self._score_threshold = kwargs.pop('score_threshold', settings.RECAPTCHA_SCORE_THRESHOLD)