mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-02-24 04:10:57 -05:00
Right now showing all the pages in full is the only way we have of showing a letter that makes sense to our users. Maybe in the future we show some kind of truncated version, but the end of the first page is not a good place to truncate the letter. This commit just extracts the code for showing multiple pages from the template view, refactors it for reuse, and includes it in the send views.
46 lines
1.4 KiB
Python
46 lines
1.4 KiB
Python
from flask import current_app, json
|
|
import requests
|
|
|
|
from app import current_service
|
|
|
|
|
|
class TemplatePreview:
|
|
@classmethod
|
|
def from_database_object(cls, template, filetype, values=None, page=None):
|
|
data = {
|
|
'letter_contact_block': current_service['letter_contact_block'],
|
|
'template': template,
|
|
'values': values,
|
|
'dvla_org_id': current_service['dvla_organisation'],
|
|
}
|
|
resp = requests.post(
|
|
'{}/preview.{}{}'.format(
|
|
current_app.config['TEMPLATE_PREVIEW_API_HOST'],
|
|
filetype,
|
|
'?page={}'.format(page) if page else '',
|
|
),
|
|
json=data,
|
|
headers={'Authorization': 'Token {}'.format(current_app.config['TEMPLATE_PREVIEW_API_KEY'])}
|
|
)
|
|
return (resp.content, resp.status_code, resp.headers.items())
|
|
|
|
@classmethod
|
|
def from_utils_template(cls, template, filetype, page=None):
|
|
return cls.from_database_object(
|
|
template._template,
|
|
filetype,
|
|
template.values,
|
|
page=page,
|
|
)
|
|
|
|
|
|
def get_page_count_for_letter(template, values=None):
|
|
|
|
if template['template_type'] != 'letter':
|
|
return None
|
|
|
|
page_count, _, _ = TemplatePreview.from_database_object(template, values, filetype='json')
|
|
page_count = json.loads(page_count.decode('utf-8'))['count']
|
|
|
|
return page_count
|