Ejemplo n.º 1
0
def desserts_delete_favorite(recipe_id, slugUrl):
    """
    Remove recipe from user favorites, decrease number of favorites
    and number of views by -1. Flash message advising user.
    """
    users_collection.find_one_and_update(
        {"username_lower": session["user"].lower()},
        {"$pull": {"user_favs": ObjectId(recipe_id)}})
    recipes_collection.update_one(
        {"_id": ObjectId(recipe_id)},
        {"$inc": {"user_favs": -1, "views": -1}})
    flash(Markup(
        f"<i class='fas fa-minus-circle red-text'></i>\
        Removed from your favorites."))
    return redirect(request.referrer)
Ejemplo n.º 2
0
def desserts_add_favorite(recipe_id, slugUrl):
    """
    Add recipe to user favorites, increase number of favorites by +1,
    and decrease number of views by -1. Flash message advising user.
    """
    users_collection.find_one_and_update(
        {"username_lower": session["user"].lower()},
        {"$push": {"user_favs": ObjectId(recipe_id)}})
    recipes_collection.update_one(
        {"_id": ObjectId(recipe_id)},
        {"$inc": {"user_favs": 1, "views": -1}})
    flash(Markup(
        f"<i class='fas fa-heart pink-text'></i>\
        Saved to your favorites!"))
    return redirect(request.referrer)
Ejemplo n.º 3
0
def desserts_delete(recipe_id, slugUrl):
    """
    Delete recipe from database.

    Remove the recipe from the collection, pull the recipe from the
    user's recipe list, and pull the recipe from all other users' favorites.
    """
    if "user" in session:  # users must be logged in
        recipe = get_recipe(recipe_id)
        session_user = get_user_lower(session["user"])["username"]
        recipe_author = users_collection.find_one(
            {"_id": ObjectId(recipe.get("author"))})["username"]
        # check that someone isn't brute-forcing the url to delete recipes
        if session_user == recipe_author or session_user == "Admin":
            author = users_collection.find_one_and_update(
                {"_id": ObjectId(recipe.get("author"))},
                {"$pull": {"user_recipes": ObjectId(recipe_id)}})
            users_collection.update_many(
                {}, {"$pull": {"user_favs": ObjectId(recipe_id)}})
            recipes_collection.remove({"_id": ObjectId(recipe_id)})
            flash(Markup(
                f"<i class='fas fa-trash-alt red-text'></i>\
                Your recipe has been deleted."))
        else:
            flash(Markup(
                f"<i class='far fa-sad-tear yellow-text'></i>\
                You are not authorized to delete this recipe!"))
    else:  # no user in session
        flash(Markup(
            f"<i class='far fa-sad-tear yellow-text'></i>\
            You must be logged in to delete this recipe."))
    return redirect(url_for("recipes.desserts"))
Ejemplo n.º 4
0
def desserts_delete(recipe_id):
    """
    Delete recipe from database.

    Remove the recipe from the collection, pull the recipe from the
    user's recipe list, and pull the recipe from all other users' favorites.
    """
    recipes_collection.remove({"_id": ObjectId(recipe_id)})
    users_collection.find_one_and_update(
        {"username_lower": session["user"].lower()},
        {"$pull": {
            "user_recipes": ObjectId(recipe_id)
        }})
    users_collection.update_many({},
                                 {"$pull": {
                                     "user_favs": ObjectId(recipe_id)
                                 }})
    flash(
        Markup(f"<i class='fas fa-trash-alt red-text'></i>\
        Your recipe has been deleted."))
    return redirect(url_for("recipes.desserts"))