Skip to content

Commit

Permalink
Use the page id when fetching the absolute url for weasyprint (#25)
Browse files Browse the repository at this point in the history
* Use id when fetching absolute url for weasyprint
  • Loading branch information
adinhodovic authored Apr 12, 2020
1 parent 5641846 commit b068443
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 59 deletions.
75 changes: 32 additions & 43 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ wagtail = ">=2.7"
wagtail-markdown = ">=0.5"
wagtail-metadata = ">=3.0.0"
weasyprint = "^51"
pytest-mock = "^3.0.0"

[tool.poetry.dev-dependencies]
astroid = "2.3.3"
Expand Down
32 changes: 22 additions & 10 deletions tests/test_weasyprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,40 @@

import pytest
from django.urls import reverse
from wagtail.core.models import Site

from .models import CustomResumePage

pytestmark = pytest.mark.django_db


def test_weasyprint(client):
# Test random page pdf generation
url = "%s?resume_url=https://google.com/&name=john-doe" % reverse(
"generate_resume_pdf"
def test_weasyprint(client, mocker):
mocker.patch("wagtail_resume.views.HTML")

site = Site.objects.first()
resume = CustomResumePage(
title="Resume", full_name="Adin Hodovic", role="Software engineer",
)
site.root_page.add_child(instance=resume)
# Test random page pdf generation
url = f"{reverse('generate_resume_pdf')}?page_id={resume.id}"
res = client.get(url)
assert "john-doe-resume" in res["content-disposition"]
assert "adin-hodovic" in res["content-disposition"]
assert res.status_code == 200
assert res["content-type"] == "application/pdf"


def test_weasyprint_with_font(client):
# Test random page pdf generation
url = "%s?resume_url=https://google.com/&name=john-doe&font=Lato" % reverse(
"generate_resume_pdf"
def test_weasyprint_with_font(client, mocker):
mocker.patch("wagtail_resume.views.HTML")
site = Site.objects.first()
resume = CustomResumePage(
title="Resume", full_name="Adin Hodovic", role="Software engineer", font="lato"
)
site.root_page.add_child(instance=resume)
# Test random page pdf generation
url = f"{reverse('generate_resume_pdf')}?page_id={resume.id}"
res = client.get(url)
assert "john-doe-resume" in res["content-disposition"]
assert "adin-hodovic" in res["content-disposition"]
assert res.status_code == 200
assert res["content-type"] == "application/pdf"

Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ deps =
pytest-clarity
pytest-cov
pytest-django
pytest-mock
wagtail
wagtail-markdown
wagtail-metadata
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ <h2 class="section-title"><i class="fas fa-info-circle mr-3"></i><span>About</sp
{% for block in page.resume %}
{{ block }}
{% endfor %}
<a class="pdf" href="{% url 'generate_resume_pdf' %}?resume_url={{ page.full_url }}&font={{ page.font|title|space_to_plus }}&name={{ page.full_name|slugify }}">Get PDF</a>
<a class="pdf" href="{% url 'generate_resume_pdf' %}?page_id={{ page.id }}">Get PDF</a>
</div>
</div>
</body>
18 changes: 13 additions & 5 deletions wagtail_resume/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import logging

from django.http import HttpResponse
from django.utils.text import slugify
from wagtail.core.models import Page
from weasyprint import HTML

from wagtail_resume.templatetags.wagtail_resume_extras import space_to_plus
Expand All @@ -13,23 +15,29 @@

def resume_pdf(request):
response = HttpResponse(content_type="application/pdf")
name = request.GET.get("name")

page_id = request.GET.get("page_id")
resume = Page.objects.filter(id=page_id).first().specific

resume_url = resume.full_url
name = slugify(resume.full_name)
font = resume.font

response[
"Content-Disposition"
] = "inline; filename={name}-resume-{date}.pdf".format(
name=name, date=datetime.datetime.now().strftime("%Y-%m-%d"),
)

font = request.GET.get("font")
if font:
font = space_to_plus(font)
HTML(url=request.GET.get("resume_url")).write_pdf(
font = space_to_plus(font).capitalize()
HTML(url=resume_url).write_pdf(
response,
stylesheets=[
# pylint: disable=line-too-long
f"https://fonts.googleapis.com/css?family={font}&display=swap"
],
)
else:
HTML(url=request.GET.get("resume_url")).write_pdf(response)
HTML(url=resume_url).write_pdf(response)
return response

0 comments on commit b068443

Please sign in to comment.