Exemplo n.º 1
0
def delete_item():
    """
    Endpoint to delete item from catalog
    :return: back to category list
    """
    if login_session.get("email") is not None:
        form = Catalog_Item(request.form)

        if request.method == "POST":
            # find the item
            item = db.session.query(Items).filter_by(id=form.id.data).first()

            # check if the item was found
            if item is not None and login_session.get("email") is not None:
                try:
                    if item.picture is not None:
                        os.remove(
                            os.path.join(os.path.dirname(__file__), "static/images/", os.path.basename(item.picture))
                        )
                except OSError:
                    pass

                # set the URL for redirecting
                url_string = "/category/%s" % item.category.category_name

                db.session.delete(item)
                db.session.commit()
            else:
                url_string = "/"

            return redirect(url_string)
        else:
            item = db.session.query(Items).filter_by(id=request.args.get("item_id")).first()
            heading = "Delete Item"
            form.name.data = item.item_name
            form.description.data = item.description
            form.picture.data = item.picture
            form.id.data = request.args.get("item_id")
            form.category_name.data = item.category.category_name
            form.category_id = item.category_id
            form.submit.label.text = "Delete Item"
            return render_template("pages/delete-item.html", form=form, heading=heading)
Exemplo n.º 2
0
def edit_item():
    """
    Endpoint to edit an item in the catalog
    :return: returns back to category list
    """
    if login_session.get("email") is not None:
        form = Catalog_Item(request.form)

        if request.method == "POST":
            # delete item
            item = db.session.query(Items).filter_by(id=form.id.data).first()

            cate_number = form.category_select.data
            category = db.session.query(Categories).filter_by(category_id=cate_number).one()

            item.item_name = form.name.data
            item.description = form.description.data
            item.insert_date = datetime.datetime.now()
            item.category = category

            if request.files["picture"].filename != "":
                # delete file if one exists
                try:
                    if os.path.basename(item.picture) != u"default_item.png":
                        os.remove(
                            os.path.join(os.path.dirname(__file__), "static/images/", os.path.basename(item.picture))
                        )
                except OSError:
                    pass

                filename = request.files["picture"].filename
                filename = secure_filename(filename)

                # returns a UUID with the file extension
                filename = ensure_unique_filename(filename)

                # builds a URL to put in the database
                item.picture = url_for("static", filename="images/" + filename)

                # read the data and write to a file
                image_data = request.files["picture"].read()
                open(os.path.join(os.path.dirname(__file__), "static/images/", filename), "w").write(image_data)

            db.session.add(item)
            db.session.commit()
            url_string = "/category/%s" % item.category.category_name
            return redirect(url_string)
        else:
            item = db.session.query(Items).filter_by(id=request.args.get("item_id")).first()
            heading = "Edit Item"
            form.category_select.choices = get_all_categories()
            form.category_select.default = item.category_id
            form.process()  # this sets the default in the select but clears the rest of the form.
            form.name.data = item.item_name
            form.description.data = item.description
            form.picture.data = item.picture
            form.id.data = request.args.get("item_id")
            form.category_id = item.category_id

            form.submit.label.text = "Save Changes"
            # form.process()
            return render_template("pages/edit-item.html", form=form, heading=heading)