prep for deployment on Pi
This commit is contained in:
66
main.py
66
main.py
@@ -1,7 +1,7 @@
|
|||||||
"""
|
"""
|
||||||
Portfolio Project for CS361 Spring 2022
|
Portfolio Project for CS361 Spring 2022
|
||||||
Written by Lucas Jensen
|
Written by Lucas Jensen
|
||||||
Last updated 5/12 for Assignment 1
|
Last updated 10-12-2023 for deployment
|
||||||
"""
|
"""
|
||||||
from flask import Flask, redirect, render_template, request, send_file
|
from flask import Flask, redirect, render_template, request, send_file
|
||||||
from recipe import Recipe, RecipeBook
|
from recipe import Recipe, RecipeBook
|
||||||
@@ -18,7 +18,7 @@ def home():
|
|||||||
return render_template("index.html")
|
return render_template("index.html")
|
||||||
|
|
||||||
|
|
||||||
@app.route('/recipes', methods=['GET'])
|
@app.route("/recipes", methods=["GET"])
|
||||||
def recipes_page():
|
def recipes_page():
|
||||||
recipes = book.get_recipes()
|
recipes = book.get_recipes()
|
||||||
# recipes is a dictionary of recipe objects
|
# recipes is a dictionary of recipe objects
|
||||||
@@ -27,54 +27,51 @@ def recipes_page():
|
|||||||
return render_template("recipes.html", content=recipes)
|
return render_template("recipes.html", content=recipes)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/recipes/<_id>/email', methods=['GET', 'POST'])
|
@app.route("/recipes/<_id>/email", methods=["GET", "POST"])
|
||||||
def email_page(_id):
|
def email_page(_id):
|
||||||
recipe = book.find_by_id(str(_id))
|
recipe = book.find_by_id(str(_id))
|
||||||
|
|
||||||
if request.method == 'POST':
|
if request.method == "POST":
|
||||||
addr = request.form.get('email')
|
addr = request.form.get("email")
|
||||||
subject = recipe.get_name()
|
subject = recipe.get_name()
|
||||||
message = f"{subject}; " \
|
message = f"{subject}; " f"Num loaves: {recipe.get_num_loaves()};"
|
||||||
f"Num loaves: {recipe.get_num_loaves()};"
|
|
||||||
ingredients = recipe.get_ingredients()
|
ingredients = recipe.get_ingredients()
|
||||||
for item in ingredients:
|
for item in ingredients:
|
||||||
message += f" {item}: {ingredients[item]} grams;"
|
message += f" {item}: {ingredients[item]} grams;"
|
||||||
|
|
||||||
body = f"$to == {addr}\n" \
|
body = f"$to == {addr}\n" f"$subject == {subject}\n" f"$message == {message}"
|
||||||
f"$subject == {subject}\n" \
|
|
||||||
f"$message == {message}"
|
|
||||||
|
|
||||||
with open('mail.txt', 'wt') as txt_file:
|
with open("mail.txt", "wt") as txt_file:
|
||||||
txt_file.write(body)
|
txt_file.write(body)
|
||||||
|
|
||||||
subprocess.call(['python', 'mailer.py'])
|
subprocess.call(["python", "mailer.py"])
|
||||||
|
|
||||||
return redirect('/recipes')
|
return redirect("/recipes")
|
||||||
|
|
||||||
return render_template('email.html', content=recipe)
|
return render_template("email.html", content=recipe)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/recipes/<_id>', methods=['GET', 'POST'])
|
@app.route("/recipes/<_id>", methods=["GET", "POST"])
|
||||||
def recipe_page(_id):
|
def recipe_page(_id):
|
||||||
recipe = book.find_by_id(_id)
|
recipe = book.find_by_id(_id)
|
||||||
|
|
||||||
if request.method == 'POST':
|
if request.method == "POST":
|
||||||
# user wants to scale their recipe
|
# user wants to scale their recipe
|
||||||
if 'scale' in request.form:
|
if "scale" in request.form:
|
||||||
scale = request.form.get('scale')
|
scale = request.form.get("scale")
|
||||||
new = recipe.scale(scale)
|
new = recipe.scale(scale)
|
||||||
new_id = book.add_recipe(new)
|
new_id = book.add_recipe(new)
|
||||||
return redirect(f'/recipes/{new_id}')
|
return redirect(f"/recipes/{new_id}")
|
||||||
elif 'photo' in request.files:
|
elif "photo" in request.files:
|
||||||
# user wants to add a photo
|
# user wants to add a photo
|
||||||
f = request.files['photo']
|
f = request.files["photo"]
|
||||||
f.save(os.path.join('static', f"image_{_id}.jpg"))
|
f.save(os.path.join("static", f"image_{_id}.jpg"))
|
||||||
# return "Success"
|
# return "Success"
|
||||||
elif 'convert' in request.form:
|
elif "convert" in request.form:
|
||||||
# user wants to convert to sourdough
|
# user wants to convert to sourdough
|
||||||
new_recipe = recipe.convert()
|
new_recipe = recipe.convert()
|
||||||
book.add_recipe(new_recipe)
|
book.add_recipe(new_recipe)
|
||||||
return redirect('/recipes')
|
return redirect("/recipes")
|
||||||
else:
|
else:
|
||||||
# user wants to download a pdf
|
# user wants to download a pdf
|
||||||
path = write_txt(_id)
|
path = write_txt(_id)
|
||||||
@@ -83,16 +80,13 @@ def recipe_page(_id):
|
|||||||
|
|
||||||
# find all associated images
|
# find all associated images
|
||||||
recipe_photos = []
|
recipe_photos = []
|
||||||
all_photos = os.listdir('static')
|
all_photos = os.listdir("static")
|
||||||
for photo in all_photos:
|
for photo in all_photos:
|
||||||
if 'jpg' in photo:
|
if "jpg" in photo:
|
||||||
if get_num(photo) == int(_id):
|
if get_num(photo) == int(_id):
|
||||||
recipe_photos.append(photo)
|
recipe_photos.append(photo)
|
||||||
|
|
||||||
return render_template("recipe.html",
|
return render_template("recipe.html", content=recipe, _id=_id, photos=recipe_photos)
|
||||||
content=recipe,
|
|
||||||
_id=_id,
|
|
||||||
photos=recipe_photos)
|
|
||||||
|
|
||||||
|
|
||||||
def get_num(path: str) -> int:
|
def get_num(path: str) -> int:
|
||||||
@@ -103,7 +97,7 @@ def get_num(path: str) -> int:
|
|||||||
"""
|
"""
|
||||||
num = ""
|
num = ""
|
||||||
for i in range(6, len(path)):
|
for i in range(6, len(path)):
|
||||||
if path[i] == '.':
|
if path[i] == ".":
|
||||||
break
|
break
|
||||||
num += path[i]
|
num += path[i]
|
||||||
|
|
||||||
@@ -116,24 +110,24 @@ def write_txt(_id):
|
|||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
print(_id)
|
print(_id)
|
||||||
with open('recipe.txt', 'w') as txt_file:
|
with open("recipe.txt", "w") as txt_file:
|
||||||
txt_file.write(_id)
|
txt_file.write(_id)
|
||||||
|
|
||||||
return f"recipe{_id}.pdf"
|
return f"recipe{_id}.pdf"
|
||||||
|
|
||||||
|
|
||||||
@app.route('/recipes/<_id>/delete')
|
@app.route("/recipes/<_id>/delete")
|
||||||
def delete_recipe(_id):
|
def delete_recipe(_id):
|
||||||
book.find_and_delete(_id)
|
book.find_and_delete(_id)
|
||||||
return redirect("/recipes")
|
return redirect("/recipes")
|
||||||
|
|
||||||
|
|
||||||
@app.route('/add', methods=['GET', 'POST'])
|
@app.route("/add", methods=["GET", "POST"])
|
||||||
def add_recipe():
|
def add_recipe():
|
||||||
if request.method == "POST":
|
if request.method == "POST":
|
||||||
new_recipe = Recipe(request.form.get('name'), request.form.get('yield'))
|
new_recipe = Recipe(request.form.get("name"), request.form.get("yield"))
|
||||||
for item in request.form:
|
for item in request.form:
|
||||||
if item not in ['name', 'yield']:
|
if item not in ["name", "yield"]:
|
||||||
new_recipe.add_ingredient(item, int(request.form.get(item)))
|
new_recipe.add_ingredient(item, int(request.form.get(item)))
|
||||||
|
|
||||||
book.add_recipe(new_recipe)
|
book.add_recipe(new_recipe)
|
||||||
|
|||||||
Reference in New Issue
Block a user