def item_add():
    """Returns the page to add a new Item."""
    form = item_forms.ItemAddForm()
    form.category.choices = [(x.id, x.name) for x in catalog.get_all_categories()]
    if form.validate_on_submit():
        catalog.create_item(form.name.data, int(form.category.data), form.description.data)
        return root({"success": "Item added successfully!"})
    return flask.render_template("item_add.html", form=form)
def root(messages=None):
    """The root homepage.

    :param messages: A dictionary of messages that will display
    at the top of the homepage.
    """
    categories = catalog.get_all_categories()

    return flask.render_template("home.html", categories=categories, messages=messages)
def item_edit(item_id):
    """Returns a page to edit the given item.

    :param item_id: database id of the requested item.
    """
    form = item_forms.ItemEditForm()
    form.category.choices = [(x.id, x.name) for x in catalog.get_all_categories()]
    item_object = catalog.get_item(item_id)
    if form.validate_on_submit():
        catalog.update_item(item_object.id, form.name.data, form.category.data, form.description.data)
        return root({"success": "Item updated successfully!"})
    form.name.data = item_object.name
    form.category.data = item_object.category
    form.description.data = item_object.description
    return flask.render_template("item_edit.html", form=form, item=item_object)