Skip to content

Commit

Permalink
Merge pull request #288 from nationalarchives/fix/fix-publishing-asse…
Browse files Browse the repository at this point in the history
…ts-for-moved-judgments

(Fix) Publish assets for moved "failure" judgments
  • Loading branch information
lozette authored Jul 18, 2022
2 parents 79c0dba + 406772a commit 731c9dc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
8 changes: 6 additions & 2 deletions judgments/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ def set_metadata(old_uri, new_uri):
def copy_assets(old_uri, new_uri):
client = create_s3_client()
bucket = env("PRIVATE_ASSET_BUCKET")
old_uri = old_uri.lstrip("/")
new_uri = new_uri.lstrip("/")
old_uri = uri_for_s3(old_uri)
new_uri = uri_for_s3(new_uri)

response = client.list_objects(Bucket=bucket, Prefix=old_uri)

Expand Down Expand Up @@ -141,3 +141,7 @@ def create_s3_client():
region_name=env("PRIVATE_ASSET_BUCKET_REGION", default=None),
config=botocore.client.Config(signature_version="s3v4"),
)


def uri_for_s3(uri: str):
return uri.lstrip("/")
23 changes: 14 additions & 9 deletions judgments/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import math
import re
import time
Expand Down Expand Up @@ -28,6 +29,7 @@
NeutralCitationToUriError,
get_judgment_root,
update_judgment_uri,
uri_for_s3,
)

env = environ.Env()
Expand Down Expand Up @@ -59,8 +61,8 @@ def get_metadata(self, uri: str, judgment: ET.Element) -> dict:
xml_tools.get_neutral_citation_name_value(judgment) or ""
)
meta["judgment_date"] = xml_tools.get_judgment_date_value(judgment) or ""
meta["docx_url"] = generate_docx_url(uri)
meta["pdf_url"] = generate_pdf_url(uri)
meta["docx_url"] = generate_docx_url(uri_for_s3(uri))
meta["pdf_url"] = generate_pdf_url(uri_for_s3(uri))
meta["previous_versions"] = self.get_versions(uri)
meta["consignment_reference"] = api_client.get_property(
uri, "transfer-consignment-reference"
Expand Down Expand Up @@ -111,9 +113,9 @@ def post(self, request, *args, **kwargs):
api_client.set_anonymised(judgment_uri, anonymised)

if published:
publish_documents(judgment_uri)
publish_documents(uri_for_s3(judgment_uri))
else:
unpublish_documents(judgment_uri)
unpublish_documents(uri_for_s3(judgment_uri))

# Set name
new_name = request.POST["metadata_name"]
Expand Down Expand Up @@ -179,8 +181,8 @@ def detail(request):
judgment = multipart_data.parts[0].text
context["judgment"] = judgment
context["page_title"] = metadata_name
context["docx_url"] = generate_docx_url(judgment_uri)
context["pdf_url"] = generate_pdf_url(judgment_uri)
context["docx_url"] = generate_docx_url(uri_for_s3(judgment_uri))
context["pdf_url"] = generate_pdf_url(uri_for_s3(judgment_uri))

if version_uri:
try:
Expand Down Expand Up @@ -381,7 +383,12 @@ def publish_documents(uri: str) -> None:
if not key.endswith("parser.log") and not key.endswith(".tar.gz"):
source = {"Bucket": private_bucket, "Key": key}
extra_args = {"ACL": "public-read"}
client.copy(source, public_bucket, key, extra_args)
try:
client.copy(source, public_bucket, key, extra_args)
except botocore.client.ClientError as e:
logging.warning(
f"Unable to copy file {key} to new location {public_bucket}, error: {e}"
)


def unpublish_documents(uri: str) -> None:
Expand Down Expand Up @@ -426,7 +433,6 @@ def generate_docx_url(uri: str):
return ""

client = create_s3_client()
uri = uri.lstrip("/")

key = f'{uri}/{uri.replace("/", "_")}.docx'

Expand All @@ -441,7 +447,6 @@ def generate_pdf_url(uri: str):
return ""

client = create_s3_client()
uri = uri.lstrip("/")

key = f'{uri}/{uri.replace("/", "_")}.pdf'

Expand Down

0 comments on commit 731c9dc

Please sign in to comment.