示例#1
0
def profile_change_password(username):
    """
    Update user hashed password.

    Each user has the option to update their password.
    User must first verify their current password though.
    """
    user = get_user_lower(session["user"])
    # check if stored hashed password matches current password in form
    if check_password_hash(user["user_password"],
                           request.form.get("current_password")):
        flash(
            Markup(f"<i class='far fa-check-circle green-text'></i>\
            Your password has been updated successfully!"))
        users_collection.update_one(
            {"username_lower": session["user"].lower()}, {
                "$set": {
                    "user_password":
                    generate_password_hash(request.form.get("new_password"))
                }
            })
    else:
        # password does not match
        flash(
            Markup(f"<i class='fas fa-exclamation-circle red-text'></i>\
            Whoops! Looks like your <span class='pink-text text-lighten-2'>\
            password</span> is incorrect. Please try again."))
    # redirect back to profile
    return redirect(url_for("users.profile", username=username))
示例#2
0
def desserts_edit(recipe_id, slugUrl):
    """
    Update recipe in database.

    Inject all existing data from the recipe back into the form.
    """
    if request.method == "GET":
        recipe = get_recipe(recipe_id)
        # generate dropdown lists from helper functions
        allergen_list = dropdown_allergens()
        dessert_list = dropdown_dessert_type()
        measurement_list = dropdown_measurement()
        # generate ingredient list items
        amount_list = [amount for amount in recipe.get("ingredient_amount")]
        unit_list = [unit for unit in recipe.get("ingredient_measurement")]
        ingredient_list = (
            [ingredient for ingredient in recipe.get("ingredient_name")])
        # zip the new lists into a single master list
        ingredients_list = zip(amount_list, unit_list, ingredient_list)
        return render_template(
            "desserts_edit.html",
            recipe=recipe,
            allergens=allergen_list,
            desserts=dessert_list,
            measurements=measurement_list,
            ingredients=ingredients_list,
            recipe_id=recipe_id,
            slugUrl=slugUrl
        )

    """ Push the edits of the recipe to the collection on submit. """
    if request.method == "POST":
        recipe = get_recipe(recipe_id)
        # get today's date and date recipe was last edited
        today = datetime.now().strftime("%d %B, %Y")
        last_edit = int(datetime.now().strftime("%Y%m%d"))
        # get non-editable values
        get_author = recipe.get("author")
        get_date_added = recipe.get("date_added")
        get_views = recipe.get("views")
        get_user_favs = recipe.get("user_favs")
        # get and convert total time
        hours = int(
            request.form.get("total_hrs")) * 60 if request.form.get(
            "total_hrs") else ""
        total_time = int(
            request.form.get("total_mins")) + hours if hours else int(
            request.form.get("total_mins"))
        # slugify url to be user-friendly
        slugUrl = slugify(request.form.get("recipe_name"))
        # push form data to recipe on submit
        submit = {
            "recipe_name": request.form.get("recipe_name"),
            "recipe_slug": slugUrl,
            "description": request.form.get("description"),
            "dessert_type": request.form.get("dessert_type"),
            "ingredient_amount": request.form.getlist("ingredient_amount"),
            "ingredient_measurement": request.form.getlist(
                "ingredient_measurement"),
            "ingredient_name": request.form.getlist("ingredient_name"),
            "directions": request.form.getlist("directions"),
            "total_hrs": request.form.get("total_hrs"),
            "total_mins": request.form.get("total_mins"),
            "total_time": total_time,
            "allergens": request.form.getlist("allergens"),
            "img_src": request.form.get("img_src"),
            "author": get_author,
            "date_added": get_date_added,
            "date_updated": today,
            "last_edit": last_edit,
            "views": get_views,
            "user_favs": get_user_favs
        }
        recipes_collection.update({"_id": ObjectId(recipe_id)}, submit)
        flash(Markup(
            f"<i class='far fa-check-circle green-text'></i>\
            Your recipe has been updated successfully!"))

        # send me an email when a recipe gets updated (personal backup)
        # msg = MIMEMultipart()
        # msg["From"] = MY_ADDRESS
        # msg["To"] = SEND_TO
        # msg["Subject"] = "2BN-Desserts | Recipe Updated: " + slugUrl
        # formatEmail = "<br><br>".join(["%s: %s" % kv for kv in submit.items()])
        # msg.attach(MIMEText(str(formatEmail), "html"))  # must convert to str()
        # smtpserver = smtplib.SMTP("smtp.gmail.com", 587)  # access server
        # smtpserver.ehlo()  # identify ourselves to smtp gmail client
        # smtpserver.starttls()  # secure our email with tls encryption
        # smtpserver.ehlo()  # re-identify ourselves as an encrypted connection
        # smtpserver.login(MY_ADDRESS, PASSWORD)  # login to the server
        # smtpserver.send_message(msg)  # send the message
        # smtpserver.quit()  # quit the server

        # add recipe to admin's profile as back-up (in lieu of email)
        users_collection.update_one(
            {"username": "******"},
            {"$push": {"edited_recipes": submit}})

        return redirect(url_for(
            "recipes.desserts_recipe",
            recipe_id=recipe_id,
            slugUrl=slugUrl))
示例#3
0
def desserts_new():
    """
    Create recipe for database.

    Inject all form data to new recipe document on submit.
    """
    if request.method == "GET":
        allergen_list = dropdown_allergens()
        dessert_list = dropdown_dessert_type()
        measurement_list = dropdown_measurement()
        return render_template(
            "desserts_new.html",
            allergens=allergen_list,
            desserts=dessert_list,
            measurements=measurement_list)

    if request.method == "POST":
        # get today's date and date recipe was last edited
        today = datetime.now().strftime("%d %B, %Y")
        last_edit = int(datetime.now().strftime("%Y%m%d"))
        # get user / author details
        session_user = get_user_lower(session["user"])["username"]
        author = users_collection.find_one({"username": session_user})["_id"]
        # get and convert total time
        hours = int(request.form.get(
            "total_hrs")) * 60 if request.form.get(
                "total_hrs") else ""
        total_time = int(request.form.get(
            "total_mins")) + hours if hours else int(request.form.get(
                "total_mins"))
        # slugify url to be user-friendly
        slugUrl = slugify(request.form.get("recipe_name"))
        # get form data prior to submitting
        submit = {
            "recipe_name": request.form.get("recipe_name"),
            "recipe_slug": slugUrl,
            "description": request.form.get("description"),
            "dessert_type": request.form.get("dessert_type"),
            "ingredient_amount": request.form.getlist("ingredient_amount"),
            "ingredient_measurement": request.form.getlist(
                "ingredient_measurement"),
            "ingredient_name": request.form.getlist("ingredient_name"),
            "directions": request.form.getlist("directions"),
            "total_hrs": request.form.get("total_hrs"),
            "total_mins": request.form.get("total_mins"),
            "total_time": total_time,
            "allergens": request.form.getlist("allergens"),
            "img_src": request.form.get("img_src"),
            "author": author,
            "date_added": today,
            "date_updated": today,
            "last_edit": last_edit,
            "views": 0,
            "user_favs": 0
        }
        # get the new _id being created on submit
        newID = recipes_collection.insert_one(submit)
        # add recipe _id to user's recipe list
        users_collection.update_one(
            {"_id": ObjectId(author)},
            {"$push": {"user_recipes": newID.inserted_id}})
        flash(Markup(
            f"<i class='far fa-check-circle green-text'></i>\
                Sounds delicious! Thanks for adding this recipe!"))
        # if selected, add recipe to user-favs as well
        if request.form.get("add_favs"):
            users_collection.update_one(
                {"_id": ObjectId(author)},
                {"$push": {"user_favs": newID.inserted_id}})
            # increase number of favorites on this recipe by +1
            recipes_collection.update_one(
                {"_id": newID.inserted_id},
                {"$inc": {"user_favs": 1}})

        # # send me an email when a recipe gets added (personal backup)
        # msg = MIMEMultipart()
        # msg["From"] = MY_ADDRESS
        # msg["To"] = SEND_TO
        # msg["Subject"] = "2BN-Desserts | New Recipe Added: " + slugUrl
        # formatEmail = "<br><br>".join(["%s: %s" % kv for kv in submit.items()])
        # msg.attach(MIMEText(str(formatEmail), "html"))  # must convert to str()
        # smtpserver = smtplib.SMTP("smtp.gmail.com", 587)  # access server
        # smtpserver.ehlo()  # identify ourselves to smtp gmail client
        # smtpserver.starttls()  # secure our email with tls encryption
        # smtpserver.ehlo()  # re-identify ourselves as an encrypted connection
        # smtpserver.login(MY_ADDRESS, PASSWORD)  # login to the server
        # smtpserver.send_message(msg)  # send the message
        # smtpserver.quit()  # quit the server

        # add recipe to admin's profile as back-up (in lieu of email)
        users_collection.update_one(
            {"username": "******"},
            {"$push": {"new_recipes": submit}})

        return redirect(url_for(
            "recipes.desserts_recipe",
            recipe_id=newID.inserted_id,
            slugUrl=slugUrl))
示例#4
0
def desserts_new():
    """
    Create recipe for database.

    Inject all form data to new recipe document on submit.
    """
    if request.method == "GET":
        allergen_list = dropdown_allergens()
        dessert_list = dropdown_dessert_type()
        measurement_list = dropdown_measurement()
        return render_template("desserts_new.html",
                               allergens=allergen_list,
                               desserts=dessert_list,
                               measurements=measurement_list)

    if request.method == "POST":
        # get today's date and date recipe was last edited
        today = datetime.now().strftime("%d %B, %Y")
        last_edit = int(datetime.now().strftime("%Y%m%d"))
        # get user / author details
        session_user = get_user_lower(session["user"])["username"]
        author = users_collection.find_one({"username": session_user})["_id"]
        # get and convert total time
        hours = int(request.form.get("total_hrs")) * 60 if request.form.get(
            "total_hrs") else ""
        total_time = int(
            request.form.get("total_mins")) + hours if hours else int(
                request.form.get("total_mins"))
        # slugify url to be user-friendly
        slugUrl = slugify(request.form.get("recipe_name"))
        # get form data prior to submitting
        submit = {
            "recipe_name":
            request.form.get("recipe_name"),
            "recipe_slug":
            slugUrl,
            "description":
            request.form.get("description"),
            "dessert_type":
            request.form.get("dessert_type"),
            "ingredient_amount":
            request.form.getlist("ingredient_amount"),
            "ingredient_measurement":
            request.form.getlist("ingredient_measurement"),
            "ingredient_name":
            request.form.getlist("ingredient_name"),
            "directions":
            request.form.getlist("directions"),
            "total_hrs":
            request.form.get("total_hrs"),
            "total_mins":
            request.form.get("total_mins"),
            "total_time":
            total_time,
            "allergens":
            request.form.getlist("allergens"),
            "img_src":
            request.form.get("img_src"),
            "author":
            author,
            "date_added":
            today,
            "date_updated":
            today,
            "last_edit":
            last_edit,
            "views":
            0,
            "user_favs":
            0
        }
        # get the new _id being created on submit
        newID = recipes_collection.insert_one(submit)
        # add recipe _id to user's recipe list
        users_collection.update_one(
            {"_id": ObjectId(author)},
            {"$push": {
                "user_recipes": newID.inserted_id
            }})
        flash(
            Markup(f"<i class='far fa-check-circle green-text'></i>\
                Sounds delicious! Thanks for adding this recipe!"))
        # if selected, add recipe to user-favs as well
        if request.form.get("add_favs"):
            users_collection.update_one(
                {"_id": ObjectId(author)},
                {"$push": {
                    "user_favs": newID.inserted_id
                }})
            # increase number of favorites on this recipe by +1
            recipes_collection.update_one({"_id": newID.inserted_id},
                                          {"$inc": {
                                              "user_favs": 1
                                          }})
        return redirect(
            url_for("recipes.desserts_recipe",
                    recipe_id=newID.inserted_id,
                    slugUrl=slugUrl))