def like_recipe(recipe_id):
    '''
    Allows the user to like a particular recipe
    The recipe_id is added to user's liked_recipes list in users_coll
    The 'likes' field in the recipes collection is incremented by 1
    The 'views' field in the recipes collection is decremented by 1
    '''

    # Add the liked recipe ID to the users collection for that user
    user = session['user'].lower()
    users_coll.find_one_and_update(
        {"username": user}, {"$push": {
            "liked_recipes": ObjectId(recipe_id)
        }})

    # Increment 'likes' field in recipes_coll by 1 for each like
    recipes_coll.update_one({"_id": ObjectId(recipe_id)},
                            {"$inc": {
                                "likes": 1,
                                "views": -1
                            }})

    # Flash message confirmation that the user successfully liked the recipe
    flash(
        Markup("Thanks \
                <span class='message-helper bold italic'>" +
               user.capitalize() + "</span>, \
                this recipe has been added to your 'Liked' list!"))

    return redirect(request.referrer)
def rate(recipe_id):
    '''
    Allows the user to rate the recipe
    Pushes rating value into rating_values list in recipes_coll
    '''
    username = session["user"]
    # Push rating from form into the rating_values field in the relevant record
    recipes_coll.update_one(
        {"_id": ObjectId(recipe_id)},
        {"$push": {
            "rating_values": int(request.form.get("rating"))
        }})

    # Decrement number of views by 1
    # Resolves bug of it being incremented when redirected to recipes.html
    recipes_coll.update_one({"_id": ObjectId(recipe_id)},
                            {"$inc": {
                                "views": -1
                            }})

    # Flash message confirmation that recipe has been successfully added
    flash(
        Markup("Thanks for rating this recipe \
                <span class='message-helper bold italic'>" +
               username.capitalize() + "</span>!"))

    return redirect(url_for('recipes.recipe', recipe_id=recipe_id))
def remove_recipe(recipe_id):
    '''
    Changes 'deleted' value in recipes_coll to 'True'
    Ensures that recipe remains in the DB but is removed from the front end
    '''
    # Change the deleted field to 'True'
    recipes_coll.update_one({"_id": ObjectId(recipe_id)},
                            {"$set": {
                                "deleted": True
                            }})

    # Get session user details
    user = session['user'].lower()
    # Remove the recipe ID from that particular users' added_recipes list
    users_coll.find_one_and_update(
        {"username": user}, {"$pull": {
            "added_recipes": ObjectId(recipe_id)
        }})
    # Remove the recipe ID from all users' liked_recipes list
    users_coll.update_many({},
                           {"$pull": {
                               "liked_recipes": ObjectId(recipe_id)
                           }})

    # Flash message confirmation that recipe has been successfully added
    flash(
        Markup("Thanks \
                <span class='message-helper bold italic'>" +
               user.capitalize() + "</span>, \
                 this recipe has been successfully deleted!"))

    return redirect(url_for('recipes.get_recipes', page=1,
                            recipe_id=recipe_id))
def recipe(recipe_id):
    '''
    Gets details for particular recipe_id that the user clicked on browse.html
    Routes the user to the recipes.html page with details for that recipe_id
    Each time the recipe is viewed, it increments the 'views' field by one
    '''
    recipe = find_recipe(recipe_id)

    try:
        # Get the user's liked_recipes list if a user is logged in
        user = session["user"].lower()
        liked_recipes = get_username(user)["liked_recipes"]
    except:
        # Create an empty liked_recipes list if no user is logged in
        liked_recipes = []

    # Increment 'views' field by 1 each time the recipe is viewed
    recipes_coll.update_one({"_id": ObjectId(recipe_id)},
                            {"$inc": {
                                "views": 1
                            }})

    # Convert added_by ObjectId to username value - verify against session user
    added_by = users_coll.find_one({"_id": ObjectId(recipe.get("added_by"))
                                    })["username"]

    return render_template("recipes.html",
                           recipe=recipe,
                           ratings=rating_list,
                           added_by=added_by,
                           liked_recipes=liked_recipes)
示例#5
0
def delete_account(username):
    '''
    Allows the user to delete their account
    Users will have to enter their correct existing password first
    Deleting the account will delete the user's recipes
    'Deleted' value will be updated to true and they won't be displayed
    The recipes will also be removed from all users' liked lists
    The user's details will be removed from the userLogin collection
    The session will end
    '''
    username = get_username(session["user"])
    confirm_password = request.form.get("confirm_password")
    user_upper = session["user"].capitalize()

    # If stored password matches the entry, the password will be changed
    if check_password_hash(username["password"], confirm_password):
        # Find the user's added recipes
        user_added_recipes = [
            recipe for recipe in username.get("added_recipes")
        ]
        # Update 'deleted' value to true for each recipe from recipes_coll
        # Remove each recipe from all other users' liked list
        for recipe in user_added_recipes:
            recipes_coll.update_one({"_id": ObjectId(recipe)},
                                    {"$set": {
                                        "deleted": True
                                    }})
            users_coll.update_many(
                {}, {"$pull": {
                    "liked_recipes": ObjectId(recipe)
                }})
        # Find the user's liked recipes
        user_liked_recipes = [
            recipe for recipe in username.get("liked_recipes")
        ]
        # Decrement the likes field in each of those recipes by 1
        for recipe in user_liked_recipes:
            recipes_coll.update_one({"_id": ObjectId(recipe)},
                                    {"$inc": {
                                        "likes": -1
                                    }})
        flash(Markup("Your account has been successfully deleted."))
        # End the user's session
        session.pop('user', None)
        # Remove the user's details from the userLogin collection
        users_coll.remove({"_id": username.get("_id")})
        # Redirect to index.html
        return redirect(url_for('main.index'))
    # If stored password doesn't match the entry, display generic flash message
    else:
        flash(
            Markup("Sorry \
                    <span class='message-helper bold italic'>" + user_upper +
                   "</span>, \
                     your existing password doesn't match what we have. \
                     Please try again."))

    return redirect(url_for('users.profile', username=username))
def unlike_recipe(recipe_id):
    '''
    Allows the user to unlike a particular recipe
    The recipe_id is removed from user's liked_recipes list in the users_coll
    The 'likes' field in the recipes collection is decremented by 1
    The 'views' field in recipes_collis decremented by 1,
    only if the recipe is unliked from recipe.html
    '''

    # Remove the liked recipe ID to the users collection for that user
    user = session['user'].lower()
    users_coll.find_one_and_update(
        {"username": user}, {"$pull": {
            "liked_recipes": ObjectId(recipe_id)
        }})

    # Decrement 'likes' field in recipes_coll by 1 each time recipe is unliked
    recipes_coll.update_one({"_id": ObjectId(recipe_id)},
                            {"$inc": {
                                "likes": -1
                            }})

    # Only decrement views if the user unlikes a recipe on the recipe.html page
    # Resolves bug where views were decremented when
    # the user unlikes a recipe in profile.html
    if request.path == 'recipe':
        recipes_coll.update_one({"_id": ObjectId(recipe_id)},
                                {"$inc": {
                                    "views": -1
                                }})

    # Flash message confirmation that the user successfully liked the recipe
    flash(
        Markup("Thanks \
                <span class='message-helper bold italic'>" +
               user.capitalize() + "</span>, \
                 this recipe has been removed from your 'Liked' list!"))

    return redirect(request.referrer)