Refactor PNG generating endpoint for code reuse

The way we generate PNGs is the same for all the different endpoints,
so the code to do this should be shared.
This commit is contained in:
Chris Hill-Scott
2016-12-28 11:06:11 +00:00
parent 559433c5d2
commit 2854dbe3dd
3 changed files with 35 additions and 30 deletions

View File

@@ -5,7 +5,6 @@ from io import BytesIO
from contextlib import suppress
from zipfile import BadZipFile
from xlrd.biffh import XLRDError
from wand.image import Image
from flask import (
request,
@@ -32,7 +31,14 @@ from app.main.uploader import (
s3download
)
from app import job_api_client, service_api_client, current_service, user_api_client
from app.utils import user_has_permissions, get_errors_for_csv, Spreadsheet, get_help_argument, get_template
from app.utils import (
user_has_permissions,
get_errors_for_csv,
Spreadsheet,
get_help_argument,
get_template,
png_from_pdf,
)
def get_page_headings(template_type):
@@ -337,14 +343,9 @@ def check_messages_as_pdf(service_id, template_type, upload_id):
@login_required
@user_has_permissions('send_texts', 'send_emails', 'send_letters')
def check_messages_as_png(service_id, template_type, upload_id):
output = BytesIO()
with Image(
blob=check_messages_as_pdf(service_id, template_type, upload_id).get_data()
) as image:
with image.convert('png') as converted:
converted.save(file=output)
output.seek(0)
return send_file(output, mimetype='image/png')
return send_file(**png_from_pdf(
check_messages_as_pdf(service_id, template_type, upload_id)
))
@main.route("/services/<service_id>/<template_type>/check/<upload_id>", methods=['POST'])

View File

@@ -1,19 +1,17 @@
from datetime import datetime, timedelta
from io import BytesIO
from string import ascii_uppercase
from flask import request, render_template, redirect, url_for, flash, abort, send_file
from flask_login import login_required
from flask_weasyprint import HTML, render_pdf
from dateutil.parser import parse
from wand.image import Image
from notifications_utils.template import LetterPreviewTemplate
from notifications_utils.recipients import first_column_headings
from notifications_python_client.errors import HTTPError
from app.main import main
from app.utils import user_has_permissions, get_template
from app.utils import user_has_permissions, get_template, png_from_pdf
from app.main.forms import SMSTemplateForm, EmailTemplateForm, LetterTemplateForm
from app.main.views.send import get_example_csv_rows
from app import service_api_client, current_service, template_statistics_client
@@ -68,14 +66,9 @@ def view_letter_template_as_pdf(service_id, template_id):
@login_required
@user_has_permissions('view_activity', admin_override=True)
def view_letter_template_as_png(service_id, template_id):
output = BytesIO()
with Image(
blob=view_letter_template_as_pdf(service_id, template_id).get_data()
) as image:
with image.convert('png') as converted:
converted.save(file=output)
output.seek(0)
return send_file(output, mimetype='image/png')
return send_file(**png_from_pdf(
view_letter_template_as_pdf(service_id, template_id)
))
def _view_template_version(service_id, template_id, version, letters_as_pdf=False):
@@ -141,14 +134,9 @@ def view_template_version_as_pdf(service_id, template_id, version):
any_=True
)
def view_template_version_as_png(service_id, template_id, version):
output = BytesIO()
with Image(
blob=view_template_version_as_pdf(service_id, template_id, version).get_data()
) as image:
with image.convert('png') as converted:
converted.save(file=output)
output.seek(0)
return send_file(output, mimetype='image/png')
return send_file(**png_from_pdf(
view_template_version_as_pdf(service_id, template_id, version)
))
@main.route("/services/<service_id>/templates/add-<template_type>", methods=['GET', 'POST'])