Skip to content

Commit

Permalink
Convert choices back to the unslugified variant
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiask committed Sep 3, 2024
1 parent 1b33b80 commit f59ed76
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Change log
Next version
------------

* Changed form submissions to convert choice values back to the unslugified
variant.

0.25
----

Expand Down
13 changes: 7 additions & 6 deletions form_designer/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,20 +176,21 @@ def submissions_data(self, *, submissions=None):
if submissions is None:
submissions = self.submissions.all()

def loader(submission, field):
def loader(submission, field, choice_dict):
value = None
if field.name in submission.data:
return submission.data[field.name]
if (old := field._old_name) is not None and old in submission.data:
return submission.data[old]
return None
value = submission.data[field.name]
elif (old := field._old_name) is not None and old in submission.data:
value = submission.data[old]
return choice_dict.get(value, value)

def old_name_loader(submission, old_name):
return submission.data.get(old_name)

fields_and_loaders = [
(
{"name": field.name, "title": field.title},
partial(loader, field=field),
partial(loader, field=field, choice_dict=dict(field.get_choices())),
)
for field in self.fields.all()
]
Expand Down
2 changes: 1 addition & 1 deletion tests/testapp/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def test_forms(self):
"title": "Please call me",
"value": False,
},
{"name": "radio", "title": "Radio Select", "value": "two-what"},
{"name": "radio", "title": "Radio Select", "value": "two what"},
{"name": "date", "title": "Date", "value": "2022-10-02"},
],
}
Expand Down

0 comments on commit f59ed76

Please sign in to comment.