mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-03 09:51:11 -05:00
Add postage for send-one-off letters.
The postage is set to europe or rest-of-world for international letters, otherwise the template postage is used. Also set international for letters.
This commit is contained in:
@@ -4,6 +4,7 @@ from base64 import urlsafe_b64encode
|
||||
|
||||
from botocore.exceptions import ClientError as BotoClientError
|
||||
from flask import current_app
|
||||
from notifications_utils.postal_address import PostalAddress
|
||||
|
||||
from notifications_utils.statsd_decorators import statsd
|
||||
from notifications_utils.letter_timings import LETTER_PROCESSING_DEADLINE
|
||||
@@ -43,8 +44,8 @@ from app.models import (
|
||||
NOTIFICATION_VALIDATION_FAILED,
|
||||
NOTIFICATION_VIRUS_SCAN_FAILED,
|
||||
POSTAGE_TYPES,
|
||||
RESOLVE_POSTAGE_FOR_FILE_NAME
|
||||
)
|
||||
RESOLVE_POSTAGE_FOR_FILE_NAME,
|
||||
INTERNATIONAL_POSTAGE_TYPES)
|
||||
from app.cronitor import cronitor
|
||||
|
||||
|
||||
@@ -393,8 +394,16 @@ def process_virus_scan_error(filename):
|
||||
|
||||
|
||||
def update_letter_pdf_status(reference, status, billable_units, recipient_address=None):
|
||||
|
||||
postage = None
|
||||
if recipient_address:
|
||||
# fix allow_international_letters
|
||||
postage = PostalAddress(raw_address=recipient_address.replace(',', '\n'),
|
||||
allow_international_letters=True
|
||||
).postage
|
||||
postage = postage if postage in INTERNATIONAL_POSTAGE_TYPES else None
|
||||
update_dict = {'status': status, 'billable_units': billable_units, 'updated_at': datetime.utcnow()}
|
||||
if postage:
|
||||
update_dict.update({'postage': postage, 'international': True})
|
||||
if recipient_address:
|
||||
update_dict['to'] = recipient_address
|
||||
return dao_update_notifications_by_reference(
|
||||
|
||||
@@ -27,7 +27,7 @@ from app.models import (
|
||||
LETTER_TYPE,
|
||||
NOTIFICATION_CREATED,
|
||||
Notification,
|
||||
)
|
||||
INTERNATIONAL_POSTAGE_TYPES)
|
||||
from app.dao.notifications_dao import (
|
||||
dao_create_notification,
|
||||
dao_delete_notifications_by_id,
|
||||
@@ -148,6 +148,7 @@ def persist_notification(
|
||||
notification.normalised_to = format_email_address(notification.to)
|
||||
elif notification_type == LETTER_TYPE:
|
||||
notification.postage = postage or template_postage
|
||||
notification.international = True if postage in INTERNATIONAL_POSTAGE_TYPES else False
|
||||
notification.normalised_to = ''.join(notification.to.split()).lower()
|
||||
|
||||
# if simulated create a Notification model to return but do not persist the Notification to the dB
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
from notifications_utils.postal_address import PostalAddress
|
||||
from sqlalchemy.orm.exc import NoResultFound
|
||||
from flask import current_app
|
||||
from notifications_utils import SMS_CHAR_COUNT_LIMIT
|
||||
@@ -14,9 +15,9 @@ from app.models import (
|
||||
INTERNATIONAL_SMS_TYPE, SMS_TYPE, EMAIL_TYPE, LETTER_TYPE,
|
||||
KEY_TYPE_TEST, KEY_TYPE_TEAM,
|
||||
ServicePermission,
|
||||
)
|
||||
INTERNATIONAL_LETTERS)
|
||||
from app.service.utils import service_allowed_to_send_to
|
||||
from app.v2.errors import TooManyRequestsError, BadRequestError, RateLimitError
|
||||
from app.v2.errors import TooManyRequestsError, BadRequestError, RateLimitError, ValidationError
|
||||
from app import redis_store
|
||||
from app.notifications.process_notifications import create_content_for_notification
|
||||
from app.utils import get_public_notify_type_text
|
||||
@@ -214,3 +215,34 @@ def check_service_letter_contact_id(service_id, letter_contact_id, notification_
|
||||
message = 'letter_contact_id {} does not exist in database for service id {}'\
|
||||
.format(letter_contact_id, service_id)
|
||||
raise BadRequestError(message=message)
|
||||
|
||||
|
||||
def validate_address(service, letter_data):
|
||||
address = PostalAddress.from_personalisation(
|
||||
letter_data,
|
||||
allow_international_letters=(INTERNATIONAL_LETTERS in str(service.permissions)),
|
||||
)
|
||||
if not address.has_enough_lines:
|
||||
raise ValidationError(
|
||||
message=f'Address must be at least {PostalAddress.MIN_LINES} lines'
|
||||
)
|
||||
if address.has_too_many_lines:
|
||||
raise ValidationError(
|
||||
message=f'Address must be no more than {PostalAddress.MAX_LINES} lines'
|
||||
)
|
||||
if not address.has_valid_last_line:
|
||||
if address.allow_international_letters:
|
||||
raise ValidationError(
|
||||
message=f'Last line of address must be a real UK postcode or another country'
|
||||
)
|
||||
raise ValidationError(
|
||||
message='Must be a real UK postcode'
|
||||
)
|
||||
if address.has_invalid_characters:
|
||||
raise ValidationError(
|
||||
message='Address lines must not start with any of the following characters: @ ( ) = [ ] ” \\ / ,'
|
||||
)
|
||||
if address.postage == 'united-kingdom':
|
||||
return None # use postage from template
|
||||
else:
|
||||
return address.postage
|
||||
|
||||
@@ -13,8 +13,8 @@ from app.notifications.validators import (
|
||||
check_service_has_permission,
|
||||
check_service_over_daily_message_limit,
|
||||
validate_and_format_recipient,
|
||||
validate_template
|
||||
)
|
||||
validate_template,
|
||||
validate_address)
|
||||
from app.notifications.process_notifications import (
|
||||
persist_notification,
|
||||
send_notification_to_queue
|
||||
@@ -75,7 +75,11 @@ def send_one_off_notification(service_id, post_data):
|
||||
notification_type=template.template_type,
|
||||
allow_whitelisted_recipients=False,
|
||||
)
|
||||
|
||||
postage = None
|
||||
if template.template_type == LETTER_TYPE:
|
||||
# Validate address and set postage to europe|rest-of-world if international letter,
|
||||
# otherwise persist_notification with use template postage
|
||||
postage = validate_address(service, personalisation)
|
||||
validate_created_by(service, post_data['created_by'])
|
||||
|
||||
sender_id = post_data.get('sender_id', None)
|
||||
@@ -98,6 +102,7 @@ def send_one_off_notification(service_id, post_data):
|
||||
created_by_id=post_data['created_by'],
|
||||
reply_to_text=reply_to,
|
||||
reference=create_one_off_reference(template.template_type),
|
||||
postage=postage
|
||||
)
|
||||
|
||||
queue_name = QueueNames.PRIORITY if template.process_type == PRIORITY else None
|
||||
|
||||
@@ -5,7 +5,6 @@ from datetime import datetime
|
||||
|
||||
from boto.exception import SQSError
|
||||
from flask import request, jsonify, current_app, abort
|
||||
from notifications_utils.postal_address import PostalAddress
|
||||
from notifications_utils.recipients import try_validate_and_format_phone_number
|
||||
from gds_metrics import Histogram
|
||||
|
||||
@@ -36,7 +35,6 @@ from app.models import (
|
||||
NOTIFICATION_SENDING,
|
||||
NOTIFICATION_DELIVERED,
|
||||
NOTIFICATION_PENDING_VIRUS_CHECK,
|
||||
INTERNATIONAL_LETTERS,
|
||||
Notification)
|
||||
from app.notifications.process_letter_notifications import (
|
||||
create_letter_notification
|
||||
@@ -51,15 +49,18 @@ from app.notifications.validators import (
|
||||
check_service_email_reply_to_id,
|
||||
check_service_has_permission,
|
||||
check_service_sms_sender_id,
|
||||
validate_address,
|
||||
validate_and_format_recipient,
|
||||
validate_template,
|
||||
)
|
||||
from app.schema_validation import validate
|
||||
from app.v2.errors import BadRequestError, ValidationError
|
||||
from app.v2.notifications import v2_notification_blueprint
|
||||
from app.v2.errors import BadRequestError
|
||||
from app.v2.notifications.create_response import (
|
||||
create_post_sms_response_from_notification, create_post_email_response_from_notification,
|
||||
create_post_letter_response_from_notification)
|
||||
create_post_email_response_from_notification,
|
||||
create_post_sms_response_from_notification,
|
||||
create_post_letter_response_from_notification
|
||||
)
|
||||
from app.v2.notifications import v2_notification_blueprint
|
||||
from app.v2.notifications.notification_schemas import (
|
||||
post_sms_request,
|
||||
post_email_request,
|
||||
@@ -339,7 +340,7 @@ def process_letter_notification(
|
||||
template=template,
|
||||
reply_to_text=reply_to_text)
|
||||
|
||||
postage = validate_address(service, letter_data)
|
||||
postage = validate_address(service, letter_data['personalisation'])
|
||||
|
||||
test_key = api_key.key_type == KEY_TYPE_TEST
|
||||
|
||||
@@ -389,37 +390,6 @@ def process_letter_notification(
|
||||
return resp
|
||||
|
||||
|
||||
def validate_address(service, letter_data):
|
||||
address = PostalAddress.from_personalisation(
|
||||
letter_data['personalisation'],
|
||||
allow_international_letters=(INTERNATIONAL_LETTERS in service.permissions),
|
||||
)
|
||||
if not address.has_enough_lines:
|
||||
raise ValidationError(
|
||||
message=f'Address must be at least {PostalAddress.MIN_LINES} lines'
|
||||
)
|
||||
if address.has_too_many_lines:
|
||||
raise ValidationError(
|
||||
message=f'Address must be no more than {PostalAddress.MAX_LINES} lines'
|
||||
)
|
||||
if not address.has_valid_last_line:
|
||||
if address.allow_international_letters:
|
||||
raise ValidationError(
|
||||
message=f'Last line of address must be a real UK postcode or another country'
|
||||
)
|
||||
raise ValidationError(
|
||||
message='Must be a real UK postcode'
|
||||
)
|
||||
if address.has_invalid_characters:
|
||||
raise ValidationError(
|
||||
message='Address lines must not start with any of the following characters: @ ( ) = [ ] ” \\ / ,'
|
||||
)
|
||||
if address.postage == 'united-kingdom':
|
||||
return None # use postage from template
|
||||
else:
|
||||
return address.postage
|
||||
|
||||
|
||||
def process_precompiled_letter_notifications(*, letter_data, api_key, service, template, reply_to_text):
|
||||
try:
|
||||
status = NOTIFICATION_PENDING_VIRUS_CHECK
|
||||
|
||||
Reference in New Issue
Block a user