Skip to content

Commit

Permalink
Edit metadata message
Browse files Browse the repository at this point in the history
  • Loading branch information
dragon-dxw committed Nov 22, 2023
1 parent dc1e008 commit 6216d54
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 21 deletions.
42 changes: 28 additions & 14 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,11 +547,7 @@ 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.get("parameters", {})
.get("INGESTER_OPTIONS", {})
.get("auto_publish", False)
)
force_publish = Metadata(metadata).force_publish

if updated:
# Notify editors that a document has been updated
Expand Down Expand Up @@ -586,11 +604,7 @@ 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} at {uri}")
api_client.set_published(uri)
Expand Down
23 changes: 16 additions & 7 deletions ds-caselaw-ingester/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,10 @@ def test_handler_messages_v1(
@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,
annotation,
notify_new,
notify_update,
urllib_pool,
Expand Down Expand Up @@ -163,6 +165,12 @@ def test_handler_messages_v2(
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)
Expand All @@ -171,8 +179,10 @@ def test_handler_messages_v2(
@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,
annotation,
notify_new,
notify_updated,
urllib_pool,
Expand Down Expand Up @@ -201,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 @@ -229,6 +232,12 @@ def test_handler_messages_s3(
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 6216d54

Please sign in to comment.