Use unambiguously magic value for None choices

WTForms coerces `None` as a choice to `'None'` as a string when
rendering form fields (form fields will only ever have string data
because that what the browser posts back).

But internally WTForms coerces `None` to mean an unset value, ie where
the user hasn’t selected a radio button:
283b280320/src/wtforms/utils.py (L1-L20)

We shouldn’t use `None` to mean two different things. And in fact we
can’t, because it in effect means that we’re always getting a value
for the `move_to` field, even if the user hasn’t chosen to move any
templates. Which results in some very expected behaviour.
This commit is contained in:
Chris Hill-Scott
2018-12-03 17:29:23 +00:00
parent 4c148c7c23
commit 2fcf278a5b
3 changed files with 39 additions and 17 deletions

View File

@@ -2188,12 +2188,12 @@ def test_set_postage_saves(
@pytest.mark.parametrize('current_branding, expected_values, expected_labels', [
(None, [
'None', '1', '2', '3', '4', '5',
'__NONE__', '1', '2', '3', '4', '5',
], [
'GOV.UK', 'org 1', 'org 2', 'org 3', 'org 4', 'org 5'
]),
('5', [
'5', 'None', '1', '2', '3', '4',
'5', '__NONE__', '1', '2', '3', '4',
], [
'org 5', 'GOV.UK', 'org 1', 'org 2', 'org 3', 'org 4',
]),
@@ -2314,7 +2314,8 @@ def test_should_preview_email_branding(
@pytest.mark.parametrize('posted_value, submitted_value', (
('1', '1'),
('None', None),
('__NONE__', None),
pytest.param('None', None, marks=pytest.mark.xfail(raises=AssertionError)),
))
def test_should_set_branding_and_organisations(
logged_in_platform_admin_client,

View File

@@ -901,7 +901,7 @@ def test_should_be_able_to_move_a_sub_item(
template_folder_id=PARENT_FOLDER_ID,
_data={
'operation': 'move_to_existing_folder',
'move_to': 'None',
'move_to': '__NONE__',
'templates_and_folders': [GRANDCHILD_FOLDER_ID],
},
_expected_status=302,
@@ -929,6 +929,13 @@ def test_should_be_able_to_move_a_sub_item(
'move_to_new_folder_name': 'foo',
'move_to': PARENT_FOLDER_ID
},
# move to existing, but no templates to move
{
'operation': 'move_to_existing_folder',
'templates_and_folders': [],
'move_to_new_folder_name': '',
'move_to': PARENT_FOLDER_ID
},
# move to new, but nothing selected to move
{
'operation': 'move_to_new_folder',
@@ -944,6 +951,14 @@ def test_should_be_able_to_move_a_sub_item(
'move_to': PARENT_FOLDER_ID,
'add_template_by_template_type': 'email',
},
# add a new template, but also move to root folder
{
'operation': 'add_template',
'templates_and_folders': [],
'move_to_new_folder_name': '',
'move_to': '__NONE__',
'add_template_by_template_type': 'email',
},
])
def test_no_action_if_user_fills_in_ambiguous_fields(
client_request,