From 7391b18f729de7a1588da9be45b3f5a3dc1e528e Mon Sep 17 00:00:00 2001 From: Henrique Fonseca Date: Thu, 29 Aug 2024 12:25:23 -0400 Subject: [PATCH] fixed issue with schema relationship link fields --- dinapy/schemas/BaseSchema.py | 6 +++--- dinapy/schemas/collectingeventschema.py | 23 +++++++++++++++++++---- dinapy/schemas/materialsampleschema.py | 24 ++++++++++++------------ dinapy/schemas/metadata_schema.py | 6 +++--- dinapy/schemas/storageunitusageschema.py | 4 ++-- tests/collecting_event_schema_test.py | 6 +++++- 6 files changed, 44 insertions(+), 25 deletions(-) diff --git a/dinapy/schemas/BaseSchema.py b/dinapy/schemas/BaseSchema.py index 3545efb..332c516 100644 --- a/dinapy/schemas/BaseSchema.py +++ b/dinapy/schemas/BaseSchema.py @@ -5,11 +5,11 @@ class BaseSchema(Schema): id = fields.Str(dump_only=True,allow_none=True) type = fields.Str(allow_none=True) -def create_relationship(type, key=None): +def create_relationship(parent_type, type, key=None): return fields.Relationship( - self_url=f"/api/v1/material-sample/{{id}}/relationships/{key if key else type}", + self_url=f"/api/v1/{parent_type}/{{id}}/relationships/{key if key else type}", self_url_kwargs={"id": ""}, - related_url=f"/api/v1/material-sample/{{id}}/{key if key else type}", + related_url=f"/api/v1/{parent_type}/{{id}}/{key if key else type}", related_url_kwargs={"id": ""}, allow_none=True, include_resource_linkage=True, diff --git a/dinapy/schemas/collectingeventschema.py b/dinapy/schemas/collectingeventschema.py index e42e968..ca80e8a 100644 --- a/dinapy/schemas/collectingeventschema.py +++ b/dinapy/schemas/collectingeventschema.py @@ -11,6 +11,21 @@ class Protocol(BaseSchema): class Meta: type_ = 'protocol' + +class CollectionMethod(BaseSchema): + + class Meta: + type_ = 'collection-method' + +class Collectors(BaseSchema): + + class Meta: + type_ = 'collectors' + +class Attachment(BaseSchema): + + class Meta: + type_ = 'attachment' class CollectingEventSchema(Schema): '''Schema for a Collecting Event used for serializing and deserializing JSON.''' @@ -55,10 +70,10 @@ class CollectingEventSchema(Schema): managedAttributes = SkipUndefinedField(fields.Dict,attribute="attributes.managedAttributes") remarks = SkipUndefinedField(fields.Str,allow_none=True, attribute="attributes.remarks") - collectionMethod = create_relationship("collectionMethod") - protocol = create_relationship("protocol") - collectors = create_relationship("collectors") - attachment = create_relationship("attachment") + collectionMethod = create_relationship("collecting-event","collectionMethod") + protocol = create_relationship("collecting-event","protocol") + collectors = create_relationship("collecting-event","collectors") + attachment = create_relationship("collecting-event","attachment") meta = fields.DocumentMeta() diff --git a/dinapy/schemas/materialsampleschema.py b/dinapy/schemas/materialsampleschema.py index 1ae7a85..15f2d72 100644 --- a/dinapy/schemas/materialsampleschema.py +++ b/dinapy/schemas/materialsampleschema.py @@ -109,18 +109,18 @@ def object_deserialization(self, data, **kwargs): del data['meta'] return MaterialSampleDTO(**data) - collection = create_relationship("collection") - collectingEvent = create_relationship("collectingEvent") - preparationType = create_relationship("preparationType") - preparationMethod = create_relationship("preparationMethod") - parentMaterialSample = create_relationship("parentMaterialSample") - preparedBy = create_relationship("preparedBy") - attachment = create_relationship("attachment") - preparationProtocol = create_relationship("preparationProtocol") - projects = create_relationship("projects") - assemblages = create_relationship("assemblages") - organism = create_relationship("organism") - storageUnit = create_relationship("storageUnit") + collection = create_relationship("material-sample","collection") + collectingEvent = create_relationship("material-sample","collectingEvent") + preparationType = create_relationship("material-sample","preparationType") + preparationMethod = create_relationship("material-sample","preparationMethod") + parentMaterialSample = create_relationship("material-sample","parentMaterialSample") + preparedBy = create_relationship("material-sample","preparedBy") + attachment = create_relationship("material-sample","attachment") + preparationProtocol = create_relationship("material-sample","preparationProtocol") + projects = create_relationship("material-sample","projects") + assemblages = create_relationship("material-sample","assemblages") + organism = create_relationship("material-sample","organism") + storageUnit = create_relationship("material-sample","storageUnit") meta = fields.DocumentMeta() diff --git a/dinapy/schemas/metadata_schema.py b/dinapy/schemas/metadata_schema.py index 6bfc0ae..09d394a 100644 --- a/dinapy/schemas/metadata_schema.py +++ b/dinapy/schemas/metadata_schema.py @@ -108,11 +108,11 @@ class MetadataSchema(Schema): ) # Relationships - acMetadataCreator = create_relationship("person", "acMetadataCreator") + acMetadataCreator = create_relationship("metadata","person", "acMetadataCreator") - derivatives = create_relationship("derivatives") + derivatives = create_relationship("metadata","derivatives") - dcCreator = create_relationship("person", "dcCreator") + dcCreator = create_relationship("metadata","person", "dcCreator") @post_load def set_none_to_undefined(self, data, **kwargs): diff --git a/dinapy/schemas/storageunitusageschema.py b/dinapy/schemas/storageunitusageschema.py index 26bc609..c527ea6 100644 --- a/dinapy/schemas/storageunitusageschema.py +++ b/dinapy/schemas/storageunitusageschema.py @@ -25,8 +25,8 @@ class StorageUnitUsage(Schema): createdOn = SkipUndefinedField(fields.DateTime, load_only=True,attribute="attributes.createdOn") createdBy = SkipUndefinedField(fields.Str, load_only=True,attribute="attributes.createdBy") - storageUnit = create_relationship("storageUnit") - storageUnitType = create_relationship("storageUnitType") + storageUnit = create_relationship("storage-unit-usage","storageUnit") + storageUnitType = create_relationship("storage-unit-usage","storageUnitType") @post_load def set_none_to_undefined(self, data, **kwargs): diff --git a/tests/collecting_event_schema_test.py b/tests/collecting_event_schema_test.py index a2aea77..1ed0a05 100644 --- a/tests/collecting_event_schema_test.py +++ b/tests/collecting_event_schema_test.py @@ -140,7 +140,11 @@ "links": { "self": "/api/v1/collecting-event/f08516e5-add2-4baa-89bc-5b8abd0ec8ba/relationships/collectionMethod", "related": "/api/v1/collecting-event/f08516e5-add2-4baa-89bc-5b8abd0ec8ba/collectionMethod" - } + }, + "data": { + "id": "01902d3a-0ec1-7a57-8284-f4ba3aff1664", + "type": "collection-method", + }, }, "protocol": { "links": {