Skip to content

Commit 5560103

Browse files
committed
Remove the need for a dedicated view #235
Signed-off-by: tdruez <[email protected]>
1 parent 2fdec19 commit 5560103

File tree

3 files changed

+27
-15
lines changed

3 files changed

+27
-15
lines changed

dejacode/urls.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
from component_catalog.views import send_scan_notification
2929
from dje import two_factor
3030
from dje.admin import dejacode_site
31-
from dje.altcha import get_altcha_challenge_view
3231
from dje.api import ExternalReferenceViewSet
3332
from dje.forms import DejaCodeAuthenticationForm
3433
from dje.registration import DejaCodeActivationView
@@ -196,7 +195,6 @@
196195
name="django_registration_register",
197196
),
198197
path("account/", include("django_registration.backends.activation.urls")),
199-
path("altcha/", get_altcha_challenge_view, name="get_altcha_challenge"),
200198
]
201199

202200
if settings.DEBUG and settings.DEBUG_TOOLBAR:

dje/altcha.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99

1010
from django import forms
1111
from django.forms.widgets import HiddenInput
12-
from django.http import JsonResponse
1312
from django.utils.translation import gettext_lazy as _
14-
from django.views.decorators.http import require_GET
1513

1614
# TODO: Add as a dependency
1715
import altcha
@@ -90,8 +88,31 @@ class AltchaField(forms.Field):
9088
"required": _("Altcha CAPTCHA token is missing."),
9189
}
9290

91+
# TODO: This is only called once on Form declaration.
92+
# Making the challenge always the same.
9393
def __init__(self, *args, **kwargs):
94-
widget_options = {key: kwargs.pop(key, None) for key in AltchaWidget.default_options}
94+
import json
95+
96+
challengeurl = kwargs.pop("challengeurl", None)
97+
challengejson = kwargs.pop("challengejson", None)
98+
99+
# If no ``challengeurl`` is provided, auto-generate ``challengejson``
100+
if challengeurl is None and challengejson is None:
101+
challenge = get_altcha_challenge()
102+
challengejson = json.dumps(challenge.__dict__)
103+
104+
# Prepare widget options
105+
widget_options = {
106+
"challengeurl": challengeurl,
107+
"challengejson": challengejson,
108+
}
109+
110+
# Include any other ALTCHA options passed
111+
for key in AltchaWidget.default_options:
112+
if key not in widget_options:
113+
widget_options[key] = kwargs.pop(key, None)
114+
115+
# Assign the updated widget
95116
kwargs["widget"] = AltchaWidget(**widget_options)
96117
super().__init__(*args, **kwargs)
97118

@@ -124,12 +145,3 @@ def get_altcha_challenge():
124145
)
125146
)
126147
return challenge
127-
128-
129-
@require_GET
130-
def get_altcha_challenge_view(request):
131-
try:
132-
challenge = get_altcha_challenge()
133-
return JsonResponse(challenge.__dict__)
134-
except Exception as e:
135-
return JsonResponse({"error": f"Failed to create challenge: {str(e)}"}, status=500)

dje/registration.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,10 @@ class DejaCodeRegistrationForm(RegistrationFormUniqueEmail):
8888

8989
use_required_attribute = False
9090
captcha = AltchaField(
91-
challengeurl="/altcha/",
91+
# challengeurl="/altcha/",
92+
# strings={"label": "I'm not a robot"},
9293
# floating=True,
94+
hidefooter=True,
9395
debug=True,
9496
)
9597

0 commit comments

Comments
 (0)