Esempio n. 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)
Esempio n. 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)
Esempio n. 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"))
Esempio n. 4
0
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)
Esempio n. 5
0
def modify_item(sku):
    query = db.session.query(relations.Item.title,
                             relations.Item.description, relations.Item.category, relations.Item.quantity,
                             relations.Item.price, relations.Item.image).filter(relations.Item.sku == sku).first()
    item = {'title': query.title,
            'description': query.description, 'category': query.category, 'quantity': query.quantity,
            'price': query.price, 'image': query.image}
    if request.method == 'GET':
        form = EditItemForm(formdata=MultiDict(
            {'title': query.title, 'description': query.description, 'category': query.category,
             'quantity': query.quantity,
             'price': query.price, 'image': query.image}))
        form.category.choices = [x.category for x in db.session.query(relations.Item.category).distinct()]
    elif request.method == 'POST':
        print('yes')
        form = EditItemForm()
        #if form.validate_on_submit():
        modify_item_query = db.session.query(relations.Item).filter_by(sku=sku).first()
        relations.Item.updateItem(form.title.data,
                                   form.description.data,
                                   form.category.data,
                                   form.quantity.data,
                                   form.price.data,
                                   form.image.data, sku)
        db.session.commit()
        return redirect(url_for('seller_page'))
    return render_template('edit_item.html', item=item, form=form)
Esempio n. 6
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)
Esempio n. 7
0
def get_edit_item(item_id):
    """get html for edit item form."""

    item = Item.query.get_or_404(item_id)

    form = EditItemForm(obj=item)

    response_json = jsonify(
        render_template('edititem.html', form=form, item=item))
    return (response_json, 200)
Esempio n. 8
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)
Esempio n. 9
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)
Esempio n. 10
0
    def get(self, item_id):
        item = Item.objects.get_or_404(item_id=item_id)
        # only admins or the item submitter can edit the item
        if item.submitter.id != current_user.id:
            if not current_user.is_admin:
                abort(403)

        form = None
        if item.github:
            form = EditGithubItemForm()
        else:
            form = EditItemForm()
            form.description.default = item.description

            licenses = License.objects.all()
            form.set_licenses(licenses)
            form.license.default = str(item.license.license_id)

        categories = Category.objects.all()
        form.set_categories(categories, g.lang)
        form.category.default = str(item.category.category_id)
        form.process()

        return render_template('items/edit_item.html', form=form, item=item)
Esempio n. 11
0
    def post(self, item_id):
        item = Item.objects.get_or_404(item_id=item_id)

        # only admins or the item submitter can edit the item
        if item.submitter.id != current_user.id:
            if not current_user.is_admin:
                abort(403)

        form = None
        if item.github:
            form = EditGithubItemForm()
        else:
            form = EditItemForm()
            licenses = License.objects.all()
            form.set_licenses(licenses)

        categories = Category.objects.all()
        form.set_categories(categories, g.lang)

        if form.validate_on_submit():
            for title in item.titles:  # ugly, I'll make it shorter, later...
                if title.lang == 'ar':
                    title.title = form.ar_title.data.strip()
                elif title.lang == 'en':
                    title.title = form.en_title.data.strip()
                else:
                    title.title = form.fr_title.data.strip()

            item.tags = form.tags.data.strip().split(',')
            item.category = Category.objects.get(
                category_id=int(form.category.data))

            if form.thumbnail.data:  # if the user has uploaded new thumbnail
                # remove the old one
                item.thumbnail.delete()
                # replace it with the new one
                thumbnail = request.files['thumbnail']
                thumbnail_name = secure_filename(thumbnail.filename)

                if thumbnail and allowed_thumbnails(thumbnail_name):
                    ext = thumbnail.mimetype.split('/')[-1]
                    # use the 'thumbnail' name for all thumbnails
                    filename = '.'.join(["thumbnail", ext])
                    item.thumbnail.put(thumbnail.stream,
                                       content_type=thumbnail.mimetype,
                                       filename=filename)

            if form.blog_post.data.strip():
                item.blog_post = form.blog_post.data
            if not item.github:
                item.description = form.description.data
                item.license = License.objects.get(
                    license_id=int(form.license.data))
            else:
                item.github = form.github.data
                item.save()
                # no need to process any uploaded files
                flash('Item updated successfully', category='success')
                return render_template('items/edit_item.html',
                                       form=form,
                                       item=item)

        else:
            flash("Couldn't update item", category='error')
            return render_template('items/edit_item.html',
                                   form=form,
                                   item=item)

        # now, replace them with the new ones
        uploaded_files = request.files.getlist("files")
        new_files = []
        for file in uploaded_files:
            # Make the filename safe, remove unsupported chars
            filename = secure_filename(file.filename)
            # Check if the file is one of the allowed types/extensions
            if file and allowed_file(filename):
                # put the file in the ListField.
                # see https://gist.github.com/tfausak/1299339
                file_ = GridFSProxy()
                file_.put(file.stream,
                          content_type=file.mimetype,
                          filename=filename)
                new_files.append(file_)
        if len(new_files) > 0:
            # delete old files first
            for file in item.files:
                file.delete()
            # push the new one
            item.files = new_files

        # Save the thing
        item.save()
        flash('Item updated successfully', category='success')
        return render_template('items/edit_item.html', form=form, item=item)