diff --git a/ds_caselaw_editor_ui/templates/includes/judgment_text_toolbar.html b/ds_caselaw_editor_ui/templates/includes/judgment_text_toolbar.html index 4db860225..726e061b9 100644 --- a/ds_caselaw_editor_ui/templates/includes/judgment_text_toolbar.html +++ b/ds_caselaw_editor_ui/templates/includes/judgment_text_toolbar.html @@ -18,8 +18,8 @@ {% if context.docx_url %} {% translate "judgment.download_docx" %} {% endif %} - {% if context.has_pdf %} - {% translate "judgment.download_pdf" %} + {% if context.pdf_url %} + {% translate "judgment.download_pdf" %} {% endif %} {% if context.is_failure %} {% translate "judgment.delete" %} diff --git a/ds_caselaw_editor_ui/templates/judgment/edit.html b/ds_caselaw_editor_ui/templates/judgment/edit.html index 6e85faf39..daf713bdc 100644 --- a/ds_caselaw_editor_ui/templates/judgment/edit.html +++ b/ds_caselaw_editor_ui/templates/judgment/edit.html @@ -2,9 +2,11 @@ {% block content %} {% load i18n %}
diff --git a/judgments/tests.py b/judgments/tests.py index 3ce6a2dc8..f6a5a0d7e 100644 --- a/judgments/tests.py +++ b/judgments/tests.py @@ -1,7 +1,5 @@ -import os import re -from unittest import mock, skip -from unittest.mock import patch +from unittest import skip from django.test import TestCase @@ -9,17 +7,6 @@ from judgments.models import Judgment -def mocked_requests_head(*args): - class MockResponse: - def __init__(self, status_code): - self.status_code = status_code - - if "ewca/civ/2004/632" in args[0]: - return MockResponse(200) - - return MockResponse(404) - - class TestJudgment(TestCase): @skip def test_valid_content(self): @@ -35,24 +22,6 @@ def test_404_response(self): self.assertIn("Judgment was not found", decoded_response) self.assertEqual(response.status_code, 404) - @patch.dict( - os.environ, - {"PUBLIC_ASSET_BUCKET": "public-asset-bucket", "S3_REGION": "eu-west-2"}, - clear=True, - ) - @mock.patch("requests.head", side_effect=mocked_requests_head) - def test_has_pdf_true(self, mock_head): - self.assertEqual(views.has_pdf("ewca/civ/2004/632"), True) - - @patch.dict( - os.environ, - {"PUBLIC_ASSET_BUCKET": "public-asset-bucket", "S3_REGION": "eu-west-2"}, - clear=True, - ) - @mock.patch("requests.head", side_effect=mocked_requests_head) - def test_has_pdf_false(self, mock_head): - self.assertEqual(views.has_pdf("ewca/civ/2004/XXX"), False) - class TestJudgmentModel(TestCase): def test_can_parse_judgment(self): diff --git a/judgments/urls.py b/judgments/urls.py index 9c3a39085..5aad7683f 100644 --- a/judgments/urls.py +++ b/judgments/urls.py @@ -5,7 +5,6 @@ urlpatterns = [ path("edit", views.EditJudgmentView.as_view(), name="edit"), path("detail", views.detail, name="detail"), - path("detail_pdf", views.detail_pdf, name="detail_pdf"), path("results", views.results, name="results"), path("delete", views.delete, name="delete"), path("", views.index, name="home"), diff --git a/judgments/views.py b/judgments/views.py index bcf8a3b22..a1b379add 100644 --- a/judgments/views.py +++ b/judgments/views.py @@ -7,7 +7,6 @@ import botocore.client import caselawclient.xml_tools as xml_tools import environ -import requests from botocore.exceptions import ClientError from caselawclient.Client import ( RESULTS_PER_PAGE, @@ -17,7 +16,6 @@ ) from caselawclient.xml_tools import JudgmentMissingMetadataError from django.http import Http404, HttpResponse -from django.shortcuts import redirect from django.template import loader from django.utils.translation import gettext from django.views.generic import View @@ -54,7 +52,7 @@ def get_metadata(self, uri: str, judgment: ET.Element) -> dict: ) meta["judgment_date"] = xml_tools.get_judgment_date_value(judgment) meta["docx_url"] = generate_docx_url(uri) - meta["has_pdf"] = has_pdf(uri) + meta["pdf_url"] = generate_pdf_url(uri) meta["previous_versions"] = self.get_versions(uri) except JudgmentMissingMetadataError: meta[ @@ -147,7 +145,7 @@ def detail(request): context["judgment"] = judgment context["page_title"] = metadata_name context["docx_url"] = generate_docx_url(judgment_uri) - context["has_pdf"] = has_pdf(judgment_uri) + context["pdf_url"] = generate_pdf_url(judgment_uri) if version_uri: context["version"] = re.search(r"([\d])-([\d]+)", version_uri).group(1) @@ -157,21 +155,6 @@ def detail(request): return HttpResponse(template.render({"context": context}, request)) -def detail_pdf(request): - params = request.GET - judgment_uri = params.get("judgment_uri") - pdf_path = f'{judgment_uri}/{judgment_uri.replace("/", "_")}.pdf' - pdf_uri = f'https://{env("PUBLIC_ASSET_BUCKET")}.s3.{env("S3_REGION")}.amazonaws.com/{pdf_path}' - return redirect(pdf_uri) - - -def has_pdf(judgment_uri): - pdf_path = f'{judgment_uri}/{judgment_uri.replace("/", "_")}.pdf' - pdf_uri = f'https://{env("PUBLIC_ASSET_BUCKET")}.s3.{env("S3_REGION")}.amazonaws.com/{pdf_path}' - response = requests.head(pdf_uri) - return response.status_code == 200 - - def delete(request): judgment_uri = request.GET.get("judgment_uri", None) context = { @@ -403,3 +386,17 @@ def generate_docx_url(uri: str): return client.generate_presigned_url( "get_object", Params={"Bucket": bucket, "Key": key} ) + + +def generate_pdf_url(uri: str): + bucket = env("PRIVATE_ASSET_BUCKET", None) + if not bucket: + return "" + + client = create_s3_client() + + key = f'{uri}/{uri.replace("/", "_")}.pdf' + + return client.generate_presigned_url( + "get_object", Params={"Bucket": bucket, "Key": key} + )