Skip to content

Commit 98df09a

Browse files
authored
Merge pull request #200 from ecmwf-projects/fix-no-form
Enhance request labels creation
2 parents 40284b3 + 89b4ef2 commit 98df09a

File tree

2 files changed

+117
-18
lines changed

2 files changed

+117
-18
lines changed

cads_processing_api_service/translators.py

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ def make_request_labels(
139139
input_value_ids: Any,
140140
cds_input_schema: dict[str, Any],
141141
) -> list[str]:
142+
if not isinstance(input_value_ids, list):
143+
input_value_ids = [input_value_ids]
142144
if cds_input_schema["type"] in (
143145
"GeographicExtentWidget",
144146
"GeographicExtentMapWidget",
@@ -196,30 +198,54 @@ def make_request_labels_group(
196198

197199

198200
def translate_request_ids_into_labels(
199-
request: dict[str, Any], cds_form: list[Any] | dict[str, Any]
201+
request: dict[str, Any], cds_form: list[Any] | dict[str, Any] | None
200202
) -> dict[str, Any]:
203+
"""Translate request input values into labels."""
204+
if cds_form is None:
205+
cds_form = {}
201206
if not isinstance(cds_form, list):
202207
cds_form = [cds_form]
203-
request_labels = {}
208+
# This will include in the labels the input keys that are not associated with
209+
# any cds_input_schema in the cds_form
210+
request_labels: dict[str, Any] = {
211+
input_key_id: str(input_value_id)
212+
for input_key_id, input_value_id in request.items()
213+
}
214+
exclusive_group_widgets_children = []
215+
for cds_input_schema in cds_form:
216+
if cds_input_schema.get("type", None) == "ExclusiveGroupWidget":
217+
exclusive_group_widgets_children.extend(cds_input_schema["children"])
204218
for cds_input_schema in cds_form:
205-
if cds_input_schema["type"] == "ExclusiveGroupWidget":
219+
cds_input_schema_name = cds_input_schema.get("name", None)
220+
if cds_input_schema_name in exclusive_group_widgets_children:
221+
continue
222+
if cds_input_schema.get("type", None) == "ExclusiveGroupWidget":
206223
input_key_label = cds_input_schema["label"]
207224
children = cds_input_schema["children"]
208-
default = cds_input_schema["details"]["default"]
225+
if keys_to_remove := list(set(request_labels.keys()) & set(children)):
226+
for key_to_remove in keys_to_remove:
227+
del request_labels[key_to_remove]
228+
default = cds_input_schema.get("details", {}).get("default", None)
209229
request_labels[input_key_label] = make_request_labels_group(
210230
request, children, default, cds_form
211231
)
212232
else:
213-
input_key_id = cds_input_schema["name"]
214-
if input_key_id in request:
215-
input_key_label = cds_input_schema["label"]
233+
input_key_id = cds_input_schema.get("name", None)
234+
input_key_label = cds_input_schema.get("label", None)
235+
if input_key_id in request_labels:
236+
del request_labels[input_key_id]
216237
input_value_ids = request[input_key_id]
217-
if not isinstance(input_value_ids, list):
218-
input_value_ids = [input_value_ids]
219-
request_labels[input_key_label] = make_request_labels(
220-
input_value_ids, cds_input_schema
221-
)
222-
238+
elif default_value_ids := cds_input_schema.get("details", {}).get(
239+
"default", None
240+
):
241+
input_value_ids = default_value_ids
242+
else:
243+
continue
244+
if not isinstance(input_value_ids, list):
245+
input_value_ids = [input_value_ids]
246+
request_labels[input_key_label] = make_request_labels(
247+
input_value_ids, cds_input_schema
248+
)
223249
return request_labels
224250

225251

tests/test_10_translators.py

Lines changed: 78 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@
2020

2121
TEST_INPUT_CDS_SCHEMAS: dict[str, Any] = {
2222
"string_list": {
23+
"name": "string_list",
24+
"label": "String List",
2325
"details": {"labels": {"val1": "Val1", "val2": "Val2", "val3": "Val3"}},
2426
"type": "StringListWidget",
2527
},
2628
"string_list_array": {
29+
"name": "string_list_array",
30+
"label": "String List Array",
2731
"details": {
2832
"groups": [
2933
{"labels": {"val1": "Val1", "val2": "Val2"}},
@@ -33,21 +37,29 @@
3337
"type": "StringListArrayWidget",
3438
},
3539
"string_choice": {
40+
"name": "string_choice",
41+
"label": "String Choice",
3642
"details": {
3743
"labels": {"val1": "Val1", "val2": "Val2", "val3": "Val3"},
3844
"default": "val1",
3945
},
4046
"type": "StringChoiceWidget",
4147
},
4248
"geographic_extent_map": {
49+
"name": "geographic_extent_map",
50+
"label": "Geographic Extent Map",
4351
"details": {"default": [1, 2, 3, 4]},
4452
"type": "GeographicExtentMapWidget",
4553
},
4654
"geographic_location": {
55+
"name": "geographic_location",
56+
"label": "Geographic Location",
4757
"details": {},
4858
"type": "GeographicLocationWidget",
4959
},
5060
"string_list_array_groups": {
61+
"name": "string_list_array_groups",
62+
"label": "String List Array Groups",
5163
"details": {
5264
"groups": [
5365
{
@@ -67,9 +79,30 @@
6779
"type": "StringListArrayWidget",
6880
},
6981
"free_edition_widget": {
82+
"name": "free_edition_widget",
83+
"label": "Free Edition Widget",
7084
"type": "FreeEditionWidget",
7185
"details": {},
7286
},
87+
"exclusive_group_widget": {
88+
"name": "exclusive_group_widget",
89+
"label": "Exclusive Group Widget",
90+
"type": "ExclusiveGroupWidget",
91+
"children": ["child_1", "child_2"],
92+
"details": {"default": "child_1"},
93+
},
94+
"child_1": {
95+
"name": "child_1",
96+
"label": "Child 1",
97+
"type": "Child1Widget",
98+
"details": {},
99+
},
100+
"child_2": {
101+
"name": "child_2",
102+
"label": "Child 2",
103+
"type": "Child2Widget",
104+
"details": {},
105+
},
73106
}
74107

75108

@@ -143,7 +176,6 @@ def test_translate_string_list() -> None:
143176
res_output = cads_processing_api_service.translators.translate_string_list(
144177
test_input
145178
)
146-
147179
assert res_output == exp_ouput
148180

149181

@@ -156,7 +188,6 @@ def test_translate_string_list_array() -> None:
156188
res_output = cads_processing_api_service.translators.translate_string_list_array(
157189
test_input
158190
)
159-
160191
assert res_output == exp_ouput
161192

162193
test_input = TEST_INPUT_CDS_SCHEMAS["string_list_array_groups"]
@@ -170,7 +201,6 @@ def test_translate_string_list_array() -> None:
170201
res_output = cads_processing_api_service.translators.translate_string_list_array(
171202
test_input
172203
)
173-
174204
assert res_output == exp_ouput
175205

176206

@@ -180,7 +210,6 @@ def test_translate_string_choice() -> None:
180210
res_output = cads_processing_api_service.translators.translate_string_choice(
181211
test_input
182212
)
183-
184213
assert res_output == exp_ouput
185214

186215

@@ -198,7 +227,6 @@ def test_translate_geographic_extent_map() -> None:
198227
test_input
199228
)
200229
)
201-
202230
assert res_output == exp_ouput
203231

204232

@@ -236,6 +264,51 @@ def test_make_request_labels() -> None:
236264
assert res_output == exp_output
237265

238266

267+
def test_translate_request_ids_into_labels() -> None:
268+
request = {"key1": "val1", "key2": "val2"}
269+
cds_schema = None
270+
exp_output = {"key1": "val1", "key2": "val2"}
271+
res_output = (
272+
cads_processing_api_service.translators.translate_request_ids_into_labels(
273+
request, cds_schema
274+
)
275+
)
276+
assert res_output == exp_output
277+
278+
request = {
279+
"string_list": ["val1", "val2"],
280+
"string_choice": "val1",
281+
"unknown_key": "unknown_value",
282+
}
283+
cds_schema = [
284+
TEST_INPUT_CDS_SCHEMAS["string_list"],
285+
TEST_INPUT_CDS_SCHEMAS["string_choice"],
286+
]
287+
exp_output = {
288+
"String List": ["Val1", "Val2"],
289+
"String Choice": ["Val1"],
290+
"unknown_key": "unknown_value",
291+
}
292+
res_output = (
293+
cads_processing_api_service.translators.translate_request_ids_into_labels(
294+
request, cds_schema
295+
)
296+
)
297+
assert res_output == exp_output
298+
299+
request = {}
300+
cds_schema = [
301+
TEST_INPUT_CDS_SCHEMAS["string_choice"],
302+
TEST_INPUT_CDS_SCHEMAS["exclusive_group_widget"],
303+
TEST_INPUT_CDS_SCHEMAS["child_1"],
304+
TEST_INPUT_CDS_SCHEMAS["child_2"],
305+
]
306+
exp_output = {
307+
"String Choice": ["Val1"],
308+
"Exclusive Group Widget": ["Child 1"],
309+
}
310+
311+
239312
def test_format_request_value() -> None:
240313
test_value_1 = "test_value"
241314
exp_output_1 = "'test_value'"

0 commit comments

Comments
 (0)