Skip to content

Commit a6b3b1e

Browse files
committed
Fix #43: Stop slugifying choices, thereby supporting non-latin languages
1 parent cf07d5d commit a6b3b1e

File tree

4 files changed

+56
-24
lines changed

4 files changed

+56
-24
lines changed

.pre-commit-config.yaml

+9-17
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
exclude: ".yarn/|yarn.lock|\\.min\\.(css|js)$"
22
repos:
33
- repo: https://github.com/pre-commit/pre-commit-hooks
4-
rev: v4.6.0
4+
rev: v5.0.0
55
hooks:
66
- id: check-added-large-files
77
- id: check-builtin-literals
@@ -14,35 +14,27 @@ repos:
1414
- id: mixed-line-ending
1515
- id: trailing-whitespace
1616
- repo: https://github.com/adamchainz/django-upgrade
17-
rev: 1.20.0
17+
rev: 1.22.1
1818
hooks:
1919
- id: django-upgrade
2020
args: [--target-version, "3.2"]
21-
- repo: https://github.com/MarcoGorelli/absolufy-imports
22-
rev: v0.3.1
23-
hooks:
24-
- id: absolufy-imports
2521
- repo: https://github.com/astral-sh/ruff-pre-commit
26-
rev: "v0.6.2"
22+
rev: "v0.7.0"
2723
hooks:
2824
- id: ruff
25+
args: [--unsafe-fixes]
2926
- id: ruff-format
30-
- repo: https://github.com/pre-commit/mirrors-prettier
31-
rev: v3.1.0
32-
hooks:
33-
- id: prettier
34-
args: [--list-different, --no-semi]
35-
exclude: "^conf/|.*\\.html$"
3627
- repo: https://github.com/biomejs/pre-commit
37-
rev: "v0.4.0"
28+
rev: "v0.5.0"
3829
hooks:
3930
- id: biome-check
40-
additional_dependencies: ["@biomejs/[email protected]"]
31+
additional_dependencies: ["@biomejs/[email protected]"]
32+
args: [--unsafe]
4133
- repo: https://github.com/tox-dev/pyproject-fmt
42-
rev: 2.2.1
34+
rev: 2.4.3
4335
hooks:
4436
- id: pyproject-fmt
4537
- repo: https://github.com/abravalheri/validate-pyproject
46-
rev: v0.19
38+
rev: v0.21
4739
hooks:
4840
- id: validate-pyproject

CHANGELOG.rst

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Next version
66

77
* Limited the email field choices to email fields only when using the send
88
emails to authors option.
9+
* Stopped slugifying choice values so that we properly support non-latin
10+
characters.
911

1012
0.26
1113
----

form_designer/models.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,17 @@ def loader(submission, field, choice_dict):
204204
def old_name_loader(submission, old_name):
205205
return submission.data.get(old_name)
206206

207+
def include_slugified_choices(choices):
208+
return dict(choices) | {slugify(value): label for value, label in choices}
209+
207210
fields_and_loaders = [
208211
(
209212
{"name": field.name, "title": field.title},
210-
partial(loader, field=field, choice_dict=dict(field.get_choices())),
213+
partial(
214+
loader,
215+
field=field,
216+
choice_dict=include_slugified_choices(field.get_choices()),
217+
),
211218
)
212219
for field in self.fields.all()
213220
]
@@ -360,7 +367,7 @@ def clean(self):
360367

361368
def get_choices(self):
362369
def get_tuple(value):
363-
return (slugify(value.strip()), value.strip())
370+
return (value.strip(), value.strip())
364371

365372
choices = [get_tuple(value) for value in self.choices.split(",")]
366373
if not self.is_required and self.type == "select":
@@ -383,8 +390,7 @@ def formfield(self):
383390
}
384391
if self.choices:
385392
kwargs["choices"] = self.get_choices()
386-
# The value of individual choices is slugified too.
387-
kwargs["initial"] = slugify(self.default_value)
393+
kwargs["initial"] = self.default_value
388394
return self.get_type(**kwargs)
389395

390396

tests/testapp/test_forms.py

+35-3
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def test_forms(self):
9494
)
9595
self.assertContains(response, "<textarea", 1)
9696
self.assertContains(
97-
response, 'value="two-what" required id="id_fc1-radio_1" checked', 1
97+
response, 'value="two what" required id="id_fc1-radio_1" checked', 1
9898
)
9999
self.assertContains(response, 'type="date"', 1)
100100

@@ -133,9 +133,9 @@ def test_forms(self):
133133
f"fc{form.id}-subject": "Test",
134134
f"fc{form.id}-email": "[email protected]",
135135
f"fc{form.id}-body": "Hello World",
136-
f"fc{form.id}-radio": "two-what",
136+
f"fc{form.id}-radio": "two what",
137137
f"fc{form.id}-date": "2022-10-02",
138-
f"fc{form.id}-multiple-choice": ["choice-a", "choice-c"],
138+
f"fc{form.id}-multiple-choice": ["Choice A", "Choice C"],
139139
},
140140
)
141141

@@ -407,3 +407,35 @@ def test_email_to_author(self):
407407
self.assertEqual(message.subject, "Test contact form")
408408
self.assertIn("Subject:\nTest\n", message.body)
409409
self.assertIn("Email:\n[email protected]\n", message.body)
410+
411+
def test_choices(self):
412+
"""Legacy slugified choices should also use the pretty format"""
413+
414+
form = Form.objects.create(
415+
title="Test contact form",
416+
config={"save_fs": {}},
417+
)
418+
form.fields.create(
419+
ordering=6,
420+
title="Multiple Choice",
421+
name="multiple-choice",
422+
type="multiple-select",
423+
choices="Choice A,Choice B,Choice C",
424+
is_required=False,
425+
)
426+
427+
s1 = FormSubmission.objects.create(
428+
form=form,
429+
data={
430+
"multiple-choice": ["Choice A", "Choice C"],
431+
},
432+
)
433+
self.assertIn("Multiple Choice:\nChoice A, Choice C", s1.formatted_data())
434+
435+
s2 = FormSubmission.objects.create(
436+
form=form,
437+
data={
438+
"multiple-choice": ["choice-a", "choice-c"],
439+
},
440+
)
441+
self.assertIn("Multiple Choice:\nChoice A, Choice C", s2.formatted_data())

0 commit comments

Comments
 (0)