Example #1
0
def editItem(name):
    if "user_id" not in session:
        return redirect("/login")

    item = db_session.query(Item).filter_by(name=name).one()

    if item is None:
        abort(404)

    if item.user_id != session["user_id"]:
        abort(401)

    form = ItemForm(obj=item)
    categories = db_session.query(Category.id, Category.name).all()
    form.category.choices = categories
    if form.validate_on_submit():
        # form.populate_obj(item)
        item.name = form.name.data
        item.description = form.description.data
        item.category_id = form.category.data

        filename = None
        # check if user uploaded file and sanitize filename
        if form.image.has_file():
            # gets the filename?
            filename = secure_filename(form.image.data.filename)
            form.image.data.save(os.path.join(app.config["UPLOAD_FOLDER"], filename))
            item.image = filename
        db_session.add(item)
        db_session.commit()
        flash("Item %s edited." % item.name)
        return redirect(url_for("item", name=item.name))
    return render_template("editItem.html", item=item, form=form)
Example #2
0
def newItem():
    if "user_id" not in session:
        return redirect("/login")
    form = ItemForm()
    categories = db_session.query(Category.id, Category.name).all()
    form.category.choices = categories
    if form.validate_on_submit():
        filename = None
        # check if user uploaded file and sanitize filename
        if form.image.has_file():
            # gets the filename?
            filename = secure_filename(form.image.data.filename)
            form.image.data.save(os.path.join(app.config["UPLOAD_FOLDER"], filename))
        # create new item and commit to database
        item = Item(
            name=form.name.data,
            description=form.description.data,
            category_id=form.category.data,
            image=filename,
            user_id=session["user_id"],
            pub_date=datetime.utcnow(),
        )
        db_session.add(item)
        db_session.commit()
        flash("New item created.")
        return redirect(url_for("item", name=item.name))
    return render_template("newItem.html", form=form)
Example #3
0
def item(name):
    item = db_session.query(Item).filter_by(name=name).one()

    if item is None:
        abort(404)

    return render_template("item.html", item=item)
Example #4
0
def category(name):
    category = db_session.query(Category).filter_by(name=name).one()

    if category is None:
        abort(404)

    # items = db_session.query(Item).filter_by(category_id = category.id)
    return render_template("category.html", category=category, items=category.items)
Example #5
0
def deleteItem(name):
    if "user_id" not in session:
        return redirect("/login")

    item = db_session.query(Item).filter_by(name=name).one()

    if item is None:
        abort(404)

    if item.user_id != session["user_id"]:
        abort(401)

    if request.method == "POST":
        db_session.delete(item)
        db_session.commit()
        flash("%s Successfully Deleted" % item.name)
        return redirect(url_for("catalog"))
    else:
        return render_template("deleteItem.html", item=item)
Example #6
0
def deleteCategory(name):
    if "user_id" not in session:
        return redirect("/login")

    category = db_session.query(Category).filter_by(name=name).one()

    if category is None:
        abort(404)

    if category.user_id != session["user_id"]:
        abort(401)

    if request.method == "POST":
        # delete the category, and related items should be deleted automatically
        db_session.delete(category)
        db_session.commit()
        flash("%s Successfully Deleted" % category.name)
        return redirect(url_for("catalog"))
    else:
        return render_template("deleteCategory.html", category=category)
Example #7
0
def editCategory(name):
    if "user_id" not in session:
        return redirect("/login")

    category = db_session.query(Category).filter_by(name=name).one()

    if category is None:
        abort(404)

    if category.user_id != session["user_id"]:
        abort(401)

    form = CategoryForm(obj=category)
    if form.validate_on_submit():
        form.populate_obj(category)
        db_session.add(category)
        db_session.commit()
        flash("Category %s edited." % category.name)
        return redirect(url_for("category", name=category.name))
    return render_template("editCategory.html", category=category, form=form)
Example #8
0
def getUserID(email):
    try:
        user = db_session.query(User).filter_by(email=email).one()
        return user.id
    except:
        return None
Example #9
0
def getUserInfo(user_id):
    user = db_session.query(User).filter_by(id=user_id).one()
    return user
Example #10
0
def createUser(session):
    newUser = User(name=session["username"], email=session["email"], picture=session["picture"])
    db_session.add(newUser)
    db_session.commit()
    user = db_session.query(User).filter_by(email=session["email"]).one()
    return user.id
Example #11
0
def catalog():
    categories = db_session.query(Category).all()
    return render_template("catalog.html", categories=categories)