-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Include friendly captcha javascript assets
- Loading branch information
1 parent
368ff4a
commit 95dd1d3
Showing
4 changed files
with
42 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters