def post_precompiled_letter_notification():
    request_json = get_valid_json()
    if 'content' not in (request_json or {}):
        return post_notification(LETTER_TYPE)

    form = validate(request_json, post_precompiled_letter_request)

    # Check permission to send letters
    check_service_has_permission(LETTER_TYPE,
                                 authenticated_service.permissions)

    check_rate_limiting(authenticated_service, api_user)

    template = get_precompiled_letter_template(authenticated_service.id)

    # For precompiled letters the to field will be set to Provided as PDF until the validation passes,
    # then the address of the letter will be set as the to field
    form['personalisation'] = {'address_line_1': 'Provided as PDF'}

    reply_to = get_reply_to_text(LETTER_TYPE, form, template)

    notification = process_letter_notification(letter_data=form,
                                               api_key=api_user,
                                               template=template,
                                               reply_to_text=reply_to,
                                               precompiled=True)

    resp = {
        'id': notification.id,
        'reference': notification.client_reference,
        'postage': notification.postage
    }

    return jsonify(resp), 201
Пример #2
0
def post_precompiled_letter_notification():
    request_json = get_valid_json()
    if 'content' not in (request_json or {}):
        return post_notification(LETTER_TYPE)

    form = validate(request_json, post_precompiled_letter_request)

    # Check permission to send letters
    check_service_has_permission(LETTER_TYPE,
                                 authenticated_service.permissions)

    check_rate_limiting(authenticated_service, api_user)

    template = get_precompiled_letter_template(authenticated_service.id)

    # For precompiled letters the to field will be set to Provided as PDF until the validation passes,
    # then the address of the letter will be set as the to field
    form['personalisation'] = {'address_line_1': 'Provided as PDF'}

    notification = process_letter_notification(
        letter_data=form,
        api_key=api_user,
        service=authenticated_service,
        template=template,
        template_with_content=None,  # not required for precompiled
        reply_to_text='',  # not required for precompiled
        precompiled=True)

    return jsonify(notification), 201
Пример #3
0
def post_template_preview(template_id):
    # The payload is empty when there are no place holders in the template.
    _data = request.get_data(as_text=True)
    if not _data:
        _data = {}
    else:
        _data = get_valid_json()

    _data['id'] = template_id

    data = validate(_data, post_template_preview_request)

    template = templates_dao.dao_get_template_by_id_and_service_id(
        template_id, authenticated_service.id)

    template_object = template._as_utils_template_with_personalisation(
        data.get('personalisation')
    )

    check_placeholders(template_object)

    resp = create_post_template_preview_response(template=template,
                                                 template_object=template_object)

    return jsonify(resp), 200
Пример #4
0
def post_notification(notification_type):
    with POST_NOTIFICATION_JSON_PARSE_DURATION_SECONDS.time():
        request_json = get_valid_json()

        if notification_type == EMAIL_TYPE:
            form = validate(request_json, post_email_request)
        elif notification_type == SMS_TYPE:
            form = validate(request_json, post_sms_request)
        elif notification_type == LETTER_TYPE:
            form = validate(request_json, post_letter_request)
        else:
            abort(404)

    check_service_has_permission(notification_type,
                                 authenticated_service.permissions)

    check_rate_limiting(authenticated_service, api_user)

    template, template_with_content = validate_template(form['template_id'],
                                                        form.get(
                                                            'personalisation',
                                                            {}),
                                                        authenticated_service,
                                                        notification_type,
                                                        check_char_count=False)

    reply_to = get_reply_to_text(notification_type, form, template)

    if notification_type == LETTER_TYPE:
        notification = process_letter_notification(
            letter_data=form,
            api_key=api_user,
            service=authenticated_service,
            template=template,
            template_with_content=template_with_content,
            reply_to_text=reply_to)
    else:
        notification = process_sms_or_email_notification(
            form=form,
            notification_type=notification_type,
            template=template,
            template_with_content=template_with_content,
            template_process_type=template.process_type,
            service=authenticated_service,
            reply_to_text=reply_to)

    return jsonify(notification), 201
def post_notification(notification_type):
    request_json = get_valid_json()

    if notification_type == EMAIL_TYPE:
        form = validate(request_json, post_email_request)
    elif notification_type == SMS_TYPE:
        form = validate(request_json, post_sms_request)
    elif notification_type == LETTER_TYPE:
        form = validate(request_json, post_letter_request)
    else:
        abort(404)

    check_service_has_permission(notification_type,
                                 authenticated_service.permissions)

    scheduled_for = form.get("scheduled_for", None)

    check_service_can_schedule_notification(authenticated_service.permissions,
                                            scheduled_for)

    check_rate_limiting(authenticated_service, api_user)

    template, template_with_content = validate_template(
        form['template_id'],
        form.get('personalisation', {}),
        authenticated_service,
        notification_type,
    )

    reply_to = get_reply_to_text(notification_type, form, template)

    if notification_type == LETTER_TYPE:
        notification = process_letter_notification(letter_data=form,
                                                   api_key=api_user,
                                                   template=template,
                                                   reply_to_text=reply_to)
    else:
        notification = process_sms_or_email_notification(
            form=form,
            notification_type=notification_type,
            api_key=api_user,
            template=template,
            service=authenticated_service,
            reply_to_text=reply_to)

        template_with_content.values = notification.personalisation

    if notification_type == SMS_TYPE:
        create_resp_partial = functools.partial(
            create_post_sms_response_from_notification,
            from_number=reply_to,
        )
    elif notification_type == EMAIL_TYPE:
        create_resp_partial = functools.partial(
            create_post_email_response_from_notification,
            subject=template_with_content.subject,
            email_from='{}@{}'.format(
                authenticated_service.email_from,
                current_app.config['NOTIFY_EMAIL_DOMAIN']),
        )
    elif notification_type == LETTER_TYPE:
        create_resp_partial = functools.partial(
            create_post_letter_response_from_notification,
            subject=template_with_content.subject,
        )

    resp = create_resp_partial(
        notification=notification,
        url_root=request.url_root,
        scheduled_for=scheduled_for,
        content=template_with_content.content_with_placeholders_filled_in,
    )
    return jsonify(resp), 201