From 61df6f722fccb710bf10c202c4cb58846b7c0061 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Fri, 13 Apr 2018 10:04:31 +0200 Subject: [PATCH] fields: consider the empty string as a valid (null) date (#23162) --- tests/test_hobo_notify.py | 2 +- tests/test_workflows.py | 14 ++++++++------ wcs/fields.py | 2 +- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/tests/test_hobo_notify.py b/tests/test_hobo_notify.py index a7409afb..1a497062 100644 --- a/tests/test_hobo_notify.py +++ b/tests/test_hobo_notify.py @@ -582,7 +582,7 @@ def test_process_notification_user_provision(pub): } CmdHoboNotify.process_notification(notification) assert User.count() == 1 - if birthdate is not None: # wrong value : no nothing + if birthdate not in (None, ''): # wrong value : no nothing assert User.select()[0].form_data['_birthdate'].tm_year == 2000 else: # empty value : empty field assert User.select()[0].form_data['_birthdate'] is None diff --git a/tests/test_workflows.py b/tests/test_workflows.py index 8bafebc9..6c2ab4cb 100644 --- a/tests/test_workflows.py +++ b/tests/test_workflows.py @@ -2919,13 +2919,15 @@ def test_profile(two_pubs): assert http_patch_request.call_count == 1 assert http_patch_request.call_args[0][1] == '{"bar": "2018-03-20"}' - user.form_data['4'] = datetime.datetime.now().timetuple() - user.store() - year = User.get(user.id).form_data.get('4').tm_year for date_value in ('baddate', '', {}, [], None): + # reset date to a known value + user.form_data['4'] = datetime.datetime.now().timetuple() + user.store() + year = User.get(user.id).form_data.get('4').tm_year + # perform action item.fields = [{'field_id': 'bar', 'value': date_value}] item.perform(formdata) - if date_value is not None: # bad value : do nothing + if date_value not in (None, ''): # bad value : do nothing assert User.get(user.id).form_data.get('4').tm_year == year else: # empty value : empty field assert User.get(user.id).form_data.get('4') == None @@ -2934,7 +2936,7 @@ def test_profile(two_pubs): http_patch_request.return_value = (None, 200, '', None) get_response().process_after_jobs() assert http_patch_request.call_count == 1 - if date_value is not None: # bad value : do nothing + if date_value not in (None, ''): # bad value : do nothing assert http_patch_request.call_args[0][1] == '{}' else: # empty value : null field assert http_patch_request.call_args[0][1] == '{"bar": null}' @@ -3282,7 +3284,7 @@ def test_set_backoffice_field_date(two_pubs): assert datetime.date(*formdata.data['bo1'][:3]) == datetime.date(2017, 3, 23) # invalid values => do nothing - for value in ('plop', '', {}, [], '{{ blah }}'): + for value in ('plop', {}, []): item = SetBackofficeFieldsWorkflowStatusItem() item.parent = st1 item.fields = [{'field_id': 'bo1', 'value': value}] diff --git a/wcs/fields.py b/wcs/fields.py index 9ce751f6..0adcfbef 100644 --- a/wcs/fields.py +++ b/wcs/fields.py @@ -980,7 +980,7 @@ class DateField(WidgetField): @classmethod def convert_value_from_anything(cls, value): - if value is None: + if value is None or value == '': return None date_value = evalutils.make_date(value).timetuple() # could raise ValueError return date_value -- 2.17.0