Skip to content

Commit

Permalink
Support new Flask and WeasyPrint versions
Browse files Browse the repository at this point in the history
  • Loading branch information
liZe committed Oct 3, 2023
1 parent 1538204 commit b3428d1
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
5 changes: 3 additions & 2 deletions flask_weasyprint/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,13 @@ def flask_url_fetcher(url):
server_name = EnvironBuilder(
path, base_url=base_url).server_name
for cookie_key, cookie_value in request.cookies.items():
client.set_cookie(server_name, cookie_key, cookie_value)
client.set_cookie(
cookie_key, cookie_value, domain=server_name)
response = client.get(path, base_url=base_url)
if response.status_code == 200:
return {
'string': response.data, 'mime_type': response.mimetype,
'encoding': response.charset, 'redirected_url': url}
'encoding': 'utf-8', 'redirected_url': url}
# The test client can follow redirects, but do it ourselves
# to get access to the redirected URL.
elif response.status_code in (301, 302, 303, 305, 307, 308):
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ requires-python = '>=3.7'
readme = {file = 'README.rst', content-type = 'text/x-rst'}
license = {file = 'LICENSE'}
dependencies = [
'flask >=2.0.0',
'weasyprint >=43.0',
'flask >=2.3.0',
'weasyprint >=53.0',
]
classifiers = [
'Development Status :: 5 - Production/Stable',
Expand Down
6 changes: 5 additions & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Demonstration and testing application for Flask-WeasyPrint."""

from flask import Flask, abort, redirect, render_template, request, url_for
from weasyprint import __version__ as weasyprint_version

# Disable the Flask’s default static file handling. (See below.)
app = Flask(__name__, static_folder=None)
Expand Down Expand Up @@ -48,7 +49,10 @@ def graph():

@app.route('/foo.pdf')
def document_pdf():
return render_pdf(url_for('index'))
if int(weasyprint_version.split('.')[0]) >= 59:
return render_pdf(url_for('index'), uncompressed_pdf=True)
else:
return render_pdf(url_for('index'))

# End of code specific to Flask-WeasyPrint.

Expand Down
14 changes: 9 additions & 5 deletions tests/test_flask_weasyprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest
from flask import Flask, json, jsonify, redirect, request
from flask_weasyprint import CSS, HTML, make_url_fetcher, render_pdf
from weasyprint import __version__ as weasyprint_version
from werkzeug.test import ClientRedirectError

from . import app, document_html
Expand Down Expand Up @@ -57,14 +58,17 @@ def test_pdf(url, filename, automatic, cookie):
if url.endswith('.pdf'):
client = app.test_client()
if cookie:
client.set_cookie('', 'cookie', cookie)
client.set_cookie('cookie', cookie)
response = client.get('/foo.pdf')
else:
with app.test_request_context('/foo/'):
response = render_pdf(
HTML(string=document_html()),
download_filename=filename,
automatic_download=automatic)
options = {
'download_filename': filename,
'automatic_download': automatic,
}
if int(weasyprint_version.split('.')[0]) >= 59:
options['uncompressed_pdf'] = True
response = render_pdf(HTML(string=document_html()), **options)
assert response.status_code == 200
assert response.mimetype == 'application/pdf'
assert response.data.startswith(b'%PDF')
Expand Down

0 comments on commit b3428d1

Please sign in to comment.