Skip to content

Commit

Permalink
Fix #43: Stop slugifying choices, thereby supporting non-latin languages
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiask committed Oct 21, 2024
1 parent cf07d5d commit a6b3b1e
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 24 deletions.
26 changes: 9 additions & 17 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
exclude: ".yarn/|yarn.lock|\\.min\\.(css|js)$"
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
rev: v5.0.0
hooks:
- id: check-added-large-files
- id: check-builtin-literals
Expand All @@ -14,35 +14,27 @@ repos:
- id: mixed-line-ending
- id: trailing-whitespace
- repo: https://github.com/adamchainz/django-upgrade
rev: 1.20.0
rev: 1.22.1
hooks:
- id: django-upgrade
args: [--target-version, "3.2"]
- repo: https://github.com/MarcoGorelli/absolufy-imports
rev: v0.3.1
hooks:
- id: absolufy-imports
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: "v0.6.2"
rev: "v0.7.0"
hooks:
- id: ruff
args: [--unsafe-fixes]
- id: ruff-format
- repo: https://github.com/pre-commit/mirrors-prettier
rev: v3.1.0
hooks:
- id: prettier
args: [--list-different, --no-semi]
exclude: "^conf/|.*\\.html$"
- repo: https://github.com/biomejs/pre-commit
rev: "v0.4.0"
rev: "v0.5.0"
hooks:
- id: biome-check
additional_dependencies: ["@biomejs/[email protected]"]
additional_dependencies: ["@biomejs/[email protected]"]
args: [--unsafe]
- repo: https://github.com/tox-dev/pyproject-fmt
rev: 2.2.1
rev: 2.4.3
hooks:
- id: pyproject-fmt
- repo: https://github.com/abravalheri/validate-pyproject
rev: v0.19
rev: v0.21
hooks:
- id: validate-pyproject
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Next version

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

0.26
----
Expand Down
14 changes: 10 additions & 4 deletions form_designer/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,17 @@ def loader(submission, field, choice_dict):
def old_name_loader(submission, old_name):
return submission.data.get(old_name)

def include_slugified_choices(choices):
return dict(choices) | {slugify(value): label for value, label in choices}

fields_and_loaders = [
(
{"name": field.name, "title": field.title},
partial(loader, field=field, choice_dict=dict(field.get_choices())),
partial(
loader,
field=field,
choice_dict=include_slugified_choices(field.get_choices()),
),
)
for field in self.fields.all()
]
Expand Down Expand Up @@ -360,7 +367,7 @@ def clean(self):

def get_choices(self):
def get_tuple(value):
return (slugify(value.strip()), value.strip())
return (value.strip(), value.strip())

choices = [get_tuple(value) for value in self.choices.split(",")]
if not self.is_required and self.type == "select":
Expand All @@ -383,8 +390,7 @@ def formfield(self):
}
if self.choices:
kwargs["choices"] = self.get_choices()
# The value of individual choices is slugified too.
kwargs["initial"] = slugify(self.default_value)
kwargs["initial"] = self.default_value
return self.get_type(**kwargs)


Expand Down
38 changes: 35 additions & 3 deletions tests/testapp/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def test_forms(self):
)
self.assertContains(response, "<textarea", 1)
self.assertContains(
response, 'value="two-what" required id="id_fc1-radio_1" checked', 1
response, 'value="two what" required id="id_fc1-radio_1" checked', 1
)
self.assertContains(response, 'type="date"', 1)

Expand Down Expand Up @@ -133,9 +133,9 @@ def test_forms(self):
f"fc{form.id}-subject": "Test",
f"fc{form.id}-email": "[email protected]",
f"fc{form.id}-body": "Hello World",
f"fc{form.id}-radio": "two-what",
f"fc{form.id}-radio": "two what",
f"fc{form.id}-date": "2022-10-02",
f"fc{form.id}-multiple-choice": ["choice-a", "choice-c"],
f"fc{form.id}-multiple-choice": ["Choice A", "Choice C"],
},
)

Expand Down Expand Up @@ -407,3 +407,35 @@ def test_email_to_author(self):
self.assertEqual(message.subject, "Test contact form")
self.assertIn("Subject:\nTest\n", message.body)
self.assertIn("Email:\n[email protected]\n", message.body)

def test_choices(self):
"""Legacy slugified choices should also use the pretty format"""

form = Form.objects.create(
title="Test contact form",
config={"save_fs": {}},
)
form.fields.create(
ordering=6,
title="Multiple Choice",
name="multiple-choice",
type="multiple-select",
choices="Choice A,Choice B,Choice C",
is_required=False,
)

s1 = FormSubmission.objects.create(
form=form,
data={
"multiple-choice": ["Choice A", "Choice C"],
},
)
self.assertIn("Multiple Choice:\nChoice A, Choice C", s1.formatted_data())

s2 = FormSubmission.objects.create(
form=form,
data={
"multiple-choice": ["choice-a", "choice-c"],
},
)
self.assertIn("Multiple Choice:\nChoice A, Choice C", s2.formatted_data())

0 comments on commit a6b3b1e

Please sign in to comment.