Beispiel #1
0
def post_precompiled_letter_notification():
    if "content" not in (request.get_json() or {}):
        return post_notification(LETTER_TYPE)

    form = validate(request.get_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)

    form["personalisation"] = {"address_line_1": form["reference"]}

    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
Beispiel #2
0
def post_precompiled_letter_notification():
    if 'content' not in (request.get_json() or {}):
        return post_notification(LETTER_TYPE)

    form = validate(request.get_json(), post_precompiled_letter_request)

    # Check both permission to send letters and permission to send pre-compiled PDFs
    check_service_has_permission(LETTER_TYPE, authenticated_service.permissions)
    check_service_has_permission(PRECOMPILED_LETTER, authenticated_service.permissions)

    check_rate_limiting(authenticated_service, api_user)

    template = get_precompiled_letter_template(authenticated_service.id)

    form['personalisation'] = {
        'address_line_1': form['reference']
    }

    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
    }

    return jsonify(resp), 201
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
Beispiel #4
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
Beispiel #5
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
Beispiel #6
0
def post_notification(notification_type):
    try:
        request_json = request.get_json()
    except werkzeug.exceptions.BadRequest as e:
        raise BadRequestError(message="Error decoding arguments: {}".format(e.description),
                              status_code=400)

    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,
        content=str(template_with_content),
        url_root=request.url_root,
        scheduled_for=scheduled_for
    )
    return jsonify(resp), 201
Beispiel #7
0
def send_notification(notification_type):

    if notification_type not in [SMS_TYPE, EMAIL_TYPE]:
        msg = "{} notification type is not supported".format(notification_type)
        msg = msg + ", please use the latest version of the client" if notification_type == LETTER_TYPE else msg
        raise InvalidRequest(msg, 400)

    notification_form, errors = (sms_template_notification_schema
                                 if notification_type == SMS_TYPE else
                                 email_notification_schema).load(
                                     request.get_json())

    if errors:
        raise InvalidRequest(errors, status_code=400)

    check_rate_limiting(authenticated_service, api_user)

    template = templates_dao.dao_get_template_by_id_and_service_id(
        template_id=notification_form['template'],
        service_id=authenticated_service.id)

    check_template_is_for_notification_type(notification_type,
                                            template.template_type)
    check_template_is_active(template)

    template_object = create_template_object_for_notification(
        template, notification_form.get('personalisation', {}))

    _service_allowed_to_send_to(notification_form, authenticated_service)
    if not service_has_permission(notification_type,
                                  authenticated_service.permissions):
        raise InvalidRequest(
            {
                'service': [
                    "Cannot send {}".format(
                        get_public_notify_type_text(notification_type,
                                                    plural=True))
                ]
            },
            status_code=400)

    if notification_type == SMS_TYPE:
        _service_can_send_internationally(authenticated_service,
                                          notification_form['to'])
    # Do not persist or send notification to the queue if it is a simulated recipient
    simulated = simulated_recipient(notification_form['to'], notification_type)
    notification_model = persist_notification(
        template_id=template.id,
        template_version=template.version,
        recipient=request.get_json()['to'],
        service=authenticated_service,
        personalisation=notification_form.get('personalisation', None),
        notification_type=notification_type,
        api_key_id=api_user.id,
        key_type=api_user.key_type,
        simulated=simulated,
        reply_to_text=template.get_reply_to_text())
    if not simulated:
        queue_name = QueueNames.PRIORITY if template.process_type == PRIORITY else None
        send_notification_to_queue(
            notification=notification_model,
            research_mode=authenticated_service.research_mode,
            queue=queue_name)
    else:
        current_app.logger.debug(
            "POST simulated notification for id: {}".format(
                notification_model.id))
    notification_form.update({"template_version": template.version})

    return jsonify(data=get_notification_return_data(
        notification_model.id, notification_form, template_object)), 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
def post_notification(notification_type):
    try:
        request_json = request.get_json()
    except werkzeug.exceptions.BadRequest as e:
        raise BadRequestError(message="Error decoding arguments: {}".format(
            e.description),
                              status_code=400)

    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:
        if 'email_address' in form or 'phone_number' in form:
            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)
        else:
            if accept_recipient_identifiers_enabled():
                notification = process_notification_with_recipient_identifier(
                    form=form,
                    notification_type=notification_type,
                    api_key=api_user,
                    template=template,
                    service=authenticated_service,
                    reply_to_text=reply_to)
            else:
                current_app.logger.debug(
                    "Sending a notification without contact information is not implemented."
                )
                return jsonify(result='error', message="Not Implemented"), 501

        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)
    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,
                               content=str(template_with_content),
                               url_root=request.url_root,
                               scheduled_for=scheduled_for)
    return jsonify(resp), 201