|
1 | 1 | from enum import Enum |
2 | | -from typing import List |
| 2 | +from typing import Any, Dict, Iterator, List, Mapping |
3 | 3 |
|
4 | 4 | from indico.types.base import BaseType |
5 | 5 | from indico.types.model import Model |
@@ -72,17 +72,99 @@ class NewQuestionnaireArguments(BaseType): |
72 | 72 | users: List[int] |
73 | 73 |
|
74 | 74 |
|
| 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 | + |
75 | 155 | class NewLabelsetArguments: |
76 | 156 | def __init__( |
77 | 157 | self, |
78 | 158 | name: str, |
79 | 159 | task_type: ModelTaskType, |
80 | 160 | target_names: List[str], |
81 | 161 | datacolumn_id: int, |
| 162 | + field_data: List[FieldInput], |
82 | 163 | num_labelers_required: int = 1, |
83 | | - ): |
| 164 | + ) -> None: |
84 | 165 | self.name = name |
85 | 166 | self.num_labelers_required = num_labelers_required |
86 | 167 | self.task_type = task_type |
87 | 168 | self.target_names = target_names |
88 | 169 | self.datacolumn_id = datacolumn_id |
| 170 | + self.field_data = [f.to_json() for f in field_data] |
0 commit comments