Beispiel #1
0
def create_template(service_id):
    fetched_service = dao_fetch_service_by_id(service_id=service_id)
    # permissions needs to be placed here otherwise marshmallow will interfere with versioning
    permissions = [p.permission for p in fetched_service.permissions]
    template_json = validate(request.get_json(), post_create_template_schema)
    folder = validate_parent_folder(template_json=template_json)
    new_template = Template.from_json(template_json, folder)

    if not service_has_permission(new_template.template_type, permissions):
        message = "Creating {} templates is not allowed".format(
            get_public_notify_type_text(new_template.template_type))
        errors = {'template_type': [message]}
        raise InvalidRequest(errors, 403)

    if not new_template.postage and new_template.template_type == LETTER_TYPE:
        new_template.postage = SECOND_CLASS

    new_template.service = fetched_service

    over_limit = _content_count_greater_than_limit(new_template.content,
                                                   new_template.template_type)
    if over_limit:
        message = 'Content has a character count greater than the limit of {}'.format(
            SMS_CHAR_COUNT_LIMIT)
        errors = {'content': [message]}
        raise InvalidRequest(errors, status_code=400)

    check_reply_to(service_id, new_template.reply_to,
                   new_template.template_type)

    dao_create_template(new_template)

    return jsonify(data=template_schema.dump(new_template).data), 201
Beispiel #2
0
def create_template(service_id):
    fetched_service = dao_fetch_service_by_id(service_id=service_id)
    # permissions needs to be placed here otherwise marshmallow will intefere with versioning
    permissions = fetched_service.permissions
    new_template = template_schema.load(request.get_json()).data

    if not service_has_permission(new_template.template_type, permissions):
        message = "Creating {} templates is not allowed".format(
            get_public_notify_type_text(new_template.template_type))
        errors = {'template_type': [message]}
        raise InvalidRequest(errors, 403)

    new_template.service = fetched_service
    over_limit = _content_count_greater_than_limit(new_template.content,
                                                   new_template.template_type)
    if over_limit:
        char_count_limit = SMS_CHAR_COUNT_LIMIT
        message = 'Content has a character count greater than the limit of {}'.format(
            char_count_limit)
        errors = {'content': [message]}
        raise InvalidRequest(errors, status_code=400)

    check_reply_to(service_id, new_template.reply_to,
                   new_template.template_type)

    dao_create_template(new_template)
    return jsonify(data=template_schema.dump(new_template).data), 201
Beispiel #3
0
def update_template(service_id, template_id):
    fetched_template = dao_get_template_by_id_and_service_id(
        template_id=template_id, service_id=service_id)

    if not service_has_permission(
            fetched_template.template_type,
        [p.permission for p in fetched_template.service.permissions]):
        message = "Updating {} templates is not allowed".format(
            get_public_notify_type_text(fetched_template.template_type))
        errors = {'template_type': [message]}

        raise InvalidRequest(errors, 403)

    data = request.get_json()
    validate(data, post_update_template_schema)

    # if redacting, don't update anything else
    if data.get('redact_personalisation') is True:
        return redact_template(fetched_template, data)

    if "reply_to" in data:
        check_reply_to(service_id, data.get("reply_to"),
                       fetched_template.template_type)
        updated = dao_update_template_reply_to(template_id=template_id,
                                               reply_to=data.get("reply_to"))
        return jsonify(data=template_schema.dump(updated).data), 200

    current_data = dict(template_schema.dump(fetched_template).data.items())
    updated_template = dict(
        template_schema.dump(fetched_template).data.items())
    updated_template.update(data)

    # Check if there is a change to make.
    if _template_has_not_changed(current_data, updated_template):
        return jsonify(data=updated_template), 200

    over_limit = _content_count_greater_than_limit(
        updated_template['content'], fetched_template.template_type)
    if over_limit:
        message = 'Content has a character count greater than the limit of {}'.format(
            SMS_CHAR_COUNT_LIMIT)
        errors = {'content': [message]}
        raise InvalidRequest(errors, status_code=400)

    update_dict = template_schema.load(updated_template).data

    dao_update_template(update_dict)
    return jsonify(data=template_schema.dump(update_dict).data), 200
Beispiel #4
0
def check_service_has_permission(notify_type, permissions):
    if not service_has_permission(notify_type, permissions):
        raise BadRequestError(
            message="Service is not allowed to send {}".format(
                get_public_notify_type_text(notify_type, plural=True)))
Beispiel #5
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