Example #1
0
def remove_item(item_id):
    """
    Remove item from data base

    :param item_id:
    :return string: JSON
    """
    item = get_item_by_id(item_id) or None

    # check the item exist
    if not item:
        return jsonify({'error': 'This record don\'t exist'})

    # check the user is the owner
    if int(item.author) != int(g.user.id):
        return jsonify(
            {'error': 'You don\'t have permission to delete this record'})

    images = get_images_by_item_id(item_id)

    # remove images files
    for image in images:
        path = ''.join([BASE_DIR, image.url])

        if os.path.isfile(path):
            os.unlink(path)

    # remove item and images from database
    delete_item(item_id)
    remove_images_by_item_id(item_id)
    return jsonify({'message': 'Record was deleted'})
Example #2
0
def delete_car(item_id):
    """
    Remove car and all images

    :param item_id:
    :return:
    """

    # Get car
    car = get_item_by_id(item_id)

    # check if the user is the owner
    if int(car.author) != int(session['uid']):
        # crate a error message and redirect user
        flash('You don\'t have permission to remove this object', 'error')
        return redirect('/profile', 302)

    if request.method == 'POST':

        # get images
        images = get_images_by_item_id(item_id)

        # get title
        title = car.title

        # remove images files
        for image in images:
            # get absolute path to image
            path = ''.join([BASE_DIR, image.url])
            # if file exist remove the image file
            if os.path.isfile(path):
                os.unlink(path)

        # remove images from from database
        remove_images_by_item_id(item_id)

        # remove data of car from database
        delete_item(item_id)

        # crate a success message and redirect user
        flash('Car: "%s" was removed' % title, 'success')
        return redirect('/profile', 302)

    return render('catalog/delete_car.html',
                  brands=brands,
                  car=car.serialize,
                  csrf_token=csrf_token)