From 5581bd3078e88cfa13a83d83c2cecf0da9ceffc7 Mon Sep 17 00:00:00 2001 From: Marcos Prieto Date: Thu, 14 Sep 2023 13:23:34 +0200 Subject: [PATCH] Support annotation metadata in annotation PATCH-ing --- h/schemas/annotation.py | 2 ++ h/services/annotation_write.py | 5 ++++- tests/h/services/annotation_write_test.py | 14 ++++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/h/schemas/annotation.py b/h/schemas/annotation.py index 11415018dae..a062b509c6e 100644 --- a/h/schemas/annotation.py +++ b/h/schemas/annotation.py @@ -230,6 +230,8 @@ def validate(self, data): new_appstruct.get("target_uri", self.existing_target_uri), ) + new_appstruct["metadata"] = appstruct.pop("metadata", None) + new_appstruct["extra"] = appstruct return new_appstruct diff --git a/h/services/annotation_write.py b/h/services/annotation_write.py index 444b8f71149..56a5cac470f 100644 --- a/h/services/annotation_write.py +++ b/h/services/annotation_write.py @@ -110,6 +110,7 @@ def update_annotation( """ initial_target_uri = annotation.target_uri + annotation_metadata = data.pop("metadata", None) self._update_annotation_values(annotation, data) if update_timestamp: annotation.updated = datetime.utcnow() @@ -132,9 +133,11 @@ def update_annotation( document.get("document_uri_dicts", {}), updated=annotation.updated, ) - self.upsert_annotation_slim(annotation) + if annotation_metadata: + self._annotation_metadata_service.set(annotation, annotation_metadata) + # The search index service by default does not reindex if the existing ES # entry's timestamp matches the DB timestamp. If we're not changing this # timestamp, we need to force reindexing. diff --git a/tests/h/services/annotation_write_test.py b/tests/h/services/annotation_write_test.py index 868781bfcc4..5e807295637 100644 --- a/tests/h/services/annotation_write_test.py +++ b/tests/h/services/annotation_write_test.py @@ -150,6 +150,20 @@ def test_update_annotation_with_non_defaults(self, svc, annotation, search_index ) assert result.updated == then + def test_update_annotation_with_metadata( + self, svc, annotation, annotation_metadata_service + ): + result = svc.update_annotation( + annotation, + {"metadata": sentinel.metadata}, + update_timestamp=False, + reindex_tag="custom_tag", + ) + + annotation_metadata_service.set.assert_called_once_with( + result, sentinel.metadata + ) + def test__validate_group_with_no_group(self, svc, annotation): annotation.group = None