Files
notifications-admin/app/template_previews.py
T

46 lines
1.4 KiB
Python
Raw Normal View History

2017-05-11 12:01:51 +01:00
from flask import current_app, json
import requests
2017-04-10 17:25:08 +01:00
from app import current_service
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 = {
2017-04-10 18:12:00 +01:00
'letter_contact_block': current_service['letter_contact_block'],
'template': template,
'values': values,
'dvla_org_id': current_service['dvla_organisation'],
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