Ejemplo n.º 1
0
def project_add():
    if request.method == 'GET':
        form = ProjectAddForm()
        return render_template('project_add.html',form=form)
    print(request.form)
    form = ProjectAddForm(formdata=request.form)
    if form.validate():
        print('验证成功',form.data)
        # {'x2': u'asdf', 'x3': u'asdf', 'x1': u'asdf', 'x4': [1, 2]})
        """
        1.创建项目 models.Project
        2.创建关系
        """
        # models.Project(name='test',title="test",repository='test')
        # obj = models.Project(**{"name":'test','title':"test",'repository':'test'})
        # 方式一:
        """
        data_dict = form.data
        host_id_list = data_dict.pop('hosts')
        obj = models.Project(**data_dict)
        db.session.add(obj)
        db.session.commit()

        p2h_list = []
        for host_id in host_id_list:
            p2h_list.append(models.Project2Host(host_id=host_id,project_id=obj.id))
        db.session.add_all(p2h_list)
        db.session.commit()
        """
        # 方式二:
        data_dict = form.data
        host_id_list = data_dict.pop('hosts')

        host_object_list = db.session.query(models.Host).filter(models.Host.id.in_(host_id_list)).all()
        obj = models.Project(**data_dict)
        obj.hosts = host_object_list

        db.session.add(obj)
        db.session.commit()
        db.session.remove()
    return render_template('project_add.html', form=form)
Ejemplo n.º 2
0
def project_add():
    from wtforms.fields import simple
    from wtforms.fields import core
    from wtforms import validators
    from wtforms import form

    class ProjectAddForm(form.Form):
        name = simple.StringField(
            label=u'名称',
            validators=[validators.DataRequired(message=u'名称不能为空')],
            render_kw={'class': 'form-control', 'placeholder': u'项目名称'}
        )
        title = simple.StringField(
            label=u'描述',
            validators=[validators.DataRequired(message=u'描述不能为空')],
            render_kw={'class': 'form-control', 'placeholder': u'项目描述'}
        )

        repository = simple.StringField(
            label=u'地址',
            validators=[validators.DataRequired(message=u'地址不能为空')],
            render_kw={'class': 'form-control', 'placeholder': u'项目地址'}
        )
        hosts = core.SelectMultipleField(
            label=u'主机',
            choices=(),
            coerce=int,
            render_kw={'class': 'form-control'}
        )

        def __init__(self, *args, **kwargs):
            super(ProjectAddForm, self).__init__(*args, **kwargs)

            self.hosts.choices = db.session.query(models.Host.id, models.Host.hostname).all()

    if request.method == 'GET':
        form = ProjectAddForm()
        return render_template('project_add.html', form=form)
    print(request.form)
    form = ProjectAddForm(formdata=request.form)
    if form.validate():
        print('验证成功', form.data)
        # {'x2': u'asdf', 'x3': u'asdf', 'x1': u'asdf', 'x4': [1, 2]})
        """
        1.创建项目 models.Project
        2.创建关系
        """
        # models.Project(name='test',title="test",repository='test')
        # obj = models.Project(**{"name":'test','title':"test",'repository':'test'})
        # 方式一:
        """
        data_dict = form.data
        host_id_list = data_dict.pop('hosts')
        obj = models.Project(**data_dict)
        db.session.add(obj)
        db.session.commit()

        p2h_list = []
        for host_id in host_id_list:
            p2h_list.append(models.Project2Host(host_id=host_id,project_id=[图片]obj.id))
        db.session.add_all(p2h_list)
        db.session.commit()
        """
        # 方式二:
        data_dict = form.data
        host_id_list = data_dict.pop('hosts')

        host_object_list = db.session.query(models.Host).filter(models.Host.id.in_(host_id_list)).all()
        obj = models.Project(**data_dict)
        obj.hosts = host_object_list

        db.session.add(obj)
        db.session.commit()
        db.session.remove()
    return render_template('project_add.html', form=form)
Ejemplo n.º 3
0
    # db.session.commit()
    # db.session.add_all([
    #     models.Project2Host(host_id=obj1.id, project_id=obj3.id),
    #     models.Project2Host(host_id=obj2.id, project_id=obj3.id)
    # ])
    # db.session.commit()
    #简便写法
    # obj3 = models.Project(title='运维平台2', name='devops2',repository='asdfgh')
    # obj3.hosts=[models.Host(hostname='c33.com', port=80),models.Host(hostname='c44.com', port=80)]
    # db.session.add(obj3)
    # db.session.commit()
    # db.session.remove()

    #
    user1 = models.UserInfo(username='******', password='******', nickname='武')
    user2 = models.UserInfo(username='******', password='******', nickname='兴普')
    pro1 = models.Project(title='公司官网',
                          name='web1',
                          repository='https://github.com/WuPeiqi/dbhot.git')
    pro1.hosts = [
        models.Host(hostname='c33.com', port=80),
        models.Host(hostname='c44.com', port=80)
    ]
    db.session.add_all([
        user1,
        user2,
        pro1,
    ])
    db.session.commit()
    db.session.remove()
    # pass