Skip to content

Commit ed86e34

Browse files
committed
Move the altcha code to its own module #235
Signed-off-by: tdruez <[email protected]>
1 parent 5560103 commit ed86e34

File tree

4 files changed

+49
-24
lines changed

4 files changed

+49
-24
lines changed

dejacode/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ def gettext_noop(s):
332332
"axes",
333333
"django_otp",
334334
"django_otp.plugins.otp_totp",
335-
"hcaptcha_field",
335+
"django_altcha",
336336
]
337337

338338
PROJECT_APPS = [

dje/altcha.py renamed to django_altcha/__init__.py

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,65 @@
11
#
22
# Copyright (c) nexB Inc. and others. All rights reserved.
3-
# DejaCode is a trademark of nexB Inc.
4-
# SPDX-License-Identifier: AGPL-3.0-only
5-
# See https://github.com/aboutcode-org/dejacode for support or download.
3+
# SPDX-License-Identifier: MIT
4+
# See https://github.com/aboutcode-org/django-altcha for support or download.
65
# See https://aboutcode.org for more information about AboutCode FOSS projects.
76
#
87

8+
import json
9+
import secrets
910

1011
from django import forms
12+
from django.conf import settings
1113
from django.forms.widgets import HiddenInput
1214
from django.utils.translation import gettext_lazy as _
1315

1416
# TODO: Add as a dependency
1517
import altcha
16-
from altcha import ChallengeOptions
17-
from altcha import create_challenge
1818

19-
ALTCHA_HMAC_KEY = "your-altcha-hmac-key"
19+
"""
20+
1. Add "to INSTALLED_APPS:
21+
22+
INSTALLED_APPS = [
23+
# ...
24+
"django_altcha"
25+
]
26+
27+
2. Add the field on your forms:
28+
29+
from django_altcha import AltchaField
30+
31+
class Form(forms.Form):
32+
captcha = AltchaField()
33+
34+
35+
You can provide any configuration options available at
36+
https://altcha.org/docs/website-integration/ such as:
37+
38+
class Form(forms.Form):
39+
captcha = AltchaField(
40+
floating=True,
41+
debug=True,
42+
# ...
43+
)
44+
"""
45+
46+
# Get the ALTCHA_HMAC_KEY from the settings, or generate one if not present
47+
ALTCHA_HMAC_KEY = getattr(settings, "ALTCHA_HMAC_KEY", secrets.token_hex(32))
48+
49+
50+
def get_altcha_challenge():
51+
"""Return an ALTCHA challenge."""
52+
challenge = altcha.create_challenge(
53+
altcha.ChallengeOptions(
54+
hmac_key=ALTCHA_HMAC_KEY,
55+
max_number=50000,
56+
)
57+
)
58+
return challenge
2059

2160

2261
class AltchaWidget(HiddenInput):
23-
template_name = "widgets/altcha.html"
62+
template_name = "altcha_widget.html"
2463
default_options = {
2564
# Required: URL of your server to fetch the challenge from.
2665
"challengeurl": None,
@@ -91,8 +130,6 @@ class AltchaField(forms.Field):
91130
# TODO: This is only called once on Form declaration.
92131
# Making the challenge always the same.
93132
def __init__(self, *args, **kwargs):
94-
import json
95-
96133
challengeurl = kwargs.pop("challengeurl", None)
97134
challengejson = kwargs.pop("challengejson", None)
98135

@@ -134,14 +171,3 @@ def validate(self, value):
134171

135172
if not verified:
136173
raise forms.ValidationError(self.error_messages["invalid"], code="invalid")
137-
138-
139-
def get_altcha_challenge():
140-
# Create the challenge using your options
141-
challenge = create_challenge(
142-
ChallengeOptions(
143-
hmac_key=ALTCHA_HMAC_KEY,
144-
max_number=50000,
145-
)
146-
)
147-
return challenge
File renamed without changes.

dje/registration.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
from django_registration.exceptions import ActivationError
2929
from django_registration.forms import RegistrationFormUniqueEmail
3030

31-
from dje.altcha import AltchaField
31+
from django_altcha import AltchaField
3232
from dje.forms import StrictSubmit
3333
from dje.models import Dataspace
3434
from dje.models import History
@@ -88,9 +88,8 @@ class DejaCodeRegistrationForm(RegistrationFormUniqueEmail):
8888

8989
use_required_attribute = False
9090
captcha = AltchaField(
91-
# challengeurl="/altcha/",
9291
# strings={"label": "I'm not a robot"},
93-
# floating=True,
92+
floating=True,
9493
hidefooter=True,
9594
debug=True,
9695
)

0 commit comments

Comments
 (0)