From 84800be9dd3a2214f46813b04fc2367eb6c3d743 Mon Sep 17 00:00:00 2001 From: Laura Porter Date: Tue, 28 Jun 2022 13:12:59 +0100 Subject: [PATCH 1/2] Copy a judgment's properties alongside the judgment content When we copy a judgment from one URI to anotherr, we should also copy its properties (which live outside the content). It woul have been neater to copy the properties XML in the API Client, but we have a time pressure here and it is eaasier to use the pre-existing `set_property` API client call to do this. --- judgments/utils.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/judgments/utils.py b/judgments/utils.py index 154a89e3d..aa5efe0b2 100644 --- a/judgments/utils.py +++ b/judgments/utils.py @@ -38,6 +38,7 @@ def update_judgment_uri(old_uri, new_citation): try: api_client.copy_judgment(old_uri, new_uri) + set_metadata(old_uri, new_uri) except MarklogicAPIError as e: raise MoveJudgmentError( f"Failure when attempting to copy judgment from {old_uri} to {new_uri}: {e}" @@ -51,3 +52,22 @@ def update_judgment_uri(old_uri, new_citation): ) return new_uri + + +def set_metadata(old_uri, new_uri): + source_organisation = api_client.get_property(old_uri, "source-organisation") + source_name = api_client.get_property(old_uri, "source-name") + source_email = api_client.get_property(old_uri, "source-email") + transfer_consignment_reference = api_client.get_property( + old_uri, "transfer-consignment-reference" + ) + transfer_received_at = api_client.get_property(old_uri, "transfer-received-at") + for (key, value) in [ + ("source-organisation", source_organisation), + ("source-name", source_name), + ("source-email", source_email), + ("transfer-consignment_reference", transfer_consignment_reference), + ("transfer-received-at", transfer_received_at), + ]: + if value is not None: + api_client.set_property(new_uri, key, value) From 1359db74d71778ce55a740a0a58a843f9521cb28 Mon Sep 17 00:00:00 2001 From: Laura Porter Date: Tue, 28 Jun 2022 14:08:31 +0100 Subject: [PATCH 2/2] Return an error message when attempting to copy a judgment to an existing URI If a newutral citation generates a URI which already exists in Marklogic, return an error message and do not let the judgment be copied to that URI. --- judgments/tests.py | 12 ++++++++++++ judgments/utils.py | 7 +++++++ 2 files changed, 19 insertions(+) diff --git a/judgments/tests.py b/judgments/tests.py index 26578db58..fbef1cf4a 100644 --- a/judgments/tests.py +++ b/judgments/tests.py @@ -170,6 +170,7 @@ def test_get_judgment_root_malformed_xml(self): def test_update_judgment_uri_success(self, fake_client): ds_caselaw_utils.neutral_url = MagicMock(return_value="new/uri") attrs = { + "get_judgment_xml.return_value": "", "copy_judgment.return_value": True, "delete_judgment.return_value": True, } @@ -210,3 +211,14 @@ def test_update_judgment_uri_unparseable_citation(self): with self.assertRaises(judgments.utils.NeutralCitationToUriError): update_judgment_uri("old/uri", "Wrong neutral citation") + + @patch("judgments.utils.api_client") + def test_update_judgment_uri_duplicate_uri(self, fake_client): + ds_caselaw_utils.neutral_url = MagicMock(return_value="new/uri") + attrs = { + "get_judgment_xml.return_value": "", + } + fake_client.configure_mock(**attrs) + + with self.assertRaises(judgments.utils.MoveJudgmentError): + update_judgment_uri("old/uri", "[2002] EAT 1") diff --git a/judgments/utils.py b/judgments/utils.py index aa5efe0b2..260f54f5c 100644 --- a/judgments/utils.py +++ b/judgments/utils.py @@ -36,6 +36,13 @@ def update_judgment_uri(old_uri, new_citation): f"Unable to form new URI for {old_uri} from neutral citation: {new_citation}" ) + existing_judgment = api_client.get_judgment_xml(new_uri, show_unpublished=True) + if existing_judgment != "": + raise MoveJudgmentError( + f"The URI {new_uri} generated from {new_citation} already exists, you cannot move this judgment to a" + f" pre-existing Neutral Citation Number." + ) + try: api_client.copy_judgment(old_uri, new_uri) set_metadata(old_uri, new_uri)