Compare commits

...

1 Commits

Author SHA1 Message Date
Pea Tyczynska
d07ad70863 WIP enforce 10 page limit for letters 2019-10-09 17:42:45 +01:00
2 changed files with 17 additions and 7 deletions

View File

@@ -15,6 +15,7 @@ from requests import (
from celery.exceptions import MaxRetriesExceededError from celery.exceptions import MaxRetriesExceededError
from notifications_utils.statsd_decorators import statsd from notifications_utils.statsd_decorators import statsd
from notifications_utils.s3 import s3upload from notifications_utils.s3 import s3upload
from notifications_utils import is_letter_too_long
from app import notify_celery from app import notify_celery
from app.aws import s3 from app.aws import s3
@@ -49,6 +50,7 @@ from app.models import (
NOTIFICATION_VIRUS_SCAN_FAILED, NOTIFICATION_VIRUS_SCAN_FAILED,
) )
from app.cronitor import cronitor from app.cronitor import cronitor
from app.utils import get_billable_units
from json import JSONDecodeError from json import JSONDecodeError
@@ -57,15 +59,20 @@ from json import JSONDecodeError
def create_letters_pdf(self, notification_id): def create_letters_pdf(self, notification_id):
try: try:
notification = get_notification_by_id(notification_id, _raise=True) notification = get_notification_by_id(notification_id, _raise=True)
pdf_data, billable_units = get_letters_pdf( pdf_data, page_count = get_letters_pdf(
notification.template, notification.template,
contact_block=notification.reply_to_text, contact_block=notification.reply_to_text,
filename=notification.service.letter_branding and notification.service.letter_branding.filename, filename=notification.service.letter_branding and notification.service.letter_branding.filename,
values=notification.personalisation values=notification.personalisation
) )
billable_units = get_billable_units(page_count)
upload_letter_pdf(notification, pdf_data) upload_letter_pdf(notification, pdf_data)
if is_letter_too_long(page_count):
notification.status = NOTIFICATION_VALIDATION_FAILED
notification.billable_units = 0
if notification.key_type != KEY_TYPE_TEST: if notification.key_type != KEY_TYPE_TEST:
notification.billable_units = billable_units notification.billable_units = billable_units
dao_update_notification(notification) dao_update_notification(notification)
@@ -108,10 +115,9 @@ def get_letters_pdf(template, contact_block, filename, values):
) )
resp.raise_for_status() resp.raise_for_status()
pages_per_sheet = 2 page_count = int(resp.headers.get("X-pdf-page-count", 0))
billable_units = math.ceil(int(resp.headers.get("X-pdf-page-count", 0)) / pages_per_sheet)
return resp.content, billable_units return resp.content, page_count
@notify_celery.task(name='collate-letter-pdfs-for-day') @notify_celery.task(name='collate-letter-pdfs-for-day')

View File

@@ -212,7 +212,11 @@ def letter_print_day(created_at):
def get_page_count(pdf): def get_page_count(pdf):
pages = pdf_page_count(io.BytesIO(pdf)) page_count = pdf_page_count(io.BytesIO(pdf))
pages_per_sheet = 2 billable_units = get_billable_units(page_count)
billable_units = math.ceil(pages / pages_per_sheet)
return billable_units return billable_units
def get_billable_units(page_count):
pages_per_sheet = 2
return math.ceil(page_count / pages_per_sheet)