Ejemplo n.º 1
0
def add_sample_item(o_name, o_email, c_name, i_name, filename):
    """
    Adds an item to the catalog database for testing or creating intial data
    :param o_name: name of owner
    :param o_email: email address of owner
    :param c_name: category for item
    :param i_name: item name
    :param filename: filename for item picture
    :return: none
    """
    owner = db.session.query(Owners).filter_by(email=o_email).first()
    if not owner:
        owner = Owners(owner_name=o_name, email=o_email)
        db.session.add(owner)
        db.session.commit()

    category = db.session.query(Categories).filter_by(category_name=c_name).first()
    if not category:
        category = Categories(category_name=c_name, owner=owner)
        db.session.add(category)
        db.session.commit()

    item = Items(item_name=i_name, owner=owner, category=category)

    unique_filename = create_random_filename(filename)
    file1 = os.path.join(os.path.dirname(__file__), 'webExample/static/', filename)
    file2 = os.path.join(os.path.dirname(__file__), 'webExample/static/images/', unique_filename)
    shutil.copyfile(file1, file2)

    item.picture = os.path.join('/static/images/', unique_filename)
    db.session.add(item)
    db.session.commit()
    return
Ejemplo n.º 2
0
def add_item():
    """
    Adds new item to database.  Category is on request object.
    :return:
    """
    if login_session.get("email") is not None:
        form = Catalog_Item(request.form)

        owner = db.session.query(Owners).filter_by(email=login_session.get("email")).first()

        form.category_select.choices = get_all_categories()

        if request.method == "POST" and form.validate():
            # add data

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

            # category = db.session.query(Categories).first()

            # create a new Item
            item = Items(item_name=form.name.data, owner=owner, category=category)

            # add the description
            item.description = form.description.data

            # add the filename to the database and upload the file
            # need to validate the file name
            if request.files["picture"].filename != "":

                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)

            # add the item and commit to db
            db.session.add(item)
            db.session.commit()

            # build a url for redirection.  Goes back to the same category the
            # item was added in.
            url_string = "/category/%s" % item.category.category_name
            return redirect(url_string)
        else:
            form.category_id.data = request.args.get("category")

            form.category_select.default = request.args.get("category")
            form.process()  # this sets the default in the select but clears the rest of the form.

            form.id.data = owner.id
            form.category_select.choices = get_all_categories()
            form.submit.label.text = "Add Item"
        return render_template("pages/add-item.html", form=form)