Skip to content

Commit 8706222

Browse files
xgimptdruez
andauthored
feat: added support for altcha translations #23
Signed-off-by: Lukáš Bačovský <l.bacovsky@gmail.com> Co-authored-by: tdruez <tdruez@aboutcode.org>
1 parent 90f4cf0 commit 8706222

8 files changed

Lines changed: 59 additions & 10 deletions

File tree

README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,20 @@ class MyForm(forms.Form):
132132

133133
### ALTCHA_JS_URL
134134

135-
URL location of the Altcha JavaScript file.
136-
Default to the django-altcha embedded file.
135+
URL of the Altcha JavaScript file.
136+
Defaults to the bundled django-altcha file.
137+
138+
### ALTCHA_INCLUDE_TRANSLATIONS
139+
140+
Whether to include [Altcha translations](https://altcha.org/docs/v2/widget-integration/#internationalization-i18n).
141+
Defaults to `False`.
142+
143+
### ALTCHA_JS_TRANSLATIONS_URL
144+
145+
URL of the Altcha translations JavaScript file.
146+
Defaults to the bundled django-altcha file.
147+
148+
Only loaded when `ALTCHA_INCLUDE_TRANSLATIONS` is `True`.
137149

138150
### ALTCHA_VERIFICATION_ENABLED
139151

django_altcha/__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@
4646
ALTCHA_HMAC_KEY = secrets.token_hex(32)
4747

4848
ALTCHA_JS_URL = getattr(settings, "ALTCHA_JS_URL", "/static/altcha/altcha.min.js")
49+
ALTCHA_JS_TRANSLATIONS_URL = getattr(
50+
settings, "ALTCHA_JS_TRANSLATIONS_URL", "/static/altcha/dist_i18n/all.min.js"
51+
)
52+
ALTCHA_INCLUDE_TRANSLATIONS = getattr(settings, "ALTCHA_INCLUDE_TRANSLATIONS", False)
4953

5054
# Challenge expiration duration in milliseconds.
5155
# Default to 20 minutes as per Altcha security recommendations.
@@ -122,7 +126,9 @@ def __init__(self, options, *args, **kwargs):
122126
def get_context(self, name, value, attrs):
123127
"""Generate the widget context, including ALTCHA JS URL and challenge."""
124128
context = super().get_context(name, value, attrs)
125-
context["js_src_url"] = ALTCHA_JS_URL
129+
context["js_altcha_url"] = ALTCHA_JS_URL
130+
context["js_translations_url"] = ALTCHA_JS_TRANSLATIONS_URL
131+
context["include_translations"] = ALTCHA_INCLUDE_TRANSLATIONS
126132

127133
# If a `challengeurl` is provided, the challenge will be fetched from this URL.
128134
# This can be a local Django view or an external API endpoint.

django_altcha/static/altcha/dist_i18n/all.min.js

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

django_altcha/templates/altcha_widget.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
<script async defer src="{{ js_src_url }}" type="module"></script>
1+
<script async defer src="{{ js_altcha_url }}" type="module"></script>
2+
{% if include_translations %}<script async defer src="{{ js_translations_url }}" type="module"></script>{% endif %}
23
{% include "django/forms/widgets/input.html" %}
34
<altcha-widget name="{{ widget.name }}"
45
{% for key, value in widget.altcha_options.items %}

docs/source/installation.rst

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,22 @@ This key is used to HMAC-sign ALTCHA challenges and **must be kept secret**.
8181
ALTCHA_JS_URL
8282
~~~~~~~~~~~~~
8383

84-
URL location of the Altcha JavaScript file.
85-
Default to the django-altcha embedded file.
84+
URL of the Altcha JavaScript file.
85+
Defaults to the bundled django-altcha file.
86+
87+
ALTCHA_INCLUDE_TRANSLATIONS
88+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
89+
90+
Whether to include `Altcha translations <https://altcha.org/docs/v2/widget-integration/#internationalization-i18n>`_.
91+
Defaults to ``False``.
92+
93+
ALTCHA_JS_TRANSLATIONS_URL
94+
~~~~~~~~~~~~~~~~~~~~~~~~~~
95+
96+
URL of the Altcha translations JavaScript file.
97+
Defaults to the bundled django-altcha file.
98+
99+
Only loaded when ``ALTCHA_INCLUDE_TRANSLATIONS`` is ``True``.
86100

87101
ALTCHA_VERIFICATION_ENABLED
88102
~~~~~~~~~~~~~~~~~~~~~~~~~~~

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ classifiers = [
3030
requires-python = ">=3.9"
3131
dependencies = [
3232
"Django>=4.2",
33-
"altcha>=0.2.0",
33+
"altcha==0.2.0",
3434
]
3535
keywords = ["captcha", "django", "widget", "form", "altcha"]
3636

tests/settings.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
INSTALLED_APPS = [
2-
"django_altcha",
3-
]
1+
INSTALLED_APPS = ["django_altcha"]
42
DATABASES = {"default": {"ENGINE": "django.db.backends.sqlite3"}}
53
ROOT_URLCONF = "tests.urls"
64
ALTCHA_HMAC_KEY = "altcha-insecure-hmac-0123456789abcdef"
5+
ALTCHA_JS_TRANSLATIONS_URL = "/static/altcha/dist_i18n/all.min.js"

tests/test_widget.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#
77

88
import json
9+
from unittest import mock
910

1011
from django.test import TestCase
1112

@@ -53,3 +54,15 @@ def test_widget_rendering_with_complex_options(self):
5354
'&quot;verified&quot;: &quot;Verified&quot;}"'
5455
)
5556
self.assertIn(expected, rendered_widget_html)
57+
58+
@mock.patch("django_altcha.ALTCHA_INCLUDE_TRANSLATIONS", True)
59+
def test_js_translation_included_if_enabled(self):
60+
widget = AltchaWidget(options=None)
61+
rendered_widget_html = widget.render("name", "value")
62+
self.assertIn("/static/altcha/dist_i18n/all.min.js", rendered_widget_html)
63+
64+
@mock.patch("django_altcha.ALTCHA_INCLUDE_TRANSLATIONS", False)
65+
def test_js_translation_not_included_if_disabled(self):
66+
widget = AltchaWidget(options=None)
67+
rendered_widget_html = widget.render("name", "value")
68+
self.assertNotIn("/static/altcha/dist_i18n/all.min.js", rendered_widget_html)

0 commit comments

Comments
 (0)