Skip to content

Commit

Permalink
fix: application pdf summary improvements (#2537)
Browse files Browse the repository at this point in the history
* fix: name endpoint for application summary pdf

* fix: add forward slash to url to prevent redirection

* fix: add expection if pdf generation does not work
sirtawast authored Nov 29, 2023
1 parent 3e82337 commit 8b86192
Showing 6 changed files with 29 additions and 16 deletions.
7 changes: 4 additions & 3 deletions backend/benefit/applications/api/v1/views.py
Original file line number Diff line number Diff line change
@@ -513,6 +513,7 @@ def get(self, request, *args, **kwargs):
context = get_context_for_summary_context(application)
return Response(context, template_name="application.html")

return HttpResponse(
generate_application_summary_file(application), "application/pdf"
)
pdf = generate_application_summary_file(application) or None
if pdf:
return HttpResponse(pdf, "application/pdf")
raise Exception("PDF error")
15 changes: 12 additions & 3 deletions backend/benefit/applications/services/app_generated_files.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Union

import pdfkit
from django.template import loader

@@ -31,11 +33,18 @@ def total_de_minimis_amount(application):
}


def generate_application_summary_file(application, request=None) -> bytes:
def generate_application_summary_file(application, request=None) -> Union[bytes, None]:
def generate_summary_pdf(context) -> bytes:
template = loader.get_template("application.html")
rendered_template = template.render(context, request)
return pdfkit.from_string(rendered_template, False, None)

context = get_context_for_summary_context(application)
return generate_summary_pdf(context)
try:
context = get_context_for_summary_context(application)
return generate_summary_pdf(context)
except Exception as e:
print(
f"Cannot generate application summary PDF for application {application.id}",
e,
)
return None
9 changes: 3 additions & 6 deletions backend/benefit/applications/tests/test_applications_api.py
Original file line number Diff line number Diff line change
@@ -2225,8 +2225,7 @@ def test_application_pdf_print(api_client, application):

# Can access own applications
response = api_client.get(
f"/v1/print/{application.id}/",
{"as_html": "1"},
reverse("print_summary_pdf", kwargs={"pk": application.id}) + "?as_html=1",
)
assert response.status_code == 200

@@ -2239,15 +2238,13 @@ def test_application_pdf_print_denied(api_client, anonymous_client):

# Cannot access anonymously
response = anonymous_client.get(
f"/v1/print/{application.id}/",
{"as_html": "1"},
reverse("print_summary_pdf", kwargs={"pk": application.id}) + "?as_html=1",
)
assert response.status_code == 403

# Cannot access other applications
response = api_client.get(
f"/v1/print/{application.id}/",
{"as_html": "1"},
reverse("print_summary_pdf", kwargs={"pk": application.id}) + "?as_html=1",
)

assert response.status_code == 403
4 changes: 2 additions & 2 deletions backend/benefit/common/permissions.py
Original file line number Diff line number Diff line change
@@ -27,8 +27,8 @@ def has_permission(self, request, view):
def has_object_permission(self, request, view, obj):
if settings.NEXT_PUBLIC_MOCK_FLAG:
return True

if request.resolver_match.view_name == "applications.api.v1.views.PrintDetail":
print(request.resolver_match.view_name)
if request.resolver_match.view_name == "print_summary_pdf":
user_org_roles = get_organization_roles(request)
trustee_for_business_id = user_org_roles.get("identifier")
return trustee_for_business_id == obj.company.business_id
6 changes: 5 additions & 1 deletion backend/benefit/helsinkibenefit/urls.py
Original file line number Diff line number Diff line change
@@ -88,7 +88,11 @@
path(
"v1/handlerapplications/<str:application_id>/review/", ReviewStateView.as_view()
),
path("v1/print/<str:pk>/", application_views.PrintDetail.as_view()),
path(
"v1/print/<str:pk>/",
application_views.PrintDetail.as_view(),
name="print_summary_pdf",
),
path("oidc/", include("shared.oidc.urls")),
path("oauth2/", include("shared.azure_adfs.urls")),
path("api-auth/", include("rest_framework.urls", namespace="rest_framework")),
Original file line number Diff line number Diff line change
@@ -166,7 +166,9 @@ const ApplicationFormStep5: React.FC<
onClick={() =>
// eslint-disable-next-line security/detect-non-literal-fs-filename
window.open(
`${getBackendUrl(BackendEndpoint.APPLICANT_PRINT)}${data.id}`,
`${getBackendUrl(BackendEndpoint.APPLICANT_PRINT)}${
data.id
}/`,
'_blank'
)
}

0 comments on commit 8b86192

Please sign in to comment.