Skip to content

Commit

Permalink
Merge pull request #243 from nationalarchives/fix/copy-document-prope…
Browse files Browse the repository at this point in the history
…rties-on-move

Copy document properties on move & guard against duplicate URIs.
  • Loading branch information
Floppy authored Jun 28, 2022
2 parents a9e1c83 + 1359db7 commit 8bb2fe2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
12 changes: 12 additions & 0 deletions judgments/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -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": "<akomaNtoso><judgment></judgment></akomaNtoso>",
}
fake_client.configure_mock(**attrs)

with self.assertRaises(judgments.utils.MoveJudgmentError):
update_judgment_uri("old/uri", "[2002] EAT 1")
27 changes: 27 additions & 0 deletions judgments/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,16 @@ 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)
except MarklogicAPIError as e:
raise MoveJudgmentError(
f"Failure when attempting to copy judgment from {old_uri} to {new_uri}: {e}"
Expand All @@ -51,3 +59,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)

0 comments on commit 8bb2fe2

Please sign in to comment.