Exemplo n.º 1
0
def host_add_post():
    group_id = request.form['group_id']
    if not group_id:
        return jsonify(msg='no group_id given')

    group_id = int(group_id)
    group = HostGroup.read('id = %s', [group_id])
    if not group:
        return jsonify(msg='no such group')

    hosts = request.form['hosts'].strip()
    if not hosts:
        return jsonify(msg='hosts is blank')

    host_arr = hosts.splitlines()
    safe_host_arr = [h for h in host_arr if h]
    if not safe_host_arr:
        return jsonify(msg='hosts is blank')

    success = []
    failure = []

    for h in safe_host_arr:
        msg = GroupHost.bind(group_id, h)
        if not msg:
            success.append('%s<br>' % h)
        else:
            failure.append('%s %s<br>' % (h, msg))

    data = '<div class="alert alert-danger" role="alert">failure:<hr>' + ''.join(
        failure
    ) + '</div><div class="alert alert-success" role="alert">success:<hr>' + ''.join(
        success) + '</div>'

    return jsonify(msg='', data=data)