From 95dd1d3367b77641a7be8c8481e7f6b5c3ab0f75 Mon Sep 17 00:00:00 2001 From: Christian Wiegand Date: Sun, 2 Apr 2023 12:30:02 +0200 Subject: [PATCH] Include friendly captcha javascript assets --- CHANGES.rst | 8 ++++++++ README.rst | 18 +++++++++++++----- friendly_captcha/widgets.py | 21 +++++++++++++++++++-- setup.py | 3 ++- 4 files changed, 42 insertions(+), 8 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 843b0bb..8899cc0 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,3 +1,11 @@ +Version 0.1.7 (2023-04-02) +-------------------------- + +* Changed: As of version 0.1.7 the friendly captcha javascript assets are + included in the widget, so no need to include them in the templates. + For more information see the docs in the README.rst. + + Version 0.1.6 (2022-11-21) -------------------------- diff --git a/README.rst b/README.rst index 9b4cb5a..705ed96 100644 --- a/README.rst +++ b/README.rst @@ -55,7 +55,19 @@ Add the captcha field to your form: captcha = FrcCaptchaField() -Add the script tags from Friendly Captcha to your forms template +As of version 0.1.7 the javascript static assets are included in +the widget, so there is no need to do that in your project templates. +Version 0.1.7 includes friendly captcha version 0.9.11 javascript files. +If you need a different version you can set these by providing +them in your settings: + +.. code-block:: + + FRC_WIDGET_MODULE_JS = 'https://unpkg.com/friendly-challenge@0.9.8/widget.module.min.js' + FRC_WIDGET_JS = 'https://unpkg.com/friendly-challenge@0.9.8/widget.min.js' + +For version 0.1.6 and below you need to include the script tags from +Friendly Captcha to your forms template (see https://docs.friendlycaptcha.com/#/installation) .. code-block:: @@ -63,10 +75,6 @@ Add the script tags from Friendly Captcha to your forms template -I thought about adding these static assets as form media assets, but -users wouldn't be able to choose the desired version. So I decided -against for now. - If you build up your form from single fields, dont't forget to include the captcha form field. diff --git a/friendly_captcha/widgets.py b/friendly_captcha/widgets.py index d133abe..1ab3d5b 100644 --- a/friendly_captcha/widgets.py +++ b/friendly_captcha/widgets.py @@ -3,14 +3,31 @@ from django.utils import translation from django.utils.html import format_html from django.forms.utils import flatatt +from django.utils.safestring import mark_safe class FrcCaptchaWidget(Widget): + def __init__(self): + super().__init__() + self.site_key = getattr(settings, 'FRC_CAPTCHA_SITE_KEY', None) + self.frc_widget_module_js = getattr( + settings, 'FRC_WIDGET_MODULE_JS', + 'https://unpkg.com/friendly-challenge@0.9.11/widget.module.min.js', + ) + self.frc_widget_js = ( + settings, 'FRC_WIDGET_JS', + 'https://unpkg.com/friendly-challenge@0.9.11/widget.min.js' + ) + def render(self, name, value, attrs=None, renderer=None): attrs['data-lang'] = translation.get_language() - attrs['data-sitekey'] = getattr(settings, 'FRC_CAPTCHA_SITE_KEY', None) + attrs['data-sitekey'] = self.site_key attrs['data-solution-field-name'] = name attrs['class'] = 'frc-captcha' final_attrs = self.build_attrs(attrs) - return format_html('
', flatatt(final_attrs)) + frc_js = mark_safe( + f'' + f'' + ) + return format_html('
{}', flatatt(final_attrs), frc_js) diff --git a/setup.py b/setup.py index 4d9accb..4784d1f 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ def read(fname): setup( name='django-friendly-captcha', - version='0.1.6', + version='0.1.7', description='Django library for friendly captcha', long_description=read('README.rst'), url='https://github.com/christianwgd/django-friendly-captcha', @@ -37,6 +37,7 @@ def read(fname): 'Programming Language :: Python :: 3.7', 'Programming Language :: Python :: 3.8', 'Programming Language :: Python :: 3.9', + 'Programming Language :: Python :: 3.10', ], zip_safe=False, )