Example #1
0
def delete_item(item_id):
    item = crud.get_item_no_categories(item_id)
    if item:
        crud.delete_item(item_id)
        flash("Item {} has been successfully deleted.".format(item["title"]),
              "success")
    else:
        flash("This item does not exist.", "danger")

    return redirect(url_for("home"))
Example #2
0
def delete_item():

    print(request)
    content = request.get_json()
    print(f'THIS IS THE CONTENT: {content}')
    name = content['name']
    quantity = content['quantity']
    expiration = content['expiration']

    item = crud.get_item_by_info(name=name, quantity=quantity, expiration_date=expiration)

    crud.delete_item(item)

    return jsonify('ITEM DELETED!')
Example #3
0
def delete_item(item_id):
    """Delete an item by id"""

    if 'username' not in login_session:
        return render_template('forbidden.html',
                               error=constants.FORBIDDEN_ERROR_MSG)

    item = crud.item_by_id(item_id)
    cat_name = item.category.name
    user_id = user_helper.get_user_id(login_session['email'])
    user = user_helper.get_user_info(user_id)
    if user.email == item.user.email:
        crud.delete_item(item_id)
    else:
        error = 'You can delete only items that you created!'
        return render_template('forbidden.html', error=error)

    flash(u'Item Successfully Deleted', 'success')

    return redirect(
        url_for('item.get_items_by_category', category_name=cat_name))
Example #4
0
def delete_item(item_name, item_id):
    """ Allows logged in users to delete an item
    item_name:  only required for route, not used
    item_id:    the id of the item which is to be deleted

    Can only be accessed by logged in users

    This function uses snippet parts of
    http://flask.pocoo.org/snippets/3 for nonces """

    context = generate_context(item_id=item_id)

    # Only allow item deletion on POST request
    if request.method == 'POST':
        delete = request.form['delete']
        token = login_session.pop('_csrf_token')
        logging.debug("The CSRF token: %s" % request.form['_csrf_token'])
        logging.debug("The login session's expected CSRF token: %s" % token)

        # Prevent deletion if checkbox not ticked or CSRF token incorrect
        if delete:
            if request.form['_csrf_token'] != token:
                logging.info("Incorrect CSRF token. Redirecting...")
                abort(403)

            logging.debug("CSRF tokens are matching. Deleting item.")

            crud.delete_item(item_id)
            flash("The item was successfully deleted")
            return redirect(url_for('index'), code=302)

    # Generate a random CSRF token and show delete form
    login_session['_csrf_token'] = generate_token()
    return render_template(
        'item_delete_form.html',
        categories=context['categories'],
        item=context['items'],
        title="Edit item - " + item_name,
        csrf_token=login_session['_csrf_token'])
Example #5
0
def delete_item(item_name, item_id):
    """ Allows logged in users to delete an item
    item_name:  only required for route, not used
    item_id:    the id of the item which is to be deleted

    Can only be accessed by logged in users

    This function uses snippet parts of
    http://flask.pocoo.org/snippets/3 for nonces """

    context = generate_context(item_id=item_id)

    # Only allow item deletion on POST request
    if request.method == 'POST':
        delete = request.form['delete']
        token = login_session.pop('_csrf_token')
        logging.debug("The CSRF token: %s" % request.form['_csrf_token'])
        logging.debug("The login session's expected CSRF token: %s" % token)

        # Prevent deletion if checkbox not ticked or CSRF token incorrect
        if delete:
            if request.form['_csrf_token'] != token:
                logging.info("Incorrect CSRF token. Redirecting...")
                abort(403)

            logging.debug("CSRF tokens are matching. Deleting item.")

            crud.delete_item(item_id)
            flash("The item was successfully deleted")
            return redirect(url_for('index'), code=302)

    # Generate a random CSRF token and show delete form
    login_session['_csrf_token'] = generate_token()
    return render_template('item_delete_form.html',
                           categories=context['categories'],
                           item=context['items'],
                           title="Edit item - " + item_name,
                           csrf_token=login_session['_csrf_token'])
Example #6
0
def delete_item(item_id: int, db: Session = Depends(get_db)):
    db_item = crud.delete_item(db, item_id=item_id)
    if db_item is None:
        raise HTTPException(status_code=404, detail="Item to delete not found")
    return db_item
Example #7
0
def delete_item(item: schemas.ItemRead, db: Session = Depends(get_db)):
    db_item = crud.delete_item(db, name=item.name)
    if db_item is None:
        raise HTTPException(status_code=404, detail="Item not found")
    return db_item