def delete_item(category_id, item_id): # Fetch the item to be deleted from the DB item = Item.by_cat_id_and_item_id(category_id=category_id, item_id=item_id) # Handle POST request if request.method == 'POST': # If user is logged in and is creator, proceed if is_logged_in() and is_creator(item.user_id): Item.delete(item) flash("Item %s deleted successfully" % item.name) return redirect(url_for('home_page')) # If user is not logged in or not the creator, redirect to the error page else: return redirect(url_for('error')) # Handle GET request else: # if user is logged in or is the creator, render the delete confirmation page if is_logged_in() and is_creator(item.user_id): category = Category.by_id(id=category_id) return render_template('delete_item.html', category=category, item=item) # If not, redirect to the error page else: return redirect(url_for('error.html'))
def update_item(category_id, item_id): # Fetch the item to be updated from DB item = Item.by_cat_id_and_item_id(category_id=category_id, item_id=item_id) # Handle POST request if request.method == 'POST': # If logged in and the user is the creator, proceed if is_logged_in() and is_creator(item.user_id): # Get new values from the sumbitted form new_name = request.form['name'] new_description = request.form['description'] new_cat_id = request.form['category'] # update the item with new values item.update(new_name=new_name, new_description=new_description, new_cat_id=new_cat_id) # Redirect after deletion flash("Item %s successfully updated" % item.name) return redirect(url_for("home_page")) # If not logged in or not the creator, redirec to the error page else: return redirect(url_for('error.html')) # Handle GET request else: # if user is logged in and is the creator, render the update form if is_logged_in() and is_creator(item.user_id): return render_template('update_item.html', category_id=category_id, item=item) # if not logged in or not the creator, redirec to the error page else: return redirect(url_for('error.html'))
def category_item(category_id, item_id): item = Item.by_cat_id_and_item_id(category_id, item_id) return render_template('category_item.html', item=item)