Skip to content

Commit

Permalink
Refactor multiple uri.lstrip() calls into a named method
Browse files Browse the repository at this point in the history
To make it more obvious that we are preparing a uri for s3, by stripping its
leading slash. S3 does not like leading slashes on its bucket prefixes!
  • Loading branch information
lozette committed Jul 18, 2022
1 parent 660d938 commit 406772a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 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("/")
17 changes: 7 additions & 10 deletions judgments/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
NeutralCitationToUriError,
get_judgment_root,
update_judgment_uri,
uri_for_s3,
)

env = environ.Env()
Expand Down Expand Up @@ -60,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 @@ -112,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 @@ -180,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 @@ -374,7 +375,6 @@ def publish_documents(uri: str) -> None:
public_bucket = env("PUBLIC_ASSET_BUCKET")
private_bucket = env("PRIVATE_ASSET_BUCKET")

uri = uri.lstrip('/')
response = client.list_objects(Bucket=private_bucket, Prefix=uri)

for result in response.get("Contents", []):
Expand All @@ -392,7 +392,6 @@ def publish_documents(uri: str) -> None:


def unpublish_documents(uri: str) -> None:
uri = uri.lstrip('/')
delete_from_bucket(uri, env("PUBLIC_ASSET_BUCKET"))


Expand Down Expand Up @@ -434,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 @@ -449,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 406772a

Please sign in to comment.