def edit_item(category_name, item_title): """Handle the logic related to edit an item to the catalog""" if 'username' not in login_session: return redirect('/login') else: if request.method == 'POST': item_id = request.form['item_id'] # build an item object item = db_helper.build_item(request.form['title'], request.form['description'], request.form['category_id']); # check if the item exists (the item must by unique by category) item_view_db = db_helper.get_item_by_title(item.title, item.category_id) # update the record if the item doesn't exists ot if exists is the sane # item that we are updating if not item_view_db or (item_view_db and item_view_db.id == item_id): db_helper.update_item(item_id, item) return redirect(url_for('category_page', category_name = category_name)) else: categories = db_helper.get_categories() return render_template('editItem.html', item_view=item_view_db, categories=categories, message = 'An item with the same name exists') else: categories = db_helper.get_categories() item_view = db_helper.get_item_by_title_category_view(item_title, category_name) return render_template('editItem.html', item_view=item_view, categories=categories, message = '')
def add_item(): """Handle the logic related to add an item to the catalog""" if 'username' not in login_session: return redirect('/login') else: if request.method == 'POST': # build an item object item = db_helper.build_item(request.form['title'], request.form['description'], request.form['category_id']) # check if the item exists (the item must by unique by category) item_view_db = db_helper.get_item_by_title(item.title, item.category_id) if not item_view_db: db_helper.add_item(item) return redirect(url_for('catalog_page')) else: categories = db_helper.get_categories() return render_template( 'addItem.html', categories=categories, message='An item with the same name exists') else: categories = db_helper.get_categories() return render_template('addItem.html', categories=categories, message='')
def edit_item(category_name, item_title): """Handle the logic related to edit an item to the catalog""" if 'username' not in login_session: return redirect('/login') else: if request.method == 'POST': item_id = request.form['item_id'] # build an item object item = db_helper.build_item(request.form['title'], request.form['description'], request.form['category_id']) # check if the item exists (the item must by unique by category) item_view_db = db_helper.get_item_by_title(item.title, item.category_id) # update the record if the item doesn't exists ot if exists is the sane # item that we are updating if not item_view_db or (item_view_db and item_view_db.id == item_id): db_helper.update_item(item_id, item) return redirect( url_for('category_page', category_name=category_name)) else: categories = db_helper.get_categories() return render_template( 'editItem.html', item_view=item_view_db, categories=categories, message='An item with the same name exists') else: categories = db_helper.get_categories() item_view = db_helper.get_item_by_title_category_view( item_title, category_name) return render_template('editItem.html', item_view=item_view, categories=categories, message='')
def add_item(): """Handle the logic related to add an item to the catalog""" if 'username' not in login_session: return redirect('/login') else: if request.method == 'POST': # build an item object item = db_helper.build_item(request.form['title'], request.form['description'] , request.form['category_id']); # check if the item exists (the item must by unique by category) item_view_db = db_helper.get_item_by_title(item.title, item.category_id) if not item_view_db: db_helper.add_item(item) return redirect(url_for('catalog_page')) else: categories = db_helper.get_categories() return render_template('addItem.html', categories=categories, message = 'An item with the same name exists') else: categories = db_helper.get_categories() return render_template('addItem.html', categories=categories, message = '')