Skip to content

Commit

Permalink
Include friendly captcha javascript assets
Browse files Browse the repository at this point in the history
  • Loading branch information
christianwgd committed Apr 2, 2023
1 parent 368ff4a commit 95dd1d3
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 8 deletions.
8 changes: 8 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -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)
--------------------------

Expand Down
18 changes: 13 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,26 @@ 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/[email protected]/widget.module.min.js'
FRC_WIDGET_JS = 'https://unpkg.com/[email protected]/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::
<script type="module" src="https://unpkg.com/[email protected]/widget.module.min.js" async defer></script>
<script nomodule src="https://unpkg.com/[email protected]/widget.min.js" async defer></script>
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.

Expand Down
21 changes: 19 additions & 2 deletions friendly_captcha/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]/widget.module.min.js',
)
self.frc_widget_js = (
settings, 'FRC_WIDGET_JS',
'https://unpkg.com/[email protected]/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('<div {}></div>', flatatt(final_attrs))
frc_js = mark_safe(
f'<script type="module" src="{self.frc_widget_module_js}" async defer></script>'
f'<script nomodule src="{self.frc_widget_js}" async defer></script>'
)
return format_html('<div {}></div>{}', flatatt(final_attrs), frc_js)
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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,
)

0 comments on commit 95dd1d3

Please sign in to comment.