1717# limitations under the License.
1818"""Activity JSON-LD schema."""
1919
20- from marshmallow import EXCLUDE
20+ from marshmallow import EXCLUDE , pre_dump
2121
2222from renku .core .commands .schema .agent import PersonSchema , SoftwareAgentSchema
2323from renku .core .commands .schema .annotation import AnnotationSchema
24- from renku .core .commands .schema .calamus import JsonLDSchema , Nested , fields , oa , prov , renku
24+ from renku .core .commands .schema .calamus import JsonLDSchema , Nested , fields , oa , prov , renku , schema
2525from renku .core .commands .schema .entity import CollectionSchema , EntitySchema
26- from renku .core .commands .schema .parameter import ParameterValueSchema
2726from renku .core .commands .schema .plan import PlanSchema
2827from renku .core .models .provenance .activity import Activity , Association , Generation , Usage
28+ from renku .core .models .provenance .parameter import ParameterValue
2929
3030NON_EXISTING_ENTITY_CHECKSUM = "0" * 40
3131
3232
33+ class _ObjectWrapper :
34+ """Object wrapper that allows temporarily overriding fields of immutable objects."""
35+
36+ def __init__ (self , wrapped , ** override ):
37+ self .__wrapped = wrapped
38+ self .__override = override
39+
40+ def __getattr__ (self , name ):
41+ if name in self .__override :
42+ return self .__override [name ]
43+
44+ return getattr (self .__wrapped , name )
45+
46+
47+ def _fix_id (obj ):
48+ """Fix ids under an activity that were wrong due to a bug."""
49+
50+ if not obj .id .startswith ("/activities/" ):
51+ obj = _ObjectWrapper (obj , id = f"/activities/{ obj .id } " )
52+
53+ return obj
54+
55+
3356class AssociationSchema (JsonLDSchema ):
3457 """Association schema."""
3558
@@ -44,6 +67,11 @@ class Meta:
4467 id = fields .Id ()
4568 plan = Nested (prov .hadPlan , PlanSchema )
4669
70+ @pre_dump
71+ def _pre_dump (self , obj , ** kwargs ):
72+ """Pre-dump hook."""
73+ return _fix_id (obj )
74+
4775
4876class UsageSchema (JsonLDSchema ):
4977 """Usage schema."""
@@ -59,6 +87,11 @@ class Meta:
5987 # TODO: DatasetSchema, DatasetFileSchema
6088 entity = Nested (prov .entity , [EntitySchema , CollectionSchema ])
6189
90+ @pre_dump
91+ def _pre_dump (self , obj , ** kwargs ):
92+ """Pre-dump hook."""
93+ return _fix_id (obj )
94+
6295
6396class GenerationSchema (JsonLDSchema ):
6497 """Generation schema."""
@@ -74,6 +107,32 @@ class Meta:
74107 # TODO: DatasetSchema, DatasetFileSchema
75108 entity = Nested (prov .qualifiedGeneration , [EntitySchema , CollectionSchema ], reverse = True )
76109
110+ @pre_dump
111+ def _pre_dump (self , obj , ** kwargs ):
112+ """Pre-dump hook."""
113+ return _fix_id (obj )
114+
115+
116+ class ParameterValueSchema (JsonLDSchema ):
117+ """ParameterValue schema."""
118+
119+ class Meta :
120+ """Meta class."""
121+
122+ rdf_type = [renku .ParameterValue , schema .PropertyValue ]
123+ model = ParameterValue
124+ unknown = EXCLUDE
125+
126+ id = fields .Id ()
127+
128+ parameter = fields .IRI (schema .valueReference , attribute = "parameter_id" )
129+ value = fields .Raw (schema .value )
130+
131+ @pre_dump
132+ def _pre_dump (self , obj , ** kwargs ):
133+ """Pre-dump hook."""
134+ return _fix_id (obj )
135+
77136
78137class ActivitySchema (JsonLDSchema ):
79138 """Activity schema."""
@@ -102,3 +161,8 @@ class Meta:
102161 project_id = fields .IRI (renku .hasActivity , reverse = True )
103162 started_at_time = fields .DateTime (prov .startedAtTime , add_value_types = True )
104163 usages = Nested (prov .qualifiedUsage , UsageSchema , many = True )
164+
165+ @pre_dump
166+ def _pre_dump (self , obj , ** kwargs ):
167+ """Pre-dump hook."""
168+ return _fix_id (obj )
0 commit comments