def showItem(item_id):
    """
	Show an item and its description.
	
	URLs:
		'/catalog/item/<int:item_id>/'
	
	Args:
		item_id: The id of the item to show
	"""

    item = readItem(item_id)
    if item == None:
        return showError("There is no item with id=%s" % item_id)
    creator = readUserInfo(item.user_id)
    category = readCategory(item.category_id)
    if 'username' not in login_session or creator.id != login_session[
            'user_id']:
        return render_template('public_show_item.html',
                               item=item,
                               category=category,
                               creator=creator)
    else:
        return render_template('show_item.html',
                               item=item,
                               category=category,
                               creator=creator)
def showCategoryJSON(category_id):
    """
	Get a single category in JSON format.
	
	URLs:
		'/catalog/category/<int:category_id>/JSON/'
	
	Args:
		category_id: The id of the catagory to get
	"""

    category = readCategory(category_id)
    if category == None:
        return showError("There is no category with id=%s" % category_id)
    items = readItems(category_id)
    return jsonify(items=[i.serialize for i in items])
def showCategoryJSON(category_id):
	"""
	Get a single category in JSON format.
	
	URLs:
		'/catalog/category/<int:category_id>/JSON/'
	
	Args:
		category_id: The id of the catagory to get
	"""

	category = readCategory(category_id)
	if category == None:
		return showError("There is no category with id=%s" % category_id)
	items = readItems(category_id)
	return jsonify(items=[i.serialize for i in items])
def newItem(category_id):
    """
	Create a new item in the catalog.
	
	URLs:
		'/catalog/category/<int:category_id>/newitem/'
	
	Args:
		category_id: The id of the category for the new item
	"""

    if 'username' not in login_session:
        return redirect('/login')
    user_id = login_session['user_id']
    category = readCategory(category_id)
    if request.method == 'POST':
        createItem(request.form['name'], request.form['description'],
                   category_id, user_id)
        return redirect(url_for('showCategory', category_id=category_id))
    else:
        return render_template('new_item.html', category=category)
def editCategory(category_id):
	"""
	Edit a category.
	
	URLs:
		'/catalog/category/<int:category_id>/edit/'
	
	Args:
		category_id: The id of the catgory to edit.
	"""

	if 'username' not in login_session:
		return redirect('/login')
	category = readCategory(category_id)
	if login_session['user_id'] != category.user_id:
		return "<script>function myFunction() {alert('You are not authorized to edit this category.');}</script><body onload='myFunction()''>"
	if request.method == 'POST':
		updateCategory(category_id, request.form['name'], request.form['description'])
		return redirect(url_for('showCategory', category_id=category_id))
	else:
		return render_template('edit_category.html', category = category)
def newItem(category_id):
	"""
	Create a new item in the catalog.
	
	URLs:
		'/catalog/category/<int:category_id>/newitem/'
	
	Args:
		category_id: The id of the category for the new item
	"""

	if 'username' not in login_session:
		return redirect('/login')
	user_id = login_session['user_id']
	category = readCategory(category_id)
	if request.method == 'POST':
		createItem(request.form['name'], request.form['description'], 
			category_id, user_id)
		return redirect(url_for('showCategory', category_id=category_id))
	else:
		return render_template('new_item.html', category=category)
def showCategory(category_id):
	"""
	Show all the items in a category, given a category id.
	
	URLs:
		'/catalog/category/<int:category_id>/'
	
	Args:
		category_id: The id of the category to show
	"""

	category = readCategory(category_id)
	if category == None:
		return showError("There is no category with id=%s" % category_id)
	items = readItems(category_id)
	if 'username' not in login_session:
		return render_template('public_show_category.html', items = items, 
			category=category)
	else:
		return render_template('show_category.html', items=items, 
			category=category)
def editCategory(category_id):
    """
	Edit a category.
	
	URLs:
		'/catalog/category/<int:category_id>/edit/'
	
	Args:
		category_id: The id of the catgory to edit.
	"""

    if 'username' not in login_session:
        return redirect('/login')
    category = readCategory(category_id)
    if login_session['user_id'] != category.user_id:
        return "<script>function myFunction() {alert('You are not authorized to edit this category.');}</script><body onload='myFunction()''>"
    if request.method == 'POST':
        updateCategory(category_id, request.form['name'],
                       request.form['description'])
        return redirect(url_for('showCategory', category_id=category_id))
    else:
        return render_template('edit_category.html', category=category)
def editItem(item_id):
    """
	Edit an item in the catalog.
	
	URLs:
		'/catalog/item/<int:item_id>/edit/'
	
	Args:
		item_id: The id of the item to edit
	"""

    if 'username' not in login_session:
        return redirect('/login')
    item = readItem(item_id)
    if login_session['user_id'] != item.user_id:
        return "<script>function myFunction() {alert('You are not authorized to edit this item.');}</script><body onload='myFunction()''>"
    category = readCategory(item.category_id)
    if request.method == 'POST':
        updateItem(item_id, request.form['name'], request.form['description'])
        return redirect(url_for('showItem', item_id=item_id))
    else:
        return render_template('edit_item.html', item=item, category=category)
def editItem(item_id):
	"""
	Edit an item in the catalog.
	
	URLs:
		'/catalog/item/<int:item_id>/edit/'
	
	Args:
		item_id: The id of the item to edit
	"""

	if 'username' not in login_session:
		return redirect('/login')
	item = readItem(item_id)
	if login_session['user_id'] != item.user_id:
		return "<script>function myFunction() {alert('You are not authorized to edit this item.');}</script><body onload='myFunction()''>"
	category = readCategory(item.category_id)
	if request.method == 'POST':
		updateItem(item_id, request.form['name'], request.form['description'])
		return redirect(url_for('showItem', item_id=item_id))
	else:
		return render_template('edit_item.html', item=item, category=category)
def showItem(item_id):
	"""
	Show an item and its description.
	
	URLs:
		'/catalog/item/<int:item_id>/'
	
	Args:
		item_id: The id of the item to show
	"""

	item = readItem(item_id)
	if item == None:
		return showError("There is no item with id=%s" % item_id)
	creator = readUserInfo(item.user_id)
	category = readCategory(item.category_id)
	if 'username' not in login_session or creator.id != login_session['user_id']:
		return render_template('public_show_item.html', item=item, 
			category=category, creator=creator)
	else:
		return render_template('show_item.html', item=item, category=category,
			creator=creator)
def showCategory(category_id):
    """
	Show all the items in a category, given a category id.
	
	URLs:
		'/catalog/category/<int:category_id>/'
	
	Args:
		category_id: The id of the category to show
	"""

    category = readCategory(category_id)
    if category == None:
        return showError("There is no category with id=%s" % category_id)
    items = readItems(category_id)
    if 'username' not in login_session:
        return render_template('public_show_category.html',
                               items=items,
                               category=category)
    else:
        return render_template('show_category.html',
                               items=items,
                               category=category)