From 683bdad0795d5226cec2dddb7e623b5c37fe33ed Mon Sep 17 00:00:00 2001 From: Ben Timby Date: Sat, 11 May 2024 05:07:38 -0400 Subject: [PATCH] Allow error to be silenced through django config setting. (#33) * Report error via return value. * Add note to README.md. --- README.md | 8 ++++++++ drf_recaptcha/checks.py | 8 +++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index b604624..edf7dbc 100644 --- a/README.md +++ b/README.md @@ -139,6 +139,14 @@ class MobileSerializer(Serializer): 2. the argument `secret_key` of field 3. request.context["recaptcha_secret_key"] +### Silence the check error + +If you need to disable the error, you can do so using the django settings. + +```python +SILENCED_SYSTEM_CHECKS = ['drf_recaptcha.checks.recaptcha_system_check'] +``` + ## reCAPTCHA v3 Validation is passed if the score value returned by Google is greater than or equal to required score. diff --git a/drf_recaptcha/checks.py b/drf_recaptcha/checks.py index 0166c9a..7800ff7 100644 --- a/drf_recaptcha/checks.py +++ b/drf_recaptcha/checks.py @@ -1,5 +1,5 @@ from django.conf import settings -from django.core.checks import Tags, Warning, register +from django.core.checks import Tags, Error, Warning, register from django.core.exceptions import ImproperlyConfigured from drf_recaptcha.constants import TEST_V2_SECRET_KEY @@ -15,9 +15,11 @@ def recaptcha_system_check(app_configs, **kwargs): secret_key = getattr(settings, "DRF_RECAPTCHA_SECRET_KEY", None) if not secret_key: - raise ImproperlyConfigured("settings.DRF_RECAPTCHA_SECRET_KEY must be set.") + errors.append( + Error("settings.DRF_RECAPTCHA_SECRET_KEY must be set."), + ) - if secret_key == TEST_V2_SECRET_KEY: + elif secret_key == TEST_V2_SECRET_KEY: errors.append( Warning( "Google test key for reCAPTCHA v2 is used now.\n"