Projet

Général

Profil

0001-fields-consider-the-empty-string-as-a-valid-null-dat.patch

Frédéric Péters, 13 avril 2018 10:05

Télécharger (3,61 ko)

Voir les différences:

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(-)
tests/test_hobo_notify.py
582 582
        }
583 583
        CmdHoboNotify.process_notification(notification)
584 584
        assert User.count() == 1
585
        if birthdate is not None:  # wrong value : no nothing
585
        if birthdate not in (None, ''):  # wrong value : no nothing
586 586
            assert User.select()[0].form_data['_birthdate'].tm_year == 2000
587 587
        else:  # empty value : empty field
588 588
            assert User.select()[0].form_data['_birthdate'] is None
tests/test_workflows.py
2919 2919
            assert http_patch_request.call_count == 1
2920 2920
            assert http_patch_request.call_args[0][1] == '{"bar": "2018-03-20"}'
2921 2921

  
2922
    user.form_data['4'] = datetime.datetime.now().timetuple()
2923
    user.store()
2924
    year = User.get(user.id).form_data.get('4').tm_year
2925 2922
    for date_value in ('baddate', '', {}, [], None):
2923
        # reset date to a known value
2924
        user.form_data['4'] = datetime.datetime.now().timetuple()
2925
        user.store()
2926
        year = User.get(user.id).form_data.get('4').tm_year
2927
        # perform action
2926 2928
        item.fields = [{'field_id': 'bar', 'value': date_value}]
2927 2929
        item.perform(formdata)
2928
        if date_value is not None:  # bad value : do nothing
2930
        if date_value not in (None, ''):  # bad value : do nothing
2929 2931
            assert User.get(user.id).form_data.get('4').tm_year == year
2930 2932
        else:  # empty value : empty field
2931 2933
            assert User.get(user.id).form_data.get('4') == None
......
2934 2936
            http_patch_request.return_value = (None, 200, '', None)
2935 2937
            get_response().process_after_jobs()
2936 2938
            assert http_patch_request.call_count == 1
2937
            if date_value is not None:  # bad value : do nothing
2939
            if date_value not in (None, ''):  # bad value : do nothing
2938 2940
                assert http_patch_request.call_args[0][1] == '{}'
2939 2941
            else:  # empty value : null field
2940 2942
                assert http_patch_request.call_args[0][1] == '{"bar": null}'
......
3282 3284
    assert datetime.date(*formdata.data['bo1'][:3]) == datetime.date(2017, 3, 23)
3283 3285

  
3284 3286
    # invalid values => do nothing
3285
    for value in ('plop', '', {}, [], '{{ blah }}'):
3287
    for value in ('plop', {}, []):
3286 3288
        item = SetBackofficeFieldsWorkflowStatusItem()
3287 3289
        item.parent = st1
3288 3290
        item.fields = [{'field_id': 'bo1', 'value': value}]
wcs/fields.py
980 980

  
981 981
    @classmethod
982 982
    def convert_value_from_anything(cls, value):
983
        if value is None:
983
        if value is None or value == '':
984 984
            return None
985 985
        date_value = evalutils.make_date(value).timetuple()  # could raise ValueError
986 986
        return date_value
987
-