Skip to content

Commit

Permalink
Merge pull request #129 from nationalarchives/fix-auto-publish
Browse files Browse the repository at this point in the history
Fix auto-publishing
  • Loading branch information
dragon-dxw authored Nov 22, 2023
2 parents 66e2ecb + 6216d54 commit 67dbc17
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 22 deletions.
50 changes: 37 additions & 13 deletions ds-caselaw-ingester/lambda_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@
)


class Metadata(object):
def __init__(self, metadata):
self.metadata = metadata
self.parameters = metadata.get("parameters", {})

@property
def is_tdr(self):
return "TDR" in self.parameters.keys()

@property
def force_publish(self):
return self.parameters.get("INGESTER_OPTIONS", {}).get("auto_publish", False)


class Message(object):
@classmethod
def from_event(cls, event):
Expand Down Expand Up @@ -430,11 +444,15 @@ def _build_version_annotation_payload_from_metadata(metadata: dict):


def update_document_xml(uri, xml, metadata: dict) -> bool:
if Metadata(metadata).is_tdr:
message = "Updated document submitted by TDR user"
else:
message = "Updated document uploaded by Find Case Law"
try:
annotation = VersionAnnotation(
VersionType.SUBMISSION,
automated=False,
message="Updated document submitted by user.",
automated=Metadata(metadata).force_publish,
message=message,
payload=_build_version_annotation_payload_from_metadata(metadata),
)

Expand All @@ -446,10 +464,14 @@ def update_document_xml(uri, xml, metadata: dict) -> bool:


def insert_document_xml(uri, xml, metadata) -> bool:
if Metadata(metadata).is_tdr:
message = "New document submitted by TDR user"
else:
message = "New document uploaded by Find Case Law"
annotation = VersionAnnotation(
VersionType.SUBMISSION,
automated=False,
message="New document submitted by user.",
automated=Metadata(metadata).force_publish,
message=message,
payload=_build_version_annotation_payload_from_metadata(metadata),
)
api_client.insert_document_xml(uri, xml, annotation)
Expand Down Expand Up @@ -525,14 +547,18 @@ def handler(event, context):
updated = update_document_xml(uri, xml, metadata)
inserted = False if updated else insert_document_xml(uri, xml, metadata)

force_publish = Metadata(metadata).force_publish

if updated:
# Notify editors that a document has been updated
send_updated_judgment_notification(uri, metadata)
unpublish_updated_judgment(uri)
if not force_publish:
send_updated_judgment_notification(uri, metadata)
unpublish_updated_judgment(uri)
print(f"Updated judgment xml for {uri}")
elif inserted:
# Notify editors that a new document is ready
send_new_judgment_notification(uri, metadata)
if not force_publish:
send_new_judgment_notification(uri, metadata)
print(f"Inserted judgment xml for {uri}")
else:
raise DocumentInsertionError(
Expand Down Expand Up @@ -578,13 +604,11 @@ def handler(event, context):
s3_client,
)

force_publish = (
metadata.get("parameters", {})
.get("INGESTER_OPTIONS", {})
.get("auto_publish", False)
)
force_publish = Metadata(metadata).force_publish
if force_publish is True:
print(f"auto_publishing {consignment_reference}")
print(f"auto_publishing {consignment_reference} at {uri}")
api_client.set_published(uri)

if api_client.get_published(uri) or force_publish:
update_published_documents(uri, s3_client)

Expand Down
52 changes: 43 additions & 9 deletions ds-caselaw-ingester/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,20 @@ def test_handler_messages_v1(
@patch("lambda_function.tarfile")
@patch("lambda_function.boto3.session.Session")
@patch("lambda_function.urllib3.PoolManager")
@patch("lambda_function.send_updated_judgment_notification")
@patch("lambda_function.send_new_judgment_notification")
@patch("lambda_function.VersionAnnotation")
def test_handler_messages_v2(
self, urllib_pool, boto_session, tarfile, metadata, apiclient, capsys
self,
annotation,
notify_new,
notify_update,
urllib_pool,
boto_session,
tarfile,
metadata,
apiclient,
capsys,
):
"""Mostly intended as a very sketchy test of the primary function"""
urllib_pool.return_value.request.return_value.status = 200
Expand Down Expand Up @@ -151,14 +163,34 @@ def test_handler_messages_v2(
assert "Upload Successful" in log
assert "Ingestion complete" in log
assert "auto_publish" not in log
notify_update.assert_called()
notify_new.assert_not_called()
annotation.assert_called_with(
ANY,
automated=False,
message="Updated document submitted by TDR user",
payload=ANY,
)

@patch("lambda_function.api_client", autospec=True)
@patch("lambda_function.extract_metadata", autospec=True)
@patch("lambda_function.tarfile")
@patch("lambda_function.boto3.session.Session")
@patch("lambda_function.urllib3.PoolManager")
@patch("lambda_function.send_new_judgment_notification")
@patch("lambda_function.send_updated_judgment_notification")
@patch("lambda_function.VersionAnnotation")
def test_handler_messages_s3(
self, urllib_pool, boto_session, tarfile, metadata, apiclient, capsys
self,
annotation,
notify_new,
notify_updated,
urllib_pool,
boto_session,
tarfile,
metadata,
apiclient,
capsys,
):
"""Test that, with appropriate stubs, an S3 message passes through the parsing process"""
urllib_pool.return_value.request.return_value.status = 200
Expand All @@ -179,13 +211,6 @@ def test_handler_messages_s3(
"images": [],
},
},
"TDR": {
"Source-Organization": "",
"Contact-Name": "",
"Contact-Email": "",
"Internal-Sender-Identifier": "",
"Consignment-Completed-Datetime": "",
},
"INGESTER_OPTIONS": {"auto_publish": True},
"PARSER": {"uri": ""},
}
Expand All @@ -204,6 +229,15 @@ def test_handler_messages_s3(
assert "Upload Successful" in log
assert "Ingestion complete" in log
assert "auto_publish" in log
apiclient.set_published.assert_called_with("failures/TDR-2020-FAR")
notify_new.assert_not_called()
notify_updated.assert_not_called()
annotation.assert_called_with(
ANY,
automated=True,
message="Updated document uploaded by Find Case Law",
payload=ANY,
)


class TestLambda:
Expand Down

0 comments on commit 67dbc17

Please sign in to comment.