diff --git a/.idea/PortfolioProject.iml b/.idea/PortfolioProject.iml index 9a323b0..26811fb 100644 --- a/.idea/PortfolioProject.iml +++ b/.idea/PortfolioProject.iml @@ -4,7 +4,7 @@ - + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index dc9ea49..6bb94de 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,4 @@ - + \ No newline at end of file diff --git a/mail.txt b/mail.txt new file mode 100644 index 0000000..1ad6a61 --- /dev/null +++ b/mail.txt @@ -0,0 +1,3 @@ +$to == jenseluc@oregonstate.edu +$subject == whole wheat loaf +$message == whole wheat loaf; Num loaves: 1; AP Flour: 100 grams; WW Flour: 400 grams; Rye Flour: 0 grams; Water: 300 grams; Salt: 20 grams; Yeast: 2 grams; Starter: 10 grams; \ No newline at end of file diff --git a/mailer.py b/mailer.py new file mode 100644 index 0000000..35270a9 --- /dev/null +++ b/mailer.py @@ -0,0 +1,87 @@ +from mailjet_rest import Client + +api_key = '9aa5c72ce9c248d0570aa1e6cabdc9ab' +api_secret = '9bed4b34d09b8e19017768cc8264479e' +mailjet = Client(auth=(api_key, api_secret), version='v3.1') + +def main(): + print("Sending ======>") + # read maile.txt + with open('mail.txt', 'rt') as infile: + text = infile.read() + + # when text start with '$' it is the key and the rest is the value + # split the text into a list of lines + lines = text.split('\n') + # loop through the lines + theMail = {} + to = "" + subject = "" + body = "" + + i = 0 + for line in lines: + # if the line starts with '$' + + if line.startswith('$'): + # split the line into key and value + key, value = line.split(' ', 1) + + # if the key is 'email' + if key == '$to': + to = value.replace("==", "").strip() + elif key == '$subject': + + subject = value.replace("==", "").strip() + elif key == '$message': + body = value.replace("==", "").strip() + elif line.startswith('#'): + #reset variables + to = "" + subject = "" + body = "" + + if to != "" and subject != "" and body != "": + + #add them to the dictionary + theMail[i] = {'to': to, 'subject': subject, 'body': body} + i += 1 + # reset the variables + to = "" + subject = "" + body = "" + # if #end is found, reset the variables + # loop through the dictionary + for key, value in theMail.items(): + # send the email + send_email(value['to'], value['subject'], value['body']) + # print(value['to']) + # print(value['subject']) + # print(value['body']) + # print(theMail) + +def send_email(email, subject, body): + data = { + 'Messages': [ + { + "From": { + "Email": "khansom@oregonstate.edu", + "Name": "Soman Khan" + }, + "To": [ + { + "Email": email, + } + ], + "Subject": subject, + "TextPart": "Greetings!", + "HTMLPart": body, + } + ] + } + result = mailjet.send.create(data=data) + print(result.status_code) + print(result.json()) +# call main +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/main.py b/main.py index db690e5..90cb09a 100644 --- a/main.py +++ b/main.py @@ -5,11 +5,15 @@ Last updated 5/12 for Assignment 1 """ from flask import Flask, redirect, render_template, request, send_file from recipe import Recipe, RecipeBook +from werkzeug.utils import secure_filename from time import sleep +import subprocess +import os app = Flask(__name__) book = RecipeBook() + @app.route("/") def home(): return render_template("index.html") @@ -24,23 +28,79 @@ def recipes_page(): return render_template("recipes.html", content=recipes) -@app.route('/recipes/<_id>', methods=['GET', 'POST']) -def recipe_page(_id): +@app.route('/recipes/<_id>/email', methods=['GET', 'POST']) +def email_page(_id): recipe = book.find_by_id(str(_id)) if request.method == 'POST': + addr = request.form.get('email') + subject = recipe.get_name() + message = f"{subject}; " \ + f"Num loaves: {recipe.get_num_loaves()};" + ingredients = recipe.get_ingredients() + for item in ingredients: + message += f" {item}: {ingredients[item]} grams;" + + body = f"$to == {addr}\n" \ + f"$subject == {subject}\n" \ + f"$message == {message}" + + with open('mail.txt', 'wt') as txt_file: + txt_file.write(body) + + subprocess.call(['python', 'mailer.py']) + + return redirect('/recipes') + + return render_template('email.html', content=recipe) + + +@app.route('/recipes/<_id>', methods=['GET', 'POST']) +def recipe_page(_id): + recipe = book.find_by_id(_id) + + if request.method == 'POST': + # user wants to scale their recipe if 'scale' in request.form: scale = request.form.get('scale') new = recipe.scale(scale) new_id = book.add_recipe(new) return redirect(f'/recipes/{new_id}') + elif 'photo' in request.files: + # user wants to add a photo + f = request.files['photo'] + f.save(os.path.join('static', f"image_{_id}.jpg")) + # return "Success" else: + # user wants to download a pdf path = write_txt(_id) sleep(0.5) return send_file(path, as_attachment=True) - return render_template("recipe.html", content=recipe, _id=_id) + # find all associated images + recipe_photos = [] + all_photos = os.listdir('static') + for photo in all_photos: + if 'jpg' in photo: + if get_num(photo) == int(_id): + recipe_photos.append(photo) + + return render_template("recipe.html", content=recipe, _id=_id, photos=recipe_photos) + + +def get_num(path: str) -> int: + """ + :param path: must be formatted: "image_XXX.jpg" where XXX is the id of the recipe with any number of digits + :return: integer of the found id number + """ + num = "" + for i in range(6, len(path)): + if path[i] == '.': + break + num += path[i] + + return int(num) def write_txt(_id): @@ -55,10 +115,6 @@ def write_txt(_id): return f"recipe{_id}.pdf" -# app.route('/recipes//download') -# def - - @app.route('/recipes/<_id>/delete') def delete_recipe(_id): book.find_and_delete(_id) @@ -80,4 +136,4 @@ def add_recipe(): if __name__ == "__main__": - app.run(debug=False) + app.run(debug=True) diff --git a/recipe.txt b/recipe.txt index 78df172..c227083 100644 --- a/recipe.txt +++ b/recipe.txt @@ -1 +1 @@ -recipe1.pdf \ No newline at end of file +0 \ No newline at end of file diff --git a/recipes.json b/recipes.json index 129889a..d8c65fe 100644 --- a/recipes.json +++ b/recipes.json @@ -12,12 +12,25 @@ "Starter": 0 } }, - "1": { - "quantity": "2", - "name": "Sourdough * 1.0", + "2": { + "quantity": "1", + "name": "Sandwich Loaf", "ingredients": { "AP Flour": 500, - "WW Flour": 500, + "WW Flour": 0, + "Rye Flour": 0, + "Water": 250, + "Salt": 20, + "Yeast": 10, + "Starter": 0 + } + }, + "1": { + "quantity": "1", + "name": "tobi", + "ingredients": { + "AP Flour": 100, + "WW Flour": 0, "Rye Flour": 0, "Water": 0, "Salt": 0, diff --git a/templates/email.html b/templates/email.html new file mode 100644 index 0000000..f198009 --- /dev/null +++ b/templates/email.html @@ -0,0 +1,18 @@ +{% extends "base.html" %} +{% block title %}{{content.get_name()}}{% endblock %} + +{% block html_head %} + +{% endblock %} + +{% block content %} + +

{{ content.get_name() }}

+ +
+ + + +
+ +{% endblock %} \ No newline at end of file diff --git a/templates/recipe.html b/templates/recipe.html index ff28159..c9662f7 100644 --- a/templates/recipe.html +++ b/templates/recipe.html @@ -9,7 +9,6 @@

{{ content.get_name() }} Recipe

-

Number of loaves: {{ content.get_num_loaves() }}

@@ -32,10 +31,27 @@ - - + + + + + + + + + +Email me! + +
+ +{% for photo in photos %} + +bread photo +{% endfor %} +
+ {% endblock %} \ No newline at end of file