예제 #1
0
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'))
예제 #2
0
def delete_category(category_id):
    # Fetch the category to be deleted from the DB
    category = Category.by_id(id=category_id)

    # Handle POST request
    if request.method == 'POST':

        # if the user is logged in and is the creator, proceed
        if is_logged_in() and is_creator(category.user_id):
            # delete all items in the category
            items_in_category = Item.by_category_id(category_id=category_id)
            for item in items_in_category:
                Item.delete(item)
            # delete the category
            Category.delete(category)
            # redirect after deletion
            flash("Category %s deleted successfully" % category.name)
            return redirect(url_for('home_page'))

    # Handle GET request
    else:
        # If user is logged in and is the creator, render the delete confirmation page
        if is_logged_in() and is_creator(category.user_id):
            return render_template('delete_category.html', category=category)
        # if user is not logged in or not the creator, redirect to the error page
        else:
            return redirect(url_for('error'))