Files
notifications-admin/app/template_previews.py
Chris Hill-Scott f3a0c505bd Enforce order and style of imports
Done using isort[1], with the following command:
```
isort -rc ./app ./tests
```

Adds linting to the `run_tests.sh` script to stop badly-sorted imports
getting re-introduced.

Chosen style is ‘Vertical Hanging Indent’ with trailing commas, because
I think it gives the cleanest diffs, eg:
```
from third_party import (
    lib1,
    lib2,
    lib3,
    lib4,
)
```

1. https://pypi.python.org/pypi/isort
2018-02-27 16:35:13 +00:00

46 lines
1.4 KiB
Python

import requests
from flask import current_app, json
from app import current_service
class TemplatePreview:
@classmethod
def from_database_object(cls, template, filetype, values=None, page=None):
data = {
'letter_contact_block': template.get('reply_to_text', ''),
'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, 'json', values)
page_count = json.loads(page_count.decode('utf-8'))['count']
return page_count