Exemplo n.º 1
0
def add_favorite(recipe_id):
    """ Adds the recipe to the users favorites list in the users profile
     :return
         Redirect to recipe url with updated favorite
    """
    updated_favorites = []
    # User is signed in and exists in the database
    if session.get('USERNAME', None):
        username = session['USERNAME']
        user = DB_USERS.find_one({'username': username})
        recipe = DB_RECIPES.find_one({'_id': ObjectId(recipe_id)})
        #  Update users favorites
        current_favorites = user['favorites']
        # if the recipe is not already in the list
        if ObjectId(recipe_id) not in current_favorites:
            current_favorites.append(ObjectId(recipe_id))

        DB_USERS.update_one({'_id': user['_id']},
                            {"$set": {
                                'favorites': current_favorites,
                            }},
                            upsert=True)

        updated_user = DB_USERS.find_one({'username': username})
        updated_favorites = updated_user['favorites']

    return redirect(
        url_for('recipe', recipe_id=recipe_id, favorites=updated_favorites))
Exemplo n.º 2
0
def update_profile(user_id):
    """ Updates the users db record with the POST form data, also updates the cloadinary image data

   :return
       Redirect to profile.html

    """
    # REFERENCE CREDITS:
    # Cloudinary api ->
    # https://github.com/tiagocordeiro/flask-cloudinary

    file_to_upload = request.files.get('file')

    if file_to_upload:
        # Current user record
        current_user = DB_USERS.find_one({'_id': ObjectId(user_id)})

        if current_user['profile_image_id'] != 'xmt2q3ttok9cwjlux8j2':
            # Remove current profile image
            destroy(current_user['profile_image_id'], invalidate=True)

        # Upload new image to cloudinary
        upload_result = upload(file_to_upload)
        # Update users profile image in DB
        DB_USERS.update_one({'_id': ObjectId(user_id)}, {
            "$set": {
                'profile_image': upload_result['secure_url'],
                'profile_image_id': upload_result['public_id']
            }
        },
                            upsert=True)

    #  Update users profile data
    DB_USERS.update_one({'_id': ObjectId(user_id)}, {
        "$set": {
            'bio': request.form.get('bio'),
            'email': request.form.get('email')
        }
    },
                        upsert=True)

    return redirect(url_for('profile'))
Exemplo n.º 3
0
def remove_favorite(user_id, recipe_id):
    """ Removes the recipe from the users favorites list in the users profile
     :return
         Redirect to profile view with updated favorites list
    """

    user = None
    current_favorites = None

    try:
        user = DB_USERS.find_one({'_id': ObjectId(user_id)})
        current_favorites = user['favorites']
        current_favorites.remove(ObjectId(recipe_id))
    except:
        # raises a 404 error if any of these fail
        abort(404, description="Resource not found")

    DB_USERS.update_one({'_id': user['_id']},
                        {"$set": {
                            'favorites': current_favorites,
                        }},
                        upsert=True)

    return redirect(url_for('profile'))