def json_products():
    categories = access.getCategories()
    for category in categories:
        category.Items = access.getProductCategory(category.category_id)
        for product in category.Items:
            product.category_name = access.getCategory(product.category_id)
    return jsonify(Category=[i.serialize for i in categories])
def category_json(name):
    productItems = []
    categories = access.getCategories()
    categories = access.getCategoryByName(name)
    for category in categories:
        category.Items = access.getProductCategory(category.category_id)
        for product in category.Items:
            product.category_name = access.getCategory(product.category_id)
    return jsonify(Category=[i.serialize for i in categories])
def deleteProduct(name):
    """Delete defined product if you are the user who owns it"""
    product = access.getProductByName(name)
    product.category = access.getCategory(product.category_id)
    if login_session["email"] == product.user_id:
        if request.method == "POST":
            access.session.delete(product)
            access.session.commit()
            flash("Product Deleted %s" % product.product_name)
            return redirect(url_for("categories"))
        else:
            return render_template("deleteProduct.html", product=product)
    else:
        flash("No permission to delete %s" % login_session["username"])
        return redirect(url_for("products"))
def recent_feed():
    feed = AtomFeed("Recent Products", feed_url=request.url, url=request.url_root)
    categories = access.getCategories()
    productItems = access.getProductCount(15)
    for product in productItems:
        product.category_name = access.getCategory(product.category_id)
        feed.add(
            product.product_name,
            unicode(product.product_description),
            content_type="html",
            author=product.user_id,
            url=make_external(url_for("getProduct", name=product.product_name)),  # noqa
            updated=product.updated or product.created,
            published=product.created,
        )
    return feed.get_response()
def products():
    """Returns the last 5 products for each category"""
    """Should have two panels - LHS lists categories, and initial RHS view lists
    most recent products across all categories plus price and category
    category and/or product are clickable, to return either the category page or
    the product page.
    Users can edit, create, or delete only the items that they created
    """
    productItems = []
    categories = access.getCategories()
    for category in categories:
        category.count = access.countItemsByCategory(category.category_name)
        productItems += access.getProductCountCategory(category.category_id, 5)
    for product in productItems:
        product.category_name = access.getCategory(product.category_id)
        if product.product_image:
            product.product_url = "uploads/" + product.product_image
    return render_template("products.html", categories=categories, products=productItems)
def editProduct(name):
    categories = access.getCategories()
    product = access.getProductByName(name)
    if product.product_image:
        product.product_url = "/uploads/", product.product_image
    product.category = access.getCategory(product.category_id)
    if request.method == "POST":
        product_image = request.files["product_image"]
        if product_image and allowed_file(product_image.filename):
            filename = secure_filename(product_image.filename)
            product_image.save(os.path.join(app.config["UPLOAD_FOLDER"], filename))
            product.product_image = filename
        product.product_name = request.form["product_name"]
        product.price = request.form["price"]
        product.product_description = request.form["product_description"]
        product.category_id = request.form["category_id"]
        product.updated = datetime.now()
        flash("Product Edited %s" % product.product_name)
        access.session.commit()
        return redirect(request.referrer)
    else:
        return render_template("editProduct.html", product=product, categories=categories)
def product_json(name):
    """Display JSON record for product"""
    product = access.getProductByName(name)
    product.category_name = access.getCategory(product.category_id)
    return jsonify(Items=[product.serialize])
def getProduct(name):
    product = access.getProductByName(name)
    if product.product_image:
        product.product_url = "uploads/" + product.product_image
    product.category = access.getCategory(product.category_id)
    return render_template("product.html", product=product)