Пример #1
0
def edittask(task_id):
    task = Task.query.get_or_404(task_id)
    task.permissions.edit.test(403)
    from_case = list()
    from_code = list()
    tmp_case = db.engine.execute("select cases_id from task_case where task_id=%s" % (task_id)).fetchall()
    tmp_code = db.engine.execute("select code_id from task_code where task_id=%s" % (task_id)).fetchall()
    if tmp_case:
        from_case = list(zip(*tmp_case)[0])
    if tmp_code:
        from_code = list(zip(*tmp_code)[0])
    form = EditTaskForm(task, next=request.referrer)
    form.moduletype_id.choices = [(item.id, _(item.name))
                                  for item in ModuleType.query.
                                  get_allsubmodule()]

    form.machine_id.choices = [(item.id, _(item.name))
                               for item in Machine.query.
                               get_all_ok()]

    '''In order to facilitate the query name, not to repeat the database query'''
    query_dict = dict(form.machine_id.choices)

    if form.validate_on_submit():
        '''
        if task.author_id != g.user.id:
            task.author_id = g.user.id
        '''
        task.task_update_time = datetime.now()
        form.populate_obj(task)
        if task.parent_id != ModuleType.query.\
                get_or_404(form.moduletype_id.data).parent_id:
            task.parent_id = ModuleType.query.\
                get_or_404(form.moduletype_id.data).parent_id
        case = []
        for case_id in request.form.getlist('case_test'):
            case.append(Case.query.get_or_404(case_id))

        code = []

        for code_id in request.form.getlist('code_test'):
            code.append(Code.query.get_or_404(code_id))
        task.case = case
        task.code = code

        '''Notice:If master hudson is not named master,please change master to your name.'''
        node_name = query_dict.get(form.machine_id.data, 'master')

        if task.svn:
            message = dict(name=task.email_topic, start_time=task.runtime,
                           svn=[eval(i) for i in task.svn.split(';')],
                           url=request.url_root + "notice/hudson/",
                           mail_address=task.email, node_name=node_name,
                           shell="\n".join(['hg clone %s' % (j) for j in [i.path for i in task.code]]) + '\n' + task.command)
        else:
            message = dict(name=task.email_topic, start_time=task.runtime,
                           url=request.url_root + "notice/hudson/",
                           svn=task.svn, mail_address=task.email, node_name=node_name,
                           shell="\n".join(['hg clone %s' % (j) for j in [i.path for i in task.code]]) + '\n' + task.command)
        hudson_job = hud.job_exists(task.name)
        if task.access == 100:
            if hudson_job:
                try:
                    hud.reconfig_job(task.name, build(message))
                    task.hudson = 4
                except HudsonException:
                    hud.disable_job(task.name)
                    task.hudson = 2
                finally:
                    db.session.add(task)
                    db.session.commit()
                    clear_cache()
                    flash(u"Update {0} successfully!".format(task.name), "success")
                    return redirect(form.next.data or url_for("task.showtask"))

            else:
                try:
                    hud.create_job(task.name, build(message))
                    task.hudson = 4
                except HudsonException:
                    task.hudson = 2
                finally:
                    db.session.add(task)
                    db.session.commit()
                    clear_cache()
                    flash(u"Update {0} successfully!".format(task.name), "success")
                    return redirect(form.next.data or url_for("task.showtask"))
        else:
            try:
                hud.delete_job(task.name)
            except HudsonException:
                pass
            finally:
                task.hudson = 2
                db.session.add(task)
                db.session.commit()
                clear_cache()
                db.engine.execute("delete from task_count where task_id=%s" % (task_id))
                flash(u"Update {0} successfully!".format(task.name), "success")
                return redirect(form.next.data or url_for("task.showtask"))
    parents = ModuleType.query.get_parent()
    nodes = ModuleType.query.get_allsubmodule()
    return render_template('task/edittask.html', form=form, parents=parents,
                           nodes=nodes, from_case=from_case, from_code=from_code)
Пример #2
0
def edittask(task_id):
    task = Task.query.get_or_404(task_id)
    task.permissions.edit.test(403)
    from_case = list()
    from_code = list()
    tmp_case = db.engine.execute(
        "select cases_id from task_case where task_id=%s" %
        (task_id)).fetchall()
    tmp_code = db.engine.execute(
        "select code_id from task_code where task_id=%s" %
        (task_id)).fetchall()
    if tmp_case:
        from_case = list(zip(*tmp_case)[0])
    if tmp_code:
        from_code = list(zip(*tmp_code)[0])
    form = EditTaskForm(task, next=request.referrer)
    form.moduletype_id.choices = [
        (item.id, _(item.name))
        for item in ModuleType.query.get_allsubmodule()
    ]

    form.machine_id.choices = [(item.id, _(item.name))
                               for item in Machine.query.get_all_ok()]
    '''In order to facilitate the query name, not to repeat the database query'''
    query_dict = dict(form.machine_id.choices)

    if form.validate_on_submit():
        '''
        if task.author_id != g.user.id:
            task.author_id = g.user.id
        '''
        task.task_update_time = datetime.now()
        form.populate_obj(task)
        if task.parent_id != ModuleType.query.\
                get_or_404(form.moduletype_id.data).parent_id:
            task.parent_id = ModuleType.query.\
                get_or_404(form.moduletype_id.data).parent_id
        case = []
        for case_id in request.form.getlist('case_test'):
            case.append(Case.query.get_or_404(case_id))

        code = []

        for code_id in request.form.getlist('code_test'):
            code.append(Code.query.get_or_404(code_id))
        task.case = case
        task.code = code
        '''Notice:If master hudson is not named master,please change master to your name.'''
        node_name = query_dict.get(form.machine_id.data, 'master')

        if task.svn:
            message = dict(
                name=task.email_topic,
                start_time=task.runtime,
                svn=[eval(i) for i in task.svn.split(';')],
                url=request.url_root + "notice/hudson/",
                mail_address=task.email,
                node_name=node_name,
                shell="\n".join(
                    ['hg clone %s' % (j)
                     for j in [i.path
                               for i in task.code]]) + '\n' + task.command)
        else:
            message = dict(
                name=task.email_topic,
                start_time=task.runtime,
                url=request.url_root + "notice/hudson/",
                svn=task.svn,
                mail_address=task.email,
                node_name=node_name,
                shell="\n".join(
                    ['hg clone %s' % (j)
                     for j in [i.path
                               for i in task.code]]) + '\n' + task.command)
        hudson_job = hud.job_exists(task.name)
        if task.access == 100:
            if hudson_job:
                try:
                    hud.reconfig_job(task.name, build(message))
                    task.hudson = 4
                except HudsonException:
                    hud.disable_job(task.name)
                    task.hudson = 2
                finally:
                    db.session.add(task)
                    db.session.commit()
                    clear_cache()
                    flash(u"Update {0} successfully!".format(task.name),
                          "success")
                    return redirect(form.next.data or url_for("task.showtask"))

            else:
                try:
                    hud.create_job(task.name, build(message))
                    task.hudson = 4
                except HudsonException:
                    task.hudson = 2
                finally:
                    db.session.add(task)
                    db.session.commit()
                    clear_cache()
                    flash(u"Update {0} successfully!".format(task.name),
                          "success")
                    return redirect(form.next.data or url_for("task.showtask"))
        else:
            try:
                hud.delete_job(task.name)
            except HudsonException:
                pass
            finally:
                task.hudson = 2
                db.session.add(task)
                db.session.commit()
                clear_cache()
                db.engine.execute("delete from task_count where task_id=%s" %
                                  (task_id))
                flash(u"Update {0} successfully!".format(task.name), "success")
                return redirect(form.next.data or url_for("task.showtask"))
Пример #3
0
def addtask():
    from_case = []
    if session.get(g.user.username):
        from_case = session.get(g.user.username)
    form = AddTaskForm(next=request.referrer)
    form.moduletype_id.choices = [(item.id, _(item.name))
                                  for item in ModuleType.query.
                                  get_allsubmodule()]

    form.machine_id.choices = [(item.id, _(item.name))
                               for item in Machine.query.
                               get_all_ok()]
    '''In order to facilitate the query name, not to repeat the database query'''
    query_dict = dict(form.machine_id.choices)

    if form.validate_on_submit():
        task = Task()
        task.author_id = g.user.id
        form.populate_obj(task)
        case = []

        '''Notice:request.form.getlist('case_test') or request.form.getlist('code_test')
        may be need to remove duplicate elements.'''

        for case_id in request.form.getlist('case_test'):
            case.append(Case.query.get_or_404(case_id))

        code = []

        for code_id in request.form.getlist('code_test'):
            code.append(Code.query.get_or_404(code_id))

        task.case = case
        task.code = code
        task.parent_id = ModuleType.query.get_or_404(form.moduletype_id.data).parent_id

        '''Notice:If master hudson is not named master,please change master to your name.'''
        node_name = query_dict.get(form.machine_id.data, 'master')

        if task.svn:
            message = dict(name=task.email_topic, start_time=task.runtime,
                           svn=[eval(i) for i in task.svn.split(';')],
                           url=request.url_root + "notice/hudson/",
                           mail_address=task.email, node_name=node_name,
                           shell="\n".join(['hg clone %s' % (j) for j in [i.path for i in task.code]]) + '\n' + task.command)
        else:
            message = dict(name=task.email_topic, start_time=task.runtime,
                           url=request.url_root + "notice/hudson/",
                           svn=task.svn, mail_address=task.email, node_name=node_name,
                           shell="\n".join(['hg clone %s' % (j) for j in [i.path for i in task.code]]) + '\n' + task.command)

        if task.access == 100:
            try:
                hud.create_job(task.name, build(message))
                task.hudson = 4
                db.session.add(task)
                db.session.commit()
                flash(u"Add {0} successfully!".format(task.name), "success")
            except HudsonException:
                task.hudson = 2
                task.access = 300
                db.session.add(task)
                db.session.commit()
                flash(u"Add {0} fail!".format(task.name), "fail")
            finally:
                clear_cache()
                return redirect(form.next.data or url_for("task.showtask"))
        else:
            task.hudson = 2
            db.session.add(task)
            db.session.commit()
            clear_cache()
            return redirect(form.next.data or url_for("task.showtask"))
    parents = ModuleType.query.get_parent()
    nodes = ModuleType.query.get_allsubmodule()
    return render_template('task/task.html', form=form, parents=parents,
                           nodes=nodes, from_case=from_case)
Пример #4
0
def addtask():
    from_case = []
    if session.get(g.user.username):
        from_case = session.get(g.user.username)
    form = AddTaskForm(next=request.referrer)
    form.moduletype_id.choices = [
        (item.id, _(item.name))
        for item in ModuleType.query.get_allsubmodule()
    ]

    form.machine_id.choices = [(item.id, _(item.name))
                               for item in Machine.query.get_all_ok()]
    '''In order to facilitate the query name, not to repeat the database query'''
    query_dict = dict(form.machine_id.choices)

    if form.validate_on_submit():
        task = Task()
        task.author_id = g.user.id
        form.populate_obj(task)
        case = []
        '''Notice:request.form.getlist('case_test') or request.form.getlist('code_test')
        may be need to remove duplicate elements.'''

        for case_id in request.form.getlist('case_test'):
            case.append(Case.query.get_or_404(case_id))

        code = []

        for code_id in request.form.getlist('code_test'):
            code.append(Code.query.get_or_404(code_id))

        task.case = case
        task.code = code
        task.parent_id = ModuleType.query.get_or_404(
            form.moduletype_id.data).parent_id
        '''Notice:If master hudson is not named master,please change master to your name.'''
        node_name = query_dict.get(form.machine_id.data, 'master')

        if task.svn:
            message = dict(
                name=task.email_topic,
                start_time=task.runtime,
                svn=[eval(i) for i in task.svn.split(';')],
                url=request.url_root + "notice/hudson/",
                mail_address=task.email,
                node_name=node_name,
                shell="\n".join(
                    ['hg clone %s' % (j)
                     for j in [i.path
                               for i in task.code]]) + '\n' + task.command)
        else:
            message = dict(
                name=task.email_topic,
                start_time=task.runtime,
                url=request.url_root + "notice/hudson/",
                svn=task.svn,
                mail_address=task.email,
                node_name=node_name,
                shell="\n".join(
                    ['hg clone %s' % (j)
                     for j in [i.path
                               for i in task.code]]) + '\n' + task.command)

        if task.access == 100:
            try:
                hud.create_job(task.name, build(message))
                task.hudson = 4
                db.session.add(task)
                db.session.commit()
                flash(u"Add {0} successfully!".format(task.name), "success")
            except HudsonException:
                task.hudson = 2
                task.access = 300
                db.session.add(task)
                db.session.commit()
                flash(u"Add {0} fail!".format(task.name), "fail")
            finally:
                clear_cache()
                return redirect(form.next.data or url_for("task.showtask"))
        else:
            task.hudson = 2
            db.session.add(task)
            db.session.commit()
            clear_cache()
            return redirect(form.next.data or url_for("task.showtask"))