Added microservice to download PDFs
This commit is contained in:
35
main.py
35
main.py
@@ -3,8 +3,9 @@ Portfolio Project for CS361 Spring 2022
|
||||
Written by Lucas Jensen
|
||||
Last updated 5/12 for Assignment 1
|
||||
"""
|
||||
from flask import Flask, redirect, render_template, request
|
||||
from flask import Flask, redirect, render_template, request, send_file
|
||||
from recipe import Recipe, RecipeBook
|
||||
from time import sleep
|
||||
|
||||
app = Flask(__name__)
|
||||
book = RecipeBook()
|
||||
@@ -25,17 +26,39 @@ def recipes_page():
|
||||
|
||||
@app.route('/recipes/<_id>', methods=['GET', 'POST'])
|
||||
def recipe_page(_id):
|
||||
recipe = book.find_by_id(_id)
|
||||
recipe = book.find_by_id(str(_id))
|
||||
|
||||
if request.method == 'POST':
|
||||
scale = request.form.get('scale')
|
||||
new = recipe.scale(scale)
|
||||
new_id = book.add_recipe(new)
|
||||
return redirect(f'/recipes/{new_id}')
|
||||
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}')
|
||||
|
||||
else:
|
||||
path = write_txt(_id)
|
||||
sleep(0.5)
|
||||
return send_file(path, as_attachment=True)
|
||||
|
||||
return render_template("recipe.html", content=recipe, _id=_id)
|
||||
|
||||
|
||||
def write_txt(_id):
|
||||
"""
|
||||
writes to the txt file to have the microservice make a selected pdf
|
||||
:return:
|
||||
"""
|
||||
print(_id)
|
||||
with open('recipe.txt', 'w') as txt_file:
|
||||
txt_file.write(_id)
|
||||
|
||||
return f"recipe{_id}.pdf"
|
||||
|
||||
|
||||
# app.route('/recipes/<id>/download')
|
||||
# def
|
||||
|
||||
|
||||
@app.route('/recipes/<_id>/delete')
|
||||
def delete_recipe(_id):
|
||||
book.find_and_delete(_id)
|
||||
|
||||
Reference in New Issue
Block a user