Skip to content

Commit 3bd0066

Browse files
authored
fix: forms did not redirect to same page if sent from alias (#20)
* fix: forms did not redirect to same page if sent from alias * Update test requirements * Add submit message action * Fix linting * Remove debugging statements * Add redirect after submission action * Update README * Bump version
1 parent ac1afaf commit 3bd0066

File tree

15 files changed

+108
-21
lines changed

15 files changed

+108
-21
lines changed

.github/workflows/codecov.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,19 @@ jobs:
1414
matrix:
1515
python-version: [ 3.9, "3.10", "3.11", "3.12"] # latest release minus two
1616
requirements-file: [
17-
dj32_cms310.txt,
18-
dj32_cms311.txt,
1917
dj42_cms311.txt,
2018
dj42_cms41.txt,
2119
dj50_cms41.txt,
20+
dj51_cms41.txt,
2221
]
2322
os: [
2423
ubuntu-20.04,
2524
]
2625
exclude:
2726
- python-version: 3.9
2827
requirements-file: dj50_cms41.txt
28+
- python-version: 3.9
29+
requirements-file: dj51_cms41.txt
2930

3031
steps:
3132
- uses: actions/checkout@v3

CHANGELOG.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
Changelog
33
=========
44

5+
0.3.0 (2025-01-07)
6+
==================
7+
8+
* feat: Success message and redirect action by @fsbraun
9+
* fix: forms did not redirect to same page if sent from alias by @fsbraun
10+
511
0.2.0 (2025-01-06)
612
==================
713

README.rst

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,21 @@ A Form plugin must not be used within another Form plugin.
8686
Actions
8787
-------
8888

89-
Upon submission of a valid form actions can be performed. A project can register as many actions as it likes::
89+
Upon submission of a valid form actions can be performed.
90+
91+
Four actions come with djangocms-form-builder comes with four actions built-in
92+
93+
* **Save form submission** - Saves each form submission to the database. See the
94+
results in the admin interface.
95+
* **Send email** - Sends an email to the site admins with the form data.
96+
* **Success message** - Specify a message to be shown to the user upon
97+
successful form submission.
98+
* **Redirect after submission** - Specify a link to a page where the user is
99+
redirected after successful form submission.
100+
101+
Actions can be configured in the form plugin.
102+
103+
A project can register as many actions as it likes::
90104

91105
from djangocms_form_builder import actions
92106

djangocms_form_builder/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
try:
44
from django.utils.translation import gettext_lazy as _
55
except ModuleNotFoundError:
6-
_ = lambda x: x
6+
_ = lambda x: x # noqa: E731
77

88

9-
__version__ = "0.2.0"
9+
__version__ = "0.3.0"
1010

1111
_form_registry = {}
1212

djangocms_form_builder/actions.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import hashlib
22

33
from django import forms
4+
from django.apps import apps
45
from django.core.exceptions import ImproperlyConfigured
56
from django.core.mail import mail_admins, send_mail
67
from django.core.validators import EmailValidator
78
from django.template import TemplateDoesNotExist
89
from django.template.loader import render_to_string
910
from django.utils.html import strip_tags
1011
from django.utils.translation import gettext_lazy as _
12+
from djangocms_text_ckeditor.fields import HTMLFormField
1113
from entangled.forms import EntangledModelFormMixin
1214

1315
from . import models
@@ -219,3 +221,51 @@ def execute(self, form, request):
219221
fail_silently=True,
220222
html_message=html_message,
221223
)
224+
225+
226+
@register
227+
class SuccessMessageAction(FormAction):
228+
verbose_name = _("Success message")
229+
230+
class Meta:
231+
entangled_fields = {
232+
"action_parameters": [
233+
"submitmessage_message",
234+
]
235+
}
236+
237+
submitmessage_message = HTMLFormField(
238+
label=_("Message"),
239+
required=True,
240+
initial=_("<p>Thank you for your submission.</p>"),
241+
)
242+
243+
def execute(self, form, request):
244+
message = self.get_parameter(form, "submitmessage_message")
245+
form.get_success_context = lambda *args, **kwargs: {"message": message}
246+
form.Meta.options["render_success"] = "djangocms_form_builder/actions/submit_message.html"
247+
form.Meta.options["redirect"] = None
248+
249+
250+
if apps.is_installed("djangocms_link"):
251+
from djangocms_link.fields import LinkFormField
252+
from djangocms_link.helpers import get_link
253+
254+
@register
255+
class RedirectAction(FormAction):
256+
verbose_name = _("Redirect after submission")
257+
258+
class Meta:
259+
entangled_fields = {
260+
"action_parameters": [
261+
"redirect_link",
262+
]
263+
}
264+
265+
redirect_link = LinkFormField(
266+
label=_("Link"),
267+
required=True,
268+
)
269+
270+
def execute(self, form, request):
271+
form.Meta.options["redirect"] = get_link(self.get_parameter(form, "redirect_link"))

djangocms_form_builder/cms_plugins/ajax_plugins.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
from ..forms import SimpleFrontendForm
2020
from ..helpers import get_option, insert_fields, mark_safe_lazy
2121

22+
SAME_PAGE_REDIRECT = "result"
23+
2224

2325
class CMSAjaxBase(CMSPluginBase):
2426
def ajax_post(self, request, instance, parameter):
@@ -82,6 +84,7 @@ def form_valid(self, form):
8284
if hasattr(form, get_success_context):
8385
get_success_context = getattr(form, get_success_context)
8486
context.update(get_success_context(self.request, self.instance, form))
87+
print(context)
8588
errors, result, redir, content = (
8689
[],
8790
context.get("result", "success"),
@@ -351,7 +354,7 @@ def traverse(instance):
351354
fields = {}
352355
traverse(self.instance)
353356

354-
# Add recaptcha field in necessary
357+
# Add recaptcha field if necessary
355358
if recaptcha.installed and self.instance.captcha_widget:
356359
fields[recaptcha.field_name] = recaptcha.get_recaptcha_field(self.instance)
357360

@@ -364,7 +367,7 @@ def traverse(instance):
364367
] = f'{self.instance.form_spacing}'
365368
meta_options[
366369
"redirect"
367-
] = self.instance.placeholder.page # Default behavior: redirect to same page
370+
] = SAME_PAGE_REDIRECT # Default behavior: redirect to same page
368371
meta_options["login_required"] = self.instance.form_login_required
369372
meta_options["unique"] = self.instance.form_unique
370373
form_actions = self.instance.form_actions or "[]"

djangocms_form_builder/models.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -255,13 +255,13 @@ def format_value(self, value):
255255
return ""
256256
value = str(value)
257257
if "." in value:
258-
l, r = value.rsplit(".", 1)
258+
left, right = value.rsplit(".", 1)
259259
else:
260-
l, r = value, ""
260+
left, right = value, ""
261261
if self.decimal_places == 0:
262-
return l
263-
r = (r + self.decimal_places * "0")[: self.decimal_places]
264-
return super().format_value(".".join((l, r)))
262+
return left
263+
right = (right + self.decimal_places * "0")[: self.decimal_places]
264+
return super().format_value(".".join((left, right)))
265265

266266
class StrDecimalField(forms.DecimalField):
267267
def clean(self, value):
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
fieldset.action-hide {
1+
fieldset.action-hide, fieldset.empty {
22
display: none;
33
}

djangocms_form_builder/static/djangocms_form_builder/js/actions_form.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
$(function () {
2+
'use strict';
23
$('fieldset.action-auto-hide input[type="checkbox"][name="form_actions"]').each(function (index, element) {
4+
const target = $('.' + $(element).attr("value"));
35
if (element.checked) {
4-
$("."+$(element).attr("value")).removeClass("action-hide");
6+
target.removeClass("action-hide");
7+
}
8+
if (!target.find('.form-row:not(.hidden)').length) {
9+
target.addClass("empty");
510
}
611
$(element).on("change", function (event) {
712
var element = event.target;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{ message|safe }}

0 commit comments

Comments
 (0)