Пример #1
0
def item_update(id=None):
    item = Items.query.get(id)
    form = ItemForm(request.form)
    if request.method == 'POST' and form.validate():
        # print 'update
        # post',form.title,form.reproduce_steps,form.crs,form.jiras,form.log_analysis,form.solution_desc,form.gerrits
        item.title = form.title.data
        item.reproduce_steps = form.reproduce_steps.data
        item.crs = form.crs.data
        item.jiras = form.jiras.data
        item.log_analysis = form.log_analysis.data
        item.solution_desc = form.solution_desc.data
        item.gerrits = form.gerrits.data
        try:
            db.session.commit()
            flash("update " + id + " success!")
        except Exception as e:
            flash("update:" + str(e))
        return redirect(url_for('.home'))
    else:
        if not item:
            flash("id=" + id + " does not exist..")
            return redirect(url_for('.home'))
        form.title.data = item.title
        form.reproduce_steps.data = item.reproduce_steps
        form.crs.data = item.crs
        form.jiras.data = item.jiras
        form.log_analysis.data = item.log_analysis
        form.solution_desc.data = item.solution_desc
        form.gerrits.data = item.gerrits
        return render_template('update.html', form=form, item=item)
Пример #2
0
def item_create():
    """ For creating item
    """
    #Initialize necessary data
    user_id = login_session.get('user_id')
    form = ItemForm(request.form)
    form_action = url_for('catalog.item_create')
    #Process data when user posts data
    if request.method == 'POST' and form.validate():
        try:
            filename = upload_file(request.files['image_file'])
            pprint(form.image_file)
            item = Item(name=form.name.data,
                        description=form.description.data,
                        catalog_id=form.catalog_id.data,
                        user_id=user_id,
                        filename=filename)
            db.session.add(item)
            db.session.commit()
            flash('Item created successfully!')
        except:
            db.session.rollback()
            flash('Failed to create this item!', 'error')
        return redirect('/')
    #Show page to user
    return render_template('catalog/item_create.html',
                           form_action=form_action, form=form)
Пример #3
0
def item_add():
    form = ItemForm(request.form)
    if request.method == 'POST' and form.validate():
        print 'get post', form.title, form.reproduce_steps, form.crs, form.jiras, form.log_analysis, form.solution_desc, form.gerrits
        try:
            item = Items(form.title.data, form.reproduce_steps.data, form.crs.data, form.jiras.data,
                         form.log_analysis.data, form.solution_desc.data, form.gerrits.data)
            db.session.add(item)
            db.session.commit()
        except Exception as e:
            flash(e)
        return redirect(url_for('.home'))
    else:
        return render_template('add.html', form=form)
Пример #4
0
def item_edit(catalog, item):
    """ For modifying item
        arguments:
        catalog : catalog name
        item    : item name
    """
    # Find item
    user_id = login_session.get('user_id')
    this_one = db.session.query(Item).join(Catalog).filter(
        Item.name == item).filter(Catalog.name == catalog).one()
    # Show 404 error if it does not exist
    if not this_one:
        abort(404)
    # Show 401 error if current user is not the owner
    if this_one.user_id != user_id:
        abort(401)
    #Show page to user
    form_action = url_for('catalog.item_edit', catalog=catalog, item=item)
    if request.method == 'GET':
        form = ItemForm(obj=this_one)
        return render_template('catalog/item_edit.html',
                               form_action=form_action, form=form)
    #Process data when user posts data
    form = ItemForm(request.form)
    if request.method == 'POST' and form.validate():
        try:
            filename = upload_file(request.files['image_file'])
            form.populate_obj(this_one)
            this_one.filename = filename
            db.session.merge(this_one)
            db.session.commit()
            flash('Item updated successfully!')
        except:
            db.session.rollback()
            flash('Failed to update this item!', 'error')
        return redirect('/')
    else:
        return redirect(request.path)