Skip to content

Commit 907f23d

Browse files
arsandhuAnmol Sandhu
andauthored
fix: [DEV-13700] update NewLabelsetArgs to accept fieldData (#384)
Co-authored-by: Anmol Sandhu <anmolsandhu@Mac.cable.rcn.com>
1 parent 30b52a0 commit 907f23d

3 files changed

Lines changed: 91 additions & 3 deletions

File tree

indico/queries/workflow_components.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ def __labelset_to_json(self, labelset: "NewLabelsetArguments") -> "AnyDict":
368368
"datacolumnId": labelset.datacolumn_id,
369369
"taskType": labelset.task_type.name,
370370
"targetNames": labelset.target_names,
371+
"fieldData": labelset.field_data,
371372
}
372373

373374
def __questionnaire_to_json(

indico/types/base.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import inspect
22
import json
33
from datetime import datetime
4+
from enum import Enum
45
from typing import TYPE_CHECKING, Any, List, cast, get_origin
56

67
from indico.types.utils import cc_to_snake
@@ -37,6 +38,7 @@ def valid_type(v: "Any") -> bool:
3738

3839
return (
3940
(inspect.isclass(v) and issubclass(v, BaseType))
41+
or (inspect.isclass(v) and issubclass(v, Enum))
4042
or v in [str, int, float, bool, JSONType, datetime]
4143
or get_origin(v) is dict
4244
or valid_type(list_subtype(v))
@@ -81,7 +83,10 @@ def __init__(self, **kwargs: "Any"):
8183

8284
subtype = list_subtype(attr_type)
8385
if subtype and issubclass(subtype, BaseType):
84-
v = [subtype(**x) for x in cast("Iterable[Any]", v)]
86+
v = [
87+
x if isinstance(x, subtype) else subtype(**x)
88+
for x in cast("Iterable[Any]", v)
89+
]
8590

8691
setattr(self, k, v)
8792

indico/types/model_group.py

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from enum import Enum
2-
from typing import List
2+
from typing import Any, Dict, Iterator, List, Mapping
33

44
from indico.types.base import BaseType
55
from indico.types.model import Model
@@ -72,17 +72,99 @@ class NewQuestionnaireArguments(BaseType):
7272
users: List[int]
7373

7474

75+
class ValidationActionType(Enum):
76+
"""Determines how validation failures are handled."""
77+
78+
NO_ACTION = "NO_ACTION"
79+
WARN = "WARN"
80+
ERROR = "ERROR"
81+
REJECT = "REJECT"
82+
83+
84+
class _ValidationConfig:
85+
"""Base configuration for validation rules."""
86+
87+
setting_name: str
88+
setting_value: Dict[str, Any]
89+
on_failure: ValidationActionType
90+
91+
92+
class ValidationInputConfig(_ValidationConfig, BaseType, Mapping[str, Any]):
93+
"""Configuration that controls which additional validation checks should be run and what actions should be taken in case of their failure."""
94+
95+
def to_json(self) -> Dict[str, Any]:
96+
"""
97+
Convert to JSON serializable format
98+
99+
Returns:
100+
dict: JSON-ready python dictionary
101+
"""
102+
return {
103+
"settingName": self.setting_name,
104+
"settingValue": self.setting_value,
105+
"onFailure": (
106+
self.on_failure.value
107+
if isinstance(self.on_failure, ValidationActionType)
108+
else self.on_failure
109+
),
110+
}
111+
112+
# Implement Mapping interface so instances can be expanded with ** in BaseType
113+
def __iter__(self) -> Iterator[str]:
114+
return iter(self.to_json())
115+
116+
def __len__(self) -> int:
117+
return len(self.to_json())
118+
119+
def __getitem__(self, key: str) -> Any:
120+
return self.to_json()[key]
121+
122+
123+
class _FieldInput:
124+
"""
125+
Basic inputs for a new field
126+
"""
127+
128+
required: bool
129+
multiple: bool
130+
datatype: str
131+
input_config: Dict[str, Any]
132+
format_config: Dict[str, Any]
133+
validation_config: List[ValidationInputConfig]
134+
135+
136+
class FieldInput(_FieldInput, BaseType):
137+
"""Field input with name for review UI and workflow result file"""
138+
139+
name: str
140+
141+
def to_json(self) -> Dict[str, Any]:
142+
return {
143+
"name": self.name,
144+
"required": self.required,
145+
"multiple": self.multiple,
146+
"datatype": self.datatype,
147+
"inputConfig": self.input_config,
148+
"formatConfig": self.format_config,
149+
"validationConfig": [vc.to_json() for vc in self.validation_config]
150+
if self.validation_config
151+
else [],
152+
}
153+
154+
75155
class NewLabelsetArguments:
76156
def __init__(
77157
self,
78158
name: str,
79159
task_type: ModelTaskType,
80160
target_names: List[str],
81161
datacolumn_id: int,
162+
field_data: List[FieldInput],
82163
num_labelers_required: int = 1,
83-
):
164+
) -> None:
84165
self.name = name
85166
self.num_labelers_required = num_labelers_required
86167
self.task_type = task_type
87168
self.target_names = target_names
88169
self.datacolumn_id = datacolumn_id
170+
self.field_data = [f.to_json() for f in field_data]

0 commit comments

Comments
 (0)