def formdata_to_payload(cls, form_dict): """Formats the result of a forecast webform into an API payload. Parameters ---------- form_dict: dict The posted form data parsed into a dict. Returns ------- dictionary Form data formatted to the API spec. """ fx_metadata = {} fx_metadata = { key: form_dict[key] for key in cls.direct_keys if form_dict.get(key) is not None } fx_metadata.update(utils.get_location_id(form_dict)) fx_metadata['issue_time_of_day'] = utils.parse_hhmm_field_from_form( form_dict, 'issue_time_of_day') fx_metadata['lead_time_to_start'] = utils.parse_timedelta_from_form( form_dict, 'lead_time_to_start') fx_metadata['run_length'] = utils.parse_timedelta_from_form( form_dict, 'run_length') fx_metadata['interval_length'] = utils.parse_timedelta_from_form( form_dict, 'interval_length') return fx_metadata
def payload_to_formdata(cls, payload_dict): """Converts an forecast metadata dict into a form_data dict. Parameters ---------- payload: dict API forecast metadata json repsonse as a dict. Returns ------- dict A dictionary for filling out form fields where keys are input name attributes. """ form_dict = {key: payload_dict[key] for key in cls.direct_keys} form_dict.update(utils.get_location_id(payload_dict)) form_dict.update( utils.parse_hhmm_field_from_api(payload_dict, 'issue_time_of_day')) form_dict.update( utils.parse_timedelta_from_api(payload_dict, 'lead_time_to_start')) form_dict.update( utils.parse_timedelta_from_api(payload_dict, 'run_length')) form_dict.update( utils.parse_timedelta_from_api(payload_dict, 'interval_length')) return form_dict
def test_set_location_id_no_location_key(): assert utils.get_location_id({}) == {}
def test_get_location_id_only_location_keys(from_dict): for key in list(utils.get_location_id(from_dict).keys()): assert key in ['aggregate_id', 'site_id']
def test_get_location_id(from_dict): assert utils.get_location_id(from_dict) == from_dict