def delete_user(user_id, review_name):
    '''
    admin only deleting a user from database
    '''
    if session['admin']:
        DB_REVIEWS.remove({'username': review_name})
        DB_USERS.remove({'_id': ObjectId(user_id)})
        return redirect(url_for('admin_tab_users'))
    return render_template('no_login.html')
示例#2
0
def delete_user(user_id):
    """ Deletes the users db record , also Delete the cloadinary image data and all users recipe data
       :return
           Redirect to sign in view
        """
    # REFERENCE CREDITS:
    # Cloudinary api ->
    # https://github.com/tiagocordeiro/flask-cloudinary

    # User exists in the database and Signed in
    if session.get('USERNAME', None):
        username = session['USERNAME']
        # user exists in the database
        current_user = DB_USERS.find_one({'username': username})
        requested_user = DB_USERS.find_one({'_id': ObjectId(user_id)})

        # if the current logged in user is the user requested
        if requested_user['_id'] == current_user['_id']:
            # Delete related user records
            DB_RECIPES.delete_many({'author_id': ObjectId(user_id)})
            DB_INGREDIENTS.delete_many({'author_id': ObjectId(user_id)})
            DB_METHODS.delete_many({'author_id': ObjectId(user_id)})

            # Delete the current user image form cloudinary
            current_user = DB_USERS.find_one({'_id': ObjectId(user_id)})
            if current_user['profile_image_id'] != 'wbzphoxefkdid3kheuqd':
                destroy(current_user['profile_image_id'], invalidate=True)

            # Delete main user record
            DB_USERS.remove({'_id': ObjectId(user_id)})
            return redirect(url_for('sign_out'))

        else:
            # raises a 404 error if any of these fail
            return abort(403, description="Forbidden")
    else:
        # raises a 404 error if any of these fail
        return abort(403, description="Forbidden")