Skip to content

Commit a003313

Browse files
committed
updated models to support background images
This was mainly added so that a background image could be applied to a protocol/input for the recently added cavas attribute at ReproNim/reproschema-ui#363 Potentially allows for background use in other activities b/c it was easier to minimally implement and/or one could add a background image/logo or something for their own protocol.
1 parent 03763c5 commit a003313

File tree

3 files changed

+110
-1
lines changed

3 files changed

+110
-1
lines changed

reproschema/cli.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,3 +302,9 @@ def reproschema2fhir(reproschema_questionnaire, output):
302302

303303
with open(output_path / f"{file_name}/{file_name}.json", "w+") as f:
304304
f.write(json.dumps(fhir_questionnaire))
305+
306+
307+
if __name__ == "__main__":
308+
import sys
309+
310+
main(prog_name="reproschema")

reproschema/models/model.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@
1010
from pydantic.version import VERSION as PYDANTIC_VERSION
1111

1212
if int(PYDANTIC_VERSION[0]) >= 2:
13-
from pydantic import BaseModel, ConfigDict, Field, field_validator
13+
from pydantic import (
14+
BaseModel,
15+
ConfigDict,
16+
Field,
17+
field_validator,
18+
model_validator,
19+
)
1420
else:
1521
from pydantic import BaseModel, Field, validator
1622
metamodel_version = "None"
@@ -178,6 +184,9 @@ class AdditionalProperty(Thing):
178184
An object to describe the various properties added to assessments and Items.
179185
"""
180186

187+
# Override parent's extra="forbid" to allow extra fields
188+
model_config = ConfigDict(extra="allow")
189+
181190
allow: Optional[List[AllowedType]] = Field(
182191
default_factory=list,
183192
title="allow",
@@ -229,6 +238,14 @@ class AdditionalProperty(Thing):
229238
title="UI",
230239
description="An element to control UI specifications. Originally @nest in jsonld, but using a class in the model.",
231240
)
241+
242+
# Add backgroundImage as an explicit field to allow it
243+
backgroundImage: Optional[str] = Field(
244+
None,
245+
title="backgroundImage",
246+
description="Background image for drawing activities.",
247+
)
248+
232249
id: Optional[str] = Field(
233250
None,
234251
description="A unique identifier for an entity. Must be either a CURIE shorthand for a URI or a complete URI.",

reproschema/models/tests/test_schema.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import pytest
66
from pyld import jsonld
7+
from pydantic import ValidationError
78

89
from ...jsonldutils import load_file
910
from ...utils import start_server, stop_server
@@ -176,3 +177,88 @@ def test_item(tmp_path, server_http_kwargs):
176177
)
177178
del data_comp["@context"]
178179
assert item_dict == data_comp
180+
181+
182+
def test_canvas_activity_allows_extra_fields():
183+
"""Test that canvas activities allow backgroundImage (now an explicit field)."""
184+
activity_dict = {
185+
"category": "Activity",
186+
"id": "canvas_activity.jsonld",
187+
"prefLabel": {"en": "Canvas Drawing Activity"},
188+
"description": {"en": "A canvas drawing activity"},
189+
"schemaVersion": "1.0.0-rc4",
190+
"version": "0.0.1",
191+
"ui": {
192+
"inputType": "canvas",
193+
"addProperties": [
194+
{
195+
"isAbout": "item1",
196+
"variableName": "canvas_item",
197+
"backgroundImage": "./images/drawaclock.png",
198+
}
199+
],
200+
},
201+
}
202+
203+
# This should work fine
204+
activity_obj = Activity(**activity_dict)
205+
assert (
206+
activity_obj.ui.addProperties[0].backgroundImage
207+
== "./images/drawaclock.png"
208+
)
209+
210+
211+
def test_background_image_always_allowed():
212+
"""Test that backgroundImage is always allowed regardless of inputType."""
213+
activity_dict = {
214+
"category": "Activity",
215+
"id": "radio_activity_with_background.jsonld",
216+
"prefLabel": {"en": "Radio Button Activity with Background"},
217+
"description": {"en": "A radio button activity with background image"},
218+
"schemaVersion": "1.0.0-rc4",
219+
"version": "0.0.1",
220+
"ui": {
221+
"inputType": "radio",
222+
"addProperties": [
223+
{
224+
"isAbout": "item1",
225+
"variableName": "radio_item",
226+
# backgroundImage should be allowed for all activities
227+
"backgroundImage": "./images/some_image.png",
228+
}
229+
],
230+
},
231+
}
232+
233+
# This should work fine - backgroundImage is now an explicit field
234+
activity_obj = Activity(**activity_dict)
235+
assert (
236+
activity_obj.ui.addProperties[0].backgroundImage
237+
== "./images/some_image.png"
238+
)
239+
240+
241+
def test_activity_without_extra_fields_works():
242+
"""Test that activities without extra fields work normally regardless of input type."""
243+
activity_dict = {
244+
"category": "Activity",
245+
"id": "normal_activity.jsonld",
246+
"prefLabel": {"en": "Normal Activity"},
247+
"description": {"en": "A normal activity without extra fields"},
248+
"schemaVersion": "1.0.0-rc4",
249+
"version": "0.0.1",
250+
"ui": {
251+
"inputType": "radio",
252+
"addProperties": [
253+
{
254+
"isAbout": "item1",
255+
"variableName": "normal_item",
256+
# No extra fields
257+
}
258+
],
259+
},
260+
}
261+
262+
# This should work fine
263+
activity_obj = Activity(**activity_dict)
264+
assert activity_obj.ui.addProperties[0].variableName == "normal_item"

0 commit comments

Comments
 (0)