def test_as_personalisation(address, expected_personalisation): assert PostalAddress(address).as_personalisation == expected_personalisation
def test_postage(address, expected_postage): assert PostalAddress(address).postage == expected_postage
def test_from_personalisation(personalisation): assert PostalAddress.from_personalisation(personalisation).normalised == ( '123 Example Street\n' 'City of Town\n' 'SW1A 1AA' )
def send_test_step(service_id, template_id, step_index): if {'recipient', 'placeholders'} - set(session.keys()): return redirect( url_for( { 'main.send_test_step': '.send_test', 'main.send_one_off_step': '.send_one_off', }[request.endpoint], service_id=service_id, template_id=template_id, )) db_template = current_service.get_template_with_user_permission_or_403( template_id, current_user) email_reply_to = None sms_sender = None if db_template['template_type'] == 'email': email_reply_to = get_email_reply_to_address_from_session() elif db_template['template_type'] == 'sms': sms_sender = get_sms_sender_from_session() template_values = get_recipient_and_placeholders_from_session( db_template['template_type']) template = get_template(db_template, current_service, show_recipient=True, letter_preview_url=url_for( 'no_cookie.send_test_preview', service_id=service_id, template_id=template_id, filetype='png', ), page_count=get_page_count_for_letter( db_template, values=template_values), email_reply_to=email_reply_to, sms_sender=sms_sender) placeholders = fields_to_fill_in( template, prefill_current_user=(request.endpoint == 'main.send_test_step'), ) try: current_placeholder = placeholders[step_index] except IndexError: if all_placeholders_in_session(placeholders): return get_notification_check_endpoint(service_id, template) return redirect( url_for( { 'main.send_test_step': '.send_test', 'main.send_one_off_step': '.send_one_off', }[request.endpoint], service_id=service_id, template_id=template_id, )) # if we're in a letter, we should show address block rather than "address line #" or "postcode" if template.template_type == 'letter': if step_index < len(address_lines_1_to_7_keys): return redirect( url_for( '.send_one_off_letter_address', service_id=service_id, template_id=template_id, )) if current_placeholder in Columns( PostalAddress('').as_personalisation): return redirect( url_for( request.endpoint, service_id=service_id, template_id=template_id, step_index=step_index + 1, help=get_help_argument(), )) form = get_placeholder_form_instance( current_placeholder, dict_to_populate_from=get_normalised_placeholders_from_session(), template_type=template.template_type, allow_international_phone_numbers=current_service.has_permission( 'international_sms'), ) if form.validate_on_submit(): # if it's the first input (phone/email), we store against `recipient` as well, for easier extraction. # Only if it's not a letter. # And only if we're not on the test route, since that will already have the user's own number set if (step_index == 0 and template.template_type != 'letter' and request.endpoint != 'main.send_test_step'): session['recipient'] = form.placeholder_value.data session['placeholders'][ current_placeholder] = form.placeholder_value.data if all_placeholders_in_session(placeholders): return get_notification_check_endpoint(service_id, template) return redirect( url_for( request.endpoint, service_id=service_id, template_id=template_id, step_index=step_index + 1, help=get_help_argument(), )) back_link = get_back_link(service_id, template, step_index, placeholders) template.values = template_values template.values[current_placeholder] = None return render_template( 'views/send-test.html', page_title=get_send_test_page_title( template.template_type, get_help_argument(), entering_recipient=not session['recipient'], name=template.name, ), template=template, form=form, skip_link=get_skip_link(step_index, template), back_link=back_link, help=get_help_argument(), link_to_upload=(request.endpoint == 'main.send_one_off_step' and step_index == 0), )
def test_normalised(address, expected_normalised, expected_as_single_line): assert PostalAddress(address).normalised == expected_normalised assert PostalAddress(address).as_single_line == expected_as_single_line
def test_valid_last_line_too_short_too_long(address): postal_address = PostalAddress(address, allow_international_letters=True) assert postal_address.valid is False assert postal_address.has_valid_last_line is True
def test_valid_from_personalisation_with_international_parameter(international, expected_valid): assert PostalAddress.from_personalisation( {'address_line_1': 'A', 'address_line_2': 'B', 'address_line_3': 'Chad'}, allow_international_letters=international, ).valid is expected_valid
def test_raw_address(): raw_address = 'a\n\n\tb\r c ' assert PostalAddress(raw_address).raw_address == raw_address
def test_has_too_many_lines(address, too_many_lines_expected): assert PostalAddress(address).has_too_many_lines is too_many_lines_expected
def process_letter_notification(*, letter_data, api_key, template, reply_to_text, precompiled=False): if api_key.key_type == KEY_TYPE_TEAM: raise BadRequestError( message='Cannot send letters with a team api key', status_code=403) if not api_key.service.research_mode and api_key.service.restricted and api_key.key_type != KEY_TYPE_TEST: raise BadRequestError( message='Cannot send letters when service is in trial mode', status_code=403) if precompiled: return process_precompiled_letter_notifications( letter_data=letter_data, api_key=api_key, template=template, reply_to_text=reply_to_text) address = PostalAddress.from_personalisation( letter_data['personalisation'], allow_international_letters=api_key.service.has_permission( INTERNATIONAL_LETTERS), ) 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') test_key = api_key.key_type == KEY_TYPE_TEST # if we don't want to actually send the letter, then start it off in SENDING so we don't pick it up status = NOTIFICATION_CREATED if not test_key else NOTIFICATION_SENDING queue = QueueNames.CREATE_LETTERS_PDF if not test_key else QueueNames.RESEARCH_MODE notification = create_letter_notification(letter_data=letter_data, template=template, api_key=api_key, status=status, reply_to_text=reply_to_text) create_letters_pdf.apply_async([str(notification.id)], queue=queue) if test_key: if current_app.config['NOTIFY_ENVIRONMENT'] in [ 'preview', 'development' ]: create_fake_letter_response_file.apply_async( (notification.reference, ), queue=queue) else: update_notification_status_by_reference(notification.reference, NOTIFICATION_DELIVERED) return notification
def test_has_enough_lines(address, enough_lines_expected): assert PostalAddress(address).has_enough_lines is enough_lines_expected
def test_normalised(address, expected_normalised): assert PostalAddress(address).normalised == expected_normalised
def as_postal_address(self): from notifications_utils.postal_address import PostalAddress return PostalAddress.from_personalisation( self.recipient_and_personalisation, allow_international_letters=self.allow_international_letters, )
def get_back_link(service_id, template, step_index, placeholders=None): if get_help_argument(): # if we're on the check page, redirect back to the beginning. anywhere else, don't return the back link if request.endpoint == 'main.check_notification': return url_for('main.send_test', service_id=service_id, template_id=template.id, help=get_help_argument()) else: if step_index == 0: return url_for( 'main.start_tour', service_id=service_id, template_id=template.id, ) elif step_index > 0: return url_for( 'main.send_test_step', service_id=service_id, template_id=template.id, step_index=step_index - 1, help=2, ) elif step_index == 0: if should_skip_template_page(template.template_type): return url_for( '.choose_template', service_id=service_id, ) else: return url_for( '.view_template', service_id=service_id, template_id=template.id, ) elif is_current_user_the_recipient() and step_index > 1: return url_for( 'main.send_test_step', service_id=service_id, template_id=template.id, step_index=step_index - 1, ) elif is_current_user_the_recipient() and step_index == 1: return url_for( 'main.send_one_off_step', service_id=service_id, template_id=template.id, step_index=0, ) if template.template_type == 'letter' and placeholders: # Make sure we’re not redirecting users to a page which will # just redirect them forwards again back_link_destination_step_index = next( (index for index, placeholder in reversed( list(enumerate(placeholders[:step_index]))) if placeholder not in Columns(PostalAddress('').as_personalisation)), 1) return get_back_link(service_id, template, back_link_destination_step_index + 1) return url_for( 'main.send_one_off_step', service_id=service_id, template_id=template.id, step_index=step_index - 1, )
def test_bool(address, expected_bool): assert bool(PostalAddress(address)) is expected_bool
def test_postcode(address, expected_postcode): assert PostalAddress(address).has_valid_postcode is bool(expected_postcode) assert PostalAddress(address).postcode == expected_postcode
def test_country(address, expected_country): assert PostalAddress(address).country == expected_country
def test_has_invalid_characters(address, expected_result): assert PostalAddress(address).has_invalid_characters is expected_result
def test_valid_with_invalid_characters(): address = 'Valid\nExcept\n[For one character\nBhutan\n' assert PostalAddress(address, allow_international_letters=True).valid is False
def test_international(address, expected_international): assert PostalAddress(address).international is expected_international
def postal_address(self): return PostalAddress.from_personalisation(Columns(self.values))
def send_one_off_letter_address(service_id, template_id): if {'recipient', 'placeholders'} - set(session.keys()): # if someone has come here via a bookmark or back button they might have some stuff still in their session return redirect( url_for('.send_one_off', service_id=service_id, template_id=template_id)) db_template = current_service.get_template_with_user_permission_or_403( template_id, current_user) template = get_template(db_template, current_service, show_recipient=True, letter_preview_url=url_for( 'no_cookie.send_test_preview', service_id=service_id, template_id=template_id, filetype='png', ), page_count=get_page_count_for_letter(db_template), email_reply_to=None, sms_sender=None) current_session_address = PostalAddress.from_personalisation( get_normalised_placeholders_from_session()) form = LetterAddressForm( address=current_session_address.normalised, allow_international_letters=current_service.has_permission( 'international_letters'), ) if form.validate_on_submit(): session['placeholders'].update( PostalAddress(form.address.data).as_personalisation) placeholders = fields_to_fill_in( template, prefill_current_user=(request.endpoint == 'main.send_test_step'), ) if all_placeholders_in_session(placeholders): return get_notification_check_endpoint(service_id, template) first_non_address_placeholder_index = len(address_lines_1_to_7_keys) return redirect( url_for( 'main.send_one_off_step', service_id=service_id, template_id=template_id, step_index=first_non_address_placeholder_index, )) return render_template( 'views/send-one-off-letter-address.html', page_title=get_send_test_page_title( template_type='letter', help_argument=None, entering_recipient=True, name=template.name, ), template=template, form=form, back_link=get_back_link(service_id, template, 0), help=False, link_to_upload=True, )