2017-04-10 15:10:34 +01:00
|
|
|
import requests
|
2018-02-20 11:22:17 +00:00
|
|
|
from flask import current_app, json
|
2017-04-10 15:10:34 +01:00
|
|
|
|
2017-04-10 17:25:08 +01:00
|
|
|
from app import current_service
|
2017-04-10 15:10:34 +01:00
|
|
|
|
2017-04-10 17:25:08 +01:00
|
|
|
|
|
|
|
|
class TemplatePreview:
|
|
|
|
|
@classmethod
|
2017-04-20 10:40:15 +01:00
|
|
|
def from_database_object(cls, template, filetype, values=None, page=None):
|
2017-04-10 17:25:08 +01:00
|
|
|
data = {
|
2018-01-03 10:44:36 +00:00
|
|
|
'letter_contact_block': template.get('reply_to_text', ''),
|
2017-04-10 18:12:00 +01:00
|
|
|
'template': template,
|
2017-04-20 12:15:30 +01:00
|
|
|
'values': values,
|
2019-02-11 18:12:22 +00:00
|
|
|
'filename': current_service.letter_branding and current_service.letter_branding['filename']
|
2017-04-10 17:25:08 +01:00
|
|
|
}
|
|
|
|
|
resp = requests.post(
|
2017-04-20 10:40:15 +01:00
|
|
|
'{}/preview.{}{}'.format(
|
|
|
|
|
current_app.config['TEMPLATE_PREVIEW_API_HOST'],
|
|
|
|
|
filetype,
|
|
|
|
|
'?page={}'.format(page) if page else '',
|
|
|
|
|
),
|
2017-04-10 17:25:08 +01:00
|
|
|
json=data,
|
2017-04-10 18:12:00 +01:00
|
|
|
headers={'Authorization': 'Token {}'.format(current_app.config['TEMPLATE_PREVIEW_API_KEY'])}
|
2017-04-10 17:25:08 +01:00
|
|
|
)
|
|
|
|
|
return (resp.content, resp.status_code, resp.headers.items())
|
|
|
|
|
|
|
|
|
|
@classmethod
|
2017-04-20 10:40:15 +01:00
|
|
|
def from_utils_template(cls, template, filetype, page=None):
|
2017-04-10 17:25:08 +01:00
|
|
|
return cls.from_database_object(
|
|
|
|
|
template._template,
|
|
|
|
|
filetype,
|
2017-04-20 10:40:15 +01:00
|
|
|
template.values,
|
|
|
|
|
page=page,
|
2017-04-10 17:25:08 +01:00
|
|
|
)
|
2017-05-11 12:01:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_page_count_for_letter(template, values=None):
|
|
|
|
|
|
|
|
|
|
if template['template_type'] != 'letter':
|
|
|
|
|
return None
|
|
|
|
|
|
2017-05-16 11:43:19 +01:00
|
|
|
page_count, _, _ = TemplatePreview.from_database_object(template, 'json', values)
|
2017-05-11 12:01:51 +01:00
|
|
|
page_count = json.loads(page_count.decode('utf-8'))['count']
|
|
|
|
|
|
|
|
|
|
return page_count
|
2018-09-26 16:42:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def validate_letter(pdf_file):
|
|
|
|
|
return requests.post(
|
|
|
|
|
'{}/precompiled/validate?include_preview=true'.format(current_app.config['TEMPLATE_PREVIEW_API_HOST']),
|
|
|
|
|
data=pdf_file,
|
|
|
|
|
headers={'Authorization': 'Token {}'.format(current_app.config['TEMPLATE_PREVIEW_API_KEY'])}
|
|
|
|
|
)
|