Skip to content

Commit

Permalink
Merge pull request #212 from nationalarchives/feature/download-pdf-fr…
Browse files Browse the repository at this point in the history
…om-private-bucket

Download the Judgment PDF from the PRIVATE_ASSET_BUCKET
  • Loading branch information
Floppy authored Jun 9, 2022
2 parents 15f2ddb + 13d3fb1 commit 4f03116
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
{% if context.docx_url %}
<a href="{{ context.docx_url }}">{% translate "judgment.download_docx" %}</a>
{% endif %}
{% if context.has_pdf %}
<a href="{% url 'detail_pdf' %}?judgment_uri={{context.judgment_uri}}">{% translate "judgment.download_pdf" %}</a>
{% if context.pdf_url %}
<a href="{{ context.pdf_url }}">{% translate "judgment.download_pdf" %}</a>
{% endif %}
{% if context.is_failure %}
<a class="judgment-toolbar__delete" href="{% url 'delete' %}?judgment_uri={{ context.judgment_uri }}">{% translate "judgment.delete" %}</a>
Expand Down
8 changes: 5 additions & 3 deletions ds_caselaw_editor_ui/templates/judgment/edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
{% block content %}
{% load i18n %}
<aside class="judgment-info-component">
<a href="{{ context.docx_url }}">{% translate "judgment.download_docx" %}</a>
{% if context.has_pdf %}
<a href="{% url 'detail_pdf' %}?judgment_uri={{context.judgment_uri}}">{% translate "judgment.download_pdf" %}</a>
{% if context.docx_url %}
<a href="{{ context.docx_url }}">{% translate "judgment.download_docx" %}</a>
{% endif %}
{% if context.pdf_url %}
<a href="{{ context.pdf_url }}">{% translate "judgment.download_pdf" %}</a>
{% endif %}
</aside>
<div class="edit-component">
Expand Down
33 changes: 1 addition & 32 deletions judgments/tests.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,12 @@
import os
import re
from unittest import mock, skip
from unittest.mock import patch
from unittest import skip

from django.test import TestCase

from judgments import converters, views
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):
Expand All @@ -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):
Expand Down
1 change: 0 additions & 1 deletion judgments/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
35 changes: 16 additions & 19 deletions judgments/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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[
Expand Down Expand Up @@ -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)
Expand All @@ -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 = {
Expand Down Expand Up @@ -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}
)

0 comments on commit 4f03116

Please sign in to comment.