def attach_picture_url(table, item_id, location):
    """
    A helper function used in populator.py to upload picture to the db from web
    Args:
        table: The category which the picture belongs to
        item_id: The category's id number which the picture should be
                 uploaded to
        location: a web url of where the picture is found
    Returns:
        None
    """
    try:
        item = session.query(table).filter_by(id=item_id).one()
        with store_context(fs_store):
            item.picture.from_file(urlopen(location))
            session.commit()
    except Exception:
        session.rollback()
        raise
def delete(dbtype, item_id):
    """Delete either the item or category in the database"""
    # check login status
    if 'email' not in login_session:
        flash('Sorry, the page you tried to access is for members only. '
              'Please sign in first.')
        abort(401)

    # query the item user wants to delete
    deleteItem = (session.query(eval(dbtype[0].upper()+dbtype[1:]))
                  .filter_by(id=item_id).one())

    # make sure user is authorized to delete this item
    if login_session['user_id'] != deleteItem.user_id:
        flash('You are not authorized to modify items you did not create. '
              'Please create your own item in order to modify it.')
        return redirect(url_for(dbtype))

    if request.method == 'POST':
        try:
            session.delete(deleteItem)
            session.commit()
        # handling legacy error when delete invovled cascade-delete
        except IntegrityError as detail:
            print 'Handling run-time error: ', detail
            session.rollback()
            flash('Delete Operation Failed')
            return redirect(url_for('home'))
        if dbtype.endswith('Lot'):
            flash('%s Lot Deleted' % dbtype[:-3].capitalize())
            return redirect(url_for(dbtype[:-3]))
        else:
            flash('%s  Deleted' % dbtype.capitalize())
            return redirect(url_for(dbtype))
    else:
        pass