def editCatalogItem(category_name, item_name):
    """Edit the attributes of an item in the database

    Args:
        category_name(str): the name of the category the item belongs to
        item_name(str): the name of the item

    Returns:
        render the editcatalogitem template if a GET request is sent
        redirect to the category page if the POST request succeeds"""
    if 'provider' in login_session:
        editedItem = session.query(CatalogItem).filter_by(name=item_name).one()
        form = FormItem()
        if form.validate_on_submit():
            if hasattr(form, 'name'):
                editedItem.name = form.name.data
            if hasattr(form, 'description'):
                editedItem.description = form.description.data
            if hasattr(form, 'image_loc'):
                editedItem.image_loc = form.image_loc.data
            session.add(editedItem)
            session.commit()
            return redirect(
                url_for('showCategory', category_name=category_name))
        return render_template('editcatalogitem.html',
                               category_name=category_name,
                               item_name=item_name,
                               item=editedItem,
                               form=form)
    else:
        flash("Please login to be able to edit an item")
        return redirect(url_for('showCategory', category_name=category_name))
def editCatalogItem(category_name, item_name):
    """Edit the attributes of an item in the database

    Args:
        category_name(str): the name of the category the item belongs to
        item_name(str): the name of the item

    Returns:
        render the editcatalogitem template if a GET request is sent
        redirect to the category page if the POST request succeeds"""
    if "provider" in login_session:
        editedItem = session.query(CatalogItem).filter_by(name=item_name).one()
        form = FormItem()
        if form.validate_on_submit():
            if hasattr(form, "name"):
                editedItem.name = form.name.data
            if hasattr(form, "description"):
                editedItem.description = form.description.data
            if hasattr(form, "image_loc"):
                editedItem.image_loc = form.image_loc.data
            session.add(editedItem)
            session.commit()
            return redirect(url_for("showCategory", category_name=category_name))
        return render_template(
            "editcatalogitem.html", category_name=category_name, item_name=item_name, item=editedItem, form=form
        )
    else:
        flash("Please login to be able to edit an item")
        return redirect(url_for("showCategory", category_name=category_name))
def newCatalogItem(category_name):
    """Create a new item in the database

    Args:
        category_name(str): the name of the category the item belongs to

    Returns:
        render the newcatalogitem template if a GET request is sent
        redirect to the category page if the POST request succeeds"""
    if 'provider' in login_session:
        form = FormItem()
        if form.validate_on_submit():
            category = session.query(Category).filter_by(
                name=category_name).one()
            newItem = CatalogItem(name=form.name.data,
                                  description=form.description.data,
                                  category_id=category.id,
                                  image_loc=form.image_loc.data)
            session.add(newItem)
            session.commit()
            return redirect(
                url_for('showCategory', category_name=category_name))
        return render_template('newcatalogitem.html',
                               category_name=category_name,
                               form=form)
    else:
        flash("Please login to be able to add a new item")
        return redirect(url_for('showCategory', category_name=category_name))
def newCatalogItem(category_name):
    """Create a new item in the database

    Args:
        category_name(str): the name of the category the item belongs to

    Returns:
        render the newcatalogitem template if a GET request is sent
        redirect to the category page if the POST request succeeds"""
    if "provider" in login_session:
        form = FormItem()
        if form.validate_on_submit():
            category = session.query(Category).filter_by(name=category_name).one()
            newItem = CatalogItem(
                name=form.name.data,
                description=form.description.data,
                category_id=category.id,
                image_loc=form.image_loc.data,
            )
            session.add(newItem)
            session.commit()
            return redirect(url_for("showCategory", category_name=category_name))
        return render_template("newcatalogitem.html", category_name=category_name, form=form)
    else:
        flash("Please login to be able to add a new item")
        return redirect(url_for("showCategory", category_name=category_name))