示例#1
0
def edit_item(itemid):
    form = EditItemForm()

    item = Item.query.filter_by(id=itemid).first()

    if request.method == "GET":
        ud_validator.item = item
        ud_validator.user = g.user
        form.name.data = item.name
        form.description.data = item.description

    form.category.choices = [(b.id, b.name)
                             for b in Category.query.order_by("name")]

    if form.validate_on_submit():
        error = ud_validator.is_valid(item)

        if error is not None:
            return redirect(url_for("catalog.error", error=error))

        item.name = form.name.data
        item.description = form.description.data

        item.category = Category.query.filter_by(id=form.category.data).first()

        db.session.add(item)
        db.session.commit()
        return redirect(url_for("items.show_item", itemid=itemid))

    return render_template("items/edititem.html", form=form, item=item)
示例#2
0
def edit(id):
    item = Item.query.filter(Item.user == current_user, Item.id == id).first()
    if not item:
        flash(u"不存在该条目。", 'warning')
        return redirect(url_for('.index'))
    form = EditItemForm()
    if form.validate_on_submit():
        # todo: is there a nicer solution?
        item.tags = []
        item.title = form.title.data
        if form.tags.data:
            tags = form.tags.data.split(',')
            for tag in tags:
                if Tag.query.filter_by(name=tag).first():
                    item.tags.append(Tag.query.filter_by(name=tag).first())
                else:
                    item.tags.append(Tag(name=tag))
        db.session.add(item)
        db.session.commit()
        # parse_html(item.id)
        flash(u'「{} 」,此条目已更新。'.format(item.title or item.link), 'info')
        return redirect(request.args.get('next') or url_for('.index'))
    form.title.data = item.title
    form.tags.data = ','.join([tag.name for tag in item.tags])
    return render_template('edit.html', form=form)
示例#3
0
def edit_item(item_id):
    item = crud.get_item_no_categories(item_id)
    if item:
        form = EditItemForm()
        if form.validate_on_submit():
            filename = save_image_upload(form.image) if form.image.data \
                else item["image"]

            # update data
            # escape()したデータはテンプレートでsafeフィルターを併用すること
            crud.update_item(item_id,
                             title=escape(form.title.data),
                             description=escape(form.description.data),
                             price=float(form.price.data),
                             image=filename)

            flash(
                "Item {} has been successfully updated".format(
                    form.title.data), "success")
            return redirect(url_for("item", item_id=item_id))

        form.title.data = unescape(item["title"])
        form.description.data = unescape(item["description"])
        form.price.data = item["price"]
        return render_template("edit_item.html", item=item, form=form)

    return redirect(url_for("home"))
示例#4
0
文件: app.py 项目: blturley/demilich
def process_edit_item_form(item_id):
    """Edit item."""

    item = Item.query.get_or_404(item_id)

    form = EditItemForm()

    if form.validate_on_submit():

        item.name = form.name.data
        item.image_url = form.image_url.data
        item.description = form.description.data
        item.weight = form.weight.data
        item.armor_class = form.armor_class.data
        item.condition = form.condition.data
        item.rarity = form.rarity.data
        item.quantity = form.quantity.data
        item.notes = form.notes.data

        db.session.commit()

        serial_item = item.serialize()

        return (jsonify(serial_item), 200)

    errors = {"errors": form.errors}
    return jsonify(errors)
示例#5
0
def edit_item():
    item_id = request.args.get("item")
    item = Items.query.filter_by(id=item_id).first()
    form = EditItemForm()
    if form.validate_on_submit():
        item.name = form.item.data
        item.description = form.item_description.data
        db.session.commit()
        return redirect(url_for("index"))
    category = Catalog.query.filter_by(id=item.catalog_id).first()
    form.category.data = category.name
    form.item.data = item.name
    form.item_description.data = item.description
    return render_template("edit_add_item.html", form=form)
示例#6
0
def editItem(category_id, item_id):

    category = session.query(Category).filter_by(id=category_id).one()
    item = session.query(Item).filter_by(id=item_id).one()

    form = EditItemForm(
        obj=item)  # obj=item populates fields with item's values

    if form.validate_on_submit():
        item.name = form.name.data
        item.description = form.description.data
        session.commit()

        flash('Item successfully edited.')
        return redirect(url_for('showCategory', category_id=category_id))

    return render_template('edititem.html',
                           form=form,
                           category=category,
                           item=item)
示例#7
0
def editItem():
    form = EditItemForm()
    db = get_db()
    cur = db.cursor()
    itemName = request.args.get('item')

    item = cur.execute('SELECT * FROM ITEM WHERE Title=? AND Owner_email=?', (
        itemName,
        g.user['Email'],
    )).fetchone()  # gets the item that the user desires to edit
    categories = cur.execute(
        'SELECT Name FROM CATEGORY').fetchall()  # gets all the categories

    if form.validate_on_submit():  # update item using entered information
        if form.category.data is None:  # form.category.data will be none if user doesn't select a new category
            cat = item[1]
        else:
            cat = form.category.data
        cur.execute(
            'UPDATE ITEM SET Title=?, Category_name=?, Description=?, Daily_rate=? WHERE Title=?',
            (
                form.title.data,
                cat,
                form.description.data,
                form.daily_rate.data,
                itemName,
            ))
        db.commit()
        cur.close()
        return redirect(url_for('ownerItems'))

    elif request.method == 'GET':  #populates the form with the selected item's information
        form.title.data = item[0]
        form.category.choices = [(g[0]) for g in categories]
        form.category.data = item[1]
        form.description.data = item[3]
        form.daily_rate.data = item[4]

    return render_template('editItem.html', form=form)