Skip to content

Commit e276032

Browse files
committed
fix: improve url registration
1 parent 0b037a0 commit e276032

File tree

4 files changed

+34
-30
lines changed

4 files changed

+34
-30
lines changed

djangocms_form_builder/apps.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from django.apps import AppConfig
44
from django.conf import settings
5-
from django.urls import NoReverseMatch, clear_url_caches, include, path, reverse
5+
from django.urls import clear_url_caches, include, path
66
from django.utils.translation import gettext_lazy as _
77

88

@@ -11,20 +11,23 @@ class FormsConfig(AppConfig):
1111
name = "djangocms_form_builder"
1212
verbose_name = _("Form builder")
1313

14+
patterns_added = False
15+
1416
def ready(self):
1517
"""Install the URLs"""
16-
try:
17-
reverse("form_builder:ajaxview", args=(1,))
18-
except NoReverseMatch: # Not installed yet
19-
urlconf_module = import_module(settings.ROOT_URLCONF)
20-
urlconf_module.urlpatterns = [
21-
path(
22-
"@form-builder/",
23-
include(
24-
"djangocms_form_builder.urls",
25-
namespace="form_builder",
26-
),
27-
)
28-
] + urlconf_module.urlpatterns
29-
clear_url_caches()
30-
reverse("form_builder:ajaxview", args=(1,))
18+
if self.patterns_added:
19+
return
20+
self.patterns_added = True
21+
urlconf_module = import_module(settings.ROOT_URLCONF)
22+
# Add the new URL pattern
23+
urlconf_module.urlpatterns = [
24+
path(
25+
"@form-builder/",
26+
include(
27+
"djangocms_form_builder.urls",
28+
namespace="form_builder",
29+
),
30+
)
31+
] + urlconf_module.urlpatterns
32+
# Clear the URL cache
33+
clear_url_caches()

djangocms_form_builder/cms_plugins/ajax_plugins.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ def get_parent_classes(cls, slot, page, instance=None):
297297
parent = instance
298298
while parent is not None:
299299
if parent.plugin_type == cls.__name__:
300-
return [""]
300+
return ["0"]
301301
parent = parent.parent
302302
return super().get_parent_classes(slot, page, instance)
303303

djangocms_form_builder/templates/djangocms_form_builder/ajax_form.html

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@
105105
}
106106

107107
function post_ajax(node) {
108-
fetch(node.getAttribute('action'),{
109-
method: 'POST',
108+
fetch(node.getAttribute('action'), {
109+
method: node.getAttribute('method') || 'POST',
110110
body: new URLSearchParams(new FormData(node)),
111111
}
112112
).then(function (response) {
@@ -135,8 +135,9 @@
135135
grecaptcha.reset(gid);
136136
},
137137
});
138-
if(!form.dataset.submitEvent) {
138+
if(!false) {
139139
form.dataset.submitEvent = true;
140+
console.log("recaptcha submit added");
140141
form.addEventListener('submit', function (event) {
141142
event.preventDefault();
142143
console.log("recaptcha submit")
@@ -146,8 +147,9 @@
146147
}
147148
}, 100);
148149
} else {
149-
if(!form.dataset.submitEvent) {
150+
if(true) {
150151
form.dataset.submitEvent = true;
152+
console.log("submit added");
151153
form.addEventListener('submit', function (event) {
152154
event.preventDefault();
153155
post_ajax(form);
@@ -161,11 +163,10 @@
161163
(function () {
162164
function initForms() {
163165
for (let form of document.getElementsByClassName('djangocms-form-builder-ajax-form')) {
164-
165166
djangocms_form_builder_form(form);
166167
}
167168
}
168-
window.addEventListener('DOMContentLoaded', initForms);
169+
window.addEventListener('load', initForms);
169170
{% if request.toolbar and request.toolbar.edit_mode_active %}
170171
CMS.$(window).on('cms-content-refresh', initForms);
171172
{% endif %}

djangocms_form_builder/views.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class AjaxView(View):
3737
Methods
3838
-------
3939
40-
dispatch(request, \*args, \*\*kwargs)
40+
dispatch(request, *args, **kwargs)
4141
Overrides the default dispatch method to handle AJAX requests.
4242
4343
decode_path(path)
@@ -46,10 +46,10 @@ class AjaxView(View):
4646
plugin_instance(pk)
4747
Retrieves the plugin instance and its associated model instance by primary key.
4848
49-
ajax_post(request, \*args, \*\*kwargs)
49+
ajax_post(request, *args, **kwargs)
5050
Handles AJAX POST requests. Calls the `ajax_post` method of the plugin or form instance if available.
5151
52-
ajax_get(request, \*args, \*\*kwargs)
52+
ajax_get(request, *args, **kwargs)
5353
Handles AJAX GET requests. Calls the `ajax_get` method of the plugin or form instance if available.
5454
"""
5555
def dispatch(self, request, *args, **kwargs):
@@ -93,8 +93,8 @@ def ajax_post(self, request, *args, **kwargs):
9393
9494
Args:
9595
request (HttpRequest): The HTTP request object.
96-
\*args: Additional positional arguments.
97-
\*\*kwargs: Additional keyword arguments, which may include:
96+
*args: Additional positional arguments.
97+
**kwargs: Additional keyword arguments, which may include:
9898
- instance_id (int): The ID of the plugin instance.
9999
- parameter (str): Optional parameter for decoding.
100100
- form_id (str): The ID of the form instance.
@@ -145,8 +145,8 @@ def ajax_get(self, request, *args, **kwargs):
145145
146146
Args:
147147
request (HttpRequest): The HTTP request object.
148-
\*args: Additional positional arguments.
149-
\*\*kwargs: Additional keyword arguments.
148+
*args: Additional positional arguments.
149+
**kwargs: Additional keyword arguments.
150150
151151
Returns:
152152
JsonResponse: A JSON response with the result of the AJAX request.

0 commit comments

Comments
 (0)