Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ class MyForm(forms.Form):
URL location of the Altcha JavaScript file.
Default to the django-altcha embedded file.

### ALTCHA_INCLUDE_TRANSLATIONS

Include Altcha [translations](https://altcha.org/docs/v2/widget-integration/#internationalization-i18n). `False` by default.

### ALTCHA_VERIFICATION_ENABLED

Set to `False` to skip Altcha validation altogether.
Expand Down
4 changes: 4 additions & 0 deletions django_altcha/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
ALTCHA_HMAC_KEY = secrets.token_hex(32)

ALTCHA_JS_URL = getattr(settings, "ALTCHA_JS_URL", "/static/altcha/altcha.min.js")
ALTCHA_INCLUDE_TRANSLATIONS = getattr(settings, "ALTCHA_INCLUDE_TRANSLATIONS", False)
ALTCHA_JS_TRANSLATIONS = "/static/altcha/dist_i18n/all.min.js"

# Challenge expiration duration in milliseconds.
# Default to 20 minutes as per Altcha security recommendations.
Expand Down Expand Up @@ -123,6 +125,8 @@ def get_context(self, name, value, attrs):
"""Generate the widget context, including ALTCHA JS URL and challenge."""
context = super().get_context(name, value, attrs)
context["js_src_url"] = ALTCHA_JS_URL
context["include_trans"] = ALTCHA_INCLUDE_TRANSLATIONS
context["js_trans"] = ALTCHA_JS_TRANSLATIONS

# If a `challengeurl` is provided, the challenge will be fetched from this URL.
# This can be a local Django view or an external API endpoint.
Expand Down
4 changes: 4 additions & 0 deletions django_altcha/static/altcha/dist_i18n/all.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions django_altcha/templates/altcha_widget.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<script async defer src="{{ js_src_url }}" type="module"></script>
{% if include_trans %}<script async defer src="{{ js_trans }}" type="module"></script>{% endif %}
{% include "django/forms/widgets/input.html" %}
<altcha-widget name="{{ widget.name }}"
{% for key, value in widget.altcha_options.items %}
Expand Down
13 changes: 13 additions & 0 deletions tests/test_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#

import json
from unittest import mock

from django.test import TestCase

Expand Down Expand Up @@ -53,3 +54,15 @@ def test_widget_rendering_with_complex_options(self):
'&quot;verified&quot;: &quot;Verified&quot;}"'
)
self.assertIn(expected, rendered_widget_html)

@mock.patch("django_altcha.ALTCHA_INCLUDE_TRANSLATIONS", True)
def test_js_translation_included_if_enabled(self):
widget = AltchaWidget(options=None)
rendered_widget_html = widget.render("name", "value")
self.assertIn("/static/altcha/dist_i18n/all.min.js", rendered_widget_html)

@mock.patch("django_altcha.ALTCHA_INCLUDE_TRANSLATIONS", False)
def test_js_translation_not_included_if_disabled(self):
widget = AltchaWidget(options=None)
rendered_widget_html = widget.render("name", "value")
self.assertNotIn("/static/altcha/dist_i18n/all.min.js", rendered_widget_html)