Skip to content

Commit 55bf186

Browse files
committed
add unitOption
1 parent 743fd34 commit 55bf186

File tree

4 files changed

+111
-39
lines changed

4 files changed

+111
-39
lines changed

reproschema/models/base.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,6 @@ def _default_context(self) -> str:
156156
Protocol, Activity, Field
157157
"""
158158
# associatedMedia
159-
# video
160-
# audio
161159
prefLabel: Optional[dict] = field(
162160
factory=(dict),
163161
converter=default_if_none(default={}), # type: ignore
@@ -228,7 +226,6 @@ def _default_context(self) -> str:
228226
"""
229227
Field only
230228
"""
231-
# TODO additionalNotesObj
232229
inputType: Optional[str] = field(
233230
factory=(str),
234231
converter=default_if_none(default=""), # type: ignore

reproschema/models/response_options.py

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,49 @@
1616

1717
from .base import DEFAULT_LANG
1818
from .base import DEFAULT_VERSION
19-
from .utils import reorder_dict_skip_missing
2019
from .utils import SchemaUtils
2120

2221

22+
@define(kw_only=True)
23+
class unitOption(SchemaUtils):
24+
25+
value: Any = field(default=None)
26+
prefLabel: Optional[Union[str, Dict[str, str]]] = field(
27+
factory=(dict),
28+
converter=default_if_none(default={}), # type: ignore
29+
validator=optional(instance_of((dict, str))),
30+
)
31+
lang: Optional[str] = field(
32+
default=None,
33+
converter=default_if_none(default=DEFAULT_LANG()), # type: ignore
34+
validator=optional(instance_of(str)),
35+
)
36+
37+
def __attrs_post_init__(self) -> None:
38+
39+
if self.schema_order in [None, []]:
40+
self.schema_order = [
41+
"prefLabel",
42+
"value",
43+
]
44+
if isinstance(self.prefLabel, str):
45+
self.prefLabel = {self.lang: self.prefLabel}
46+
47+
self.update()
48+
self.sort_schema()
49+
50+
def set_pref_label(
51+
self, pref_label: Optional[str] = None, lang: Optional[str] = None
52+
) -> None:
53+
if pref_label is None:
54+
return
55+
if lang is None:
56+
lang = self.lang
57+
58+
self.prefLabel[lang] = pref_label
59+
self.update()
60+
61+
2362
@define(kw_only=True)
2463
class Choice(SchemaUtils):
2564

@@ -131,10 +170,10 @@ def _default_context(self) -> str:
131170
validator=optional(instance_of(int)),
132171
)
133172

134-
unitOptions: Optional[int] = field(
173+
unitOptions: Optional[list] = field(
135174
default=None,
136-
converter=default_if_none(default=""), # type: ignore
137-
validator=optional(instance_of(str)),
175+
converter=default_if_none(default=[]), # type: ignore
176+
validator=optional(instance_of(list)),
138177
)
139178

140179
unitCode: Optional[str] = field(

reproschema/models/tests/data/items/item2.jsonld

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
"@context": "../../../contexts/generic",
33
"@type": "reproschema:Field",
44
"@id": "item2.jsonld",
5-
"prefLabel": "item2",
5+
"prefLabel": {
6+
"en": "item2"
7+
},
68
"description": "Q2 of example 1",
79
"schemaVersion": "1.0.0-rc4",
810
"version": "0.0.1",
@@ -11,30 +13,29 @@
1113
"es": "Fiebre actual."
1214
},
1315
"video": {
14-
"@type": "VideoObject",
15-
"contentUrl": "http://media.freesound.org/data/0/previews/719__elmomo__12oclock_girona_preview.mp4"
16+
"@type": "VideoObject",
17+
"contentUrl": "http://media.freesound.org/data/0/previews/719__elmomo__12oclock_girona_preview.mp4"
1618
},
17-
"imageUrl": "http://example.com/sample-image.jpg",
1819
"ui": {
1920
"inputType": "float"
2021
},
2122
"responseOptions": {
2223
"valueType": "xsd:float",
2324
"unitOptions": [
24-
{
25-
"prefLabel": {
26-
"en": "Fahrenheit",
27-
"es": "Fahrenheit"
28-
},
29-
"value": "°F"
30-
},
31-
{
32-
"prefLabel": {
33-
"en": "Celsius",
34-
"es": "Celsius"
25+
{
26+
"prefLabel": {
27+
"en": "Fahrenheit",
28+
"es": "Fahrenheit"
29+
},
30+
"value": "°F"
3531
},
36-
"value": "°C"
37-
}
32+
{
33+
"prefLabel": {
34+
"en": "Celsius",
35+
"es": "Celsius"
36+
},
37+
"value": "°C"
38+
}
3839
]
3940
}
4041
}

reproschema/models/tests/test_item.py

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99

1010
from reproschema.models.base import AdditionalNoteObj
1111
from reproschema.models.item import Item
12-
from reproschema.models.item import ResponseOption
12+
from reproschema.models.response_options import ResponseOption
13+
from reproschema.models.response_options import unitOption
1314

1415
item_dir = output_dir("items")
1516

@@ -171,7 +172,6 @@ def test_item1():
171172
column="notes", source="redcap", value="some extra note"
172173
).schema
173174
]
174-
print(additionalNotes)
175175
additionalNotes.append(
176176
AdditionalNoteObj(
177177
column="notes",
@@ -180,6 +180,20 @@ def test_item1():
180180
).schema
181181
)
182182

183+
response_options = ResponseOption(multipleChoice=False)
184+
response_options.add_choice(name={"en": "Not at all", "es": "Para nada"}, value=0)
185+
response_options.add_choice(
186+
name={"en": "Several days", "es": "Varios días"}, value="a"
187+
)
188+
response_options.add_choice(
189+
name={"en": "More than half the days", "es": "Más de la mitad de los días"},
190+
value={"@id": "http://example.com/choice3"},
191+
)
192+
response_options.add_choice(
193+
name={"en": "Nearly everyday", "es": "Casi todos los días"},
194+
value={"@value": "choice-with-lang", "@language": "en"},
195+
)
196+
183197
item = Item(
184198
name="item1",
185199
input_type="radio",
@@ -199,21 +213,42 @@ def test_item1():
199213
)
200214
item.at_context = "../../../contexts/generic"
201215
item.set_question(question="Poco interés o placer en hacer cosas", lang="es")
216+
item.set_input_type(response_options=response_options)
202217

203-
response_options = ResponseOption(multipleChoice=False)
204-
response_options.add_choice(name={"en": "Not at all", "es": "Para nada"}, value=0)
205-
response_options.add_choice(
206-
name={"en": "Several days", "es": "Varios días"}, value="a"
207-
)
208-
response_options.add_choice(
209-
name={"en": "More than half the days", "es": "Más de la mitad de los días"},
210-
value={"@id": "http://example.com/choice3"},
211-
)
212-
response_options.add_choice(
213-
name={"en": "Nearly everyday", "es": "Casi todos los días"},
214-
value={"@value": "choice-with-lang", "@language": "en"},
218+
item.write()
219+
220+
item_content, expected = load_jsons(item_dir, item)
221+
assert item_content == expected
222+
223+
clean_up(item_dir, item)
224+
225+
226+
def test_item2():
227+
228+
item = Item(
229+
name="item2",
230+
input_type="float",
231+
description="Q2 of example 1",
232+
question="Current temperature.",
233+
video={
234+
"@type": "VideoObject",
235+
"contentUrl": "http://media.freesound.org/data/0/previews/719__elmomo__12oclock_girona_preview.mp4",
236+
},
237+
read_only=None,
238+
output_dir=item_dir,
215239
)
216-
item.set_input_type(response_options=response_options)
240+
item.at_context = "../../../contexts/generic"
241+
item.set_question(question="Fiebre actual.", lang="es")
242+
243+
unitOption_0 = unitOption(value="°F", prefLabel="Fahrenheit")
244+
unitOption_0.set_pref_label(pref_label="Fahrenheit", lang="es")
245+
246+
unitOption_1 = unitOption(value="°C", prefLabel="Celsius")
247+
unitOption_1.set_pref_label(pref_label="Celsius", lang="es")
248+
249+
item.response_options.unitOptions = [unitOption_0.schema]
250+
item.response_options.unitOptions.append(unitOption_1.schema)
251+
item.set_response_options()
217252

218253
item.write()
219254

0 commit comments

Comments
 (0)