From d07ad70863a09eb5d933cc171949e31294fed257 Mon Sep 17 00:00:00 2001 From: Pea Tyczynska Date: Wed, 9 Oct 2019 17:42:45 +0100 Subject: [PATCH] WIP enforce 10 page limit for letters --- app/celery/letters_pdf_tasks.py | 14 ++++++++++---- app/letters/utils.py | 10 +++++++--- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/app/celery/letters_pdf_tasks.py b/app/celery/letters_pdf_tasks.py index 434f2fdb7..6228955fa 100644 --- a/app/celery/letters_pdf_tasks.py +++ b/app/celery/letters_pdf_tasks.py @@ -15,6 +15,7 @@ from requests import ( from celery.exceptions import MaxRetriesExceededError from notifications_utils.statsd_decorators import statsd from notifications_utils.s3 import s3upload +from notifications_utils import is_letter_too_long from app import notify_celery from app.aws import s3 @@ -49,6 +50,7 @@ from app.models import ( NOTIFICATION_VIRUS_SCAN_FAILED, ) from app.cronitor import cronitor +from app.utils import get_billable_units from json import JSONDecodeError @@ -57,15 +59,20 @@ from json import JSONDecodeError def create_letters_pdf(self, notification_id): try: 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, contact_block=notification.reply_to_text, filename=notification.service.letter_branding and notification.service.letter_branding.filename, values=notification.personalisation ) + billable_units = get_billable_units(page_count) 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: notification.billable_units = billable_units dao_update_notification(notification) @@ -108,10 +115,9 @@ def get_letters_pdf(template, contact_block, filename, values): ) resp.raise_for_status() - pages_per_sheet = 2 - billable_units = math.ceil(int(resp.headers.get("X-pdf-page-count", 0)) / pages_per_sheet) + page_count = int(resp.headers.get("X-pdf-page-count", 0)) - return resp.content, billable_units + return resp.content, page_count @notify_celery.task(name='collate-letter-pdfs-for-day') diff --git a/app/letters/utils.py b/app/letters/utils.py index 98e954fb5..ea2fda645 100644 --- a/app/letters/utils.py +++ b/app/letters/utils.py @@ -212,7 +212,11 @@ def letter_print_day(created_at): def get_page_count(pdf): - pages = pdf_page_count(io.BytesIO(pdf)) - pages_per_sheet = 2 - billable_units = math.ceil(pages / pages_per_sheet) + page_count = pdf_page_count(io.BytesIO(pdf)) + billable_units = get_billable_units(page_count) return billable_units + + +def get_billable_units(page_count): + pages_per_sheet = 2 + return math.ceil(page_count / pages_per_sheet)