Merge pull request #2943 from alphagov/international-postage

Set international postage and international flag for international letters
This commit is contained in:
Rebecca Law
2020-08-10 08:22:52 +01:00
committed by GitHub
14 changed files with 236 additions and 63 deletions

View File

@@ -14,11 +14,11 @@ def create_letter_notification(
reply_to_text=None,
billable_units=None,
updated_at=None,
postage=None
):
notification = persist_notification(
template_id=template.id,
template_version=template.version,
template_postage=template.postage,
# we only accept addresses_with_underscores from the API (from CSV we also accept dashes, spaces etc)
recipient=PostalAddress.from_personalisation(letter_data['personalisation']).normalised,
service=service,
@@ -33,7 +33,9 @@ def create_letter_notification(
status=status,
reply_to_text=reply_to_text,
billable_units=billable_units,
postage=letter_data.get('postage'),
# letter_data.get('postage') is only set for precompiled letters (if international it is set after sanitise)
# letters from a template will pass in 'europe' or 'rest-of-world' if None then use postage from template
postage=postage or letter_data.get('postage') or template.postage,
updated_at=updated_at
)
return notification

View File

@@ -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,
@@ -107,7 +107,6 @@ def persist_notification(
reply_to_text=None,
billable_units=None,
postage=None,
template_postage=None,
document_download_count=None,
updated_at=None
):
@@ -147,7 +146,8 @@ def persist_notification(
elif notification_type == EMAIL_TYPE:
notification.normalised_to = format_email_address(notification.to)
elif notification_type == LETTER_TYPE:
notification.postage = postage or template_postage
notification.postage = postage
notification.international = postage in INTERNATIONAL_POSTAGE_TYPES
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

View File

@@ -120,7 +120,7 @@ def send_notification(notification_type):
simulated = simulated_recipient(notification_form['to'], notification_type)
notification_model = persist_notification(template_id=template.id,
template_version=template.version,
template_postage=template.postage,
postage=template.postage,
recipient=request.get_json()['to'],
service=authenticated_service,
personalisation=notification_form.get('personalisation', None),

View File

@@ -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