예제 #1
0
    def post(self):
        form = CreateHostForm()
        hosts = Host.query.order_by(Host.type.asc(), Host.name.asc()).all()
        if form.validate_on_submit():
            try:
                # Create database entry
                h = Host()
                h.name = form.name.data or None
                h.hostname = form.hostname.data or None
                h.region = form.region.data or None
                h.uri = form.uri.data or None
                h.active = False
                h.type = form.type.data or None
                h.username = form.username.data or None
                h.password = form.password.data or None

                db.session.add(h)
                db.session.commit()
                return redirect('/admin/hosts/')

            except:
                import traceback
                db.session.rollback()
                traceback.print_exc()
                return redirect('/admin/hosts/')

        return render_template('admin/hosts.html', form=form, hosts=hosts)
예제 #2
0
def add_host():
    if request.json:
        host = Host()
        host.uuid = str(uuid.uuid1())
        host.name = request.json['name']
        host.ip = request.json['ip']
        host.port = request.json['port']
        host.password = request.json['password']
        host.status = request.json['status']
        host.create_time = datetime.utcnow()
        db.session.add(host)

        try:
            db.session.commit()
            data = {'message': '新建数据成功'}

        except SQLAlchemyError as e:
            db.session.rollback()
            data = {'message': ' 新建数据失败'}

    else:
        data = {'message': '请求数据异常'}

    # return jsonify(data)

    # 支持跨域
    response = make_response(jsonify(data))
    response.headers['Access-Control-Allow-Origin'] = '*'
    response.headers['Access-Control-Allow-Methods'] = 'PUT,GET,POST,DELETE'
    response.headers[
        'Access-Control-Allow-Headers'] = 'Referer,Accept,Origin,User-Agent'
    return response
예제 #3
0
            ['中国银行', '中国工商银行', '中国农业银行', '中国建设银行', '中国交通银行', '招商银行', '民生银行'])
        #idc.groups.add(1)  # many 多对多 这样赋值是不行的!
        idc.save()

        # many 多对多 这样赋值
        idc = IDC.objects.get(id=i + 1)
        idc.groups.add(1)  # many
        idc.save()

    HOST_NUM = 10
    for i in range(HOST_NUM):
        h = Host()
        h.idc = IDC.objects.get(id=i + 1)
        h.name = 'Host名称-%s' % i
        h.user = random.choice(names)
        h.password = '******'
        h.memory = random.choice([4, 8, 16])
        h.service_type = SERVICE_TYPES[random.randint(0, 8)][0]
        h.description = '描述 --' + ''.join(
            [str(random.randint(0, 9)) for _i in range(8)])
        h.administrator = User.objects.get(id=i + 1)
        h.save()

    HOST_NUM = 10
    for i in range(HOST_NUM):
        h = HostGroup()
        h.name = 'Host名称-%s' % i
        h.description = '描述 --' + ''.join(
            [str(random.randint(0, 9)) for _i in range(8)])
        h.save()