You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
159 lines
5.8 KiB
159 lines
5.8 KiB
4 years ago
|
#!venv/bin/python3
|
||
|
|
||
|
from flask import Flask, render_template, request
|
||
|
from config import DevConfig
|
||
|
from ingredient import Ingredient
|
||
|
from recipe import Recipe
|
||
|
import json
|
||
|
|
||
|
|
||
|
def create_app(test_config=None):
|
||
|
app = Flask(__name__)
|
||
|
if test_config is not None:
|
||
|
app.config.from_object(test_config)
|
||
|
else:
|
||
|
app.config.from_object(DevConfig)
|
||
|
|
||
|
import database
|
||
|
database.init_app(app)
|
||
|
|
||
|
@app.route('/', methods=['GET'])
|
||
|
def index():
|
||
|
return render_template("index.html")
|
||
|
|
||
|
@app.route('/debug', methods=['GET'])
|
||
|
def debug():
|
||
|
return repr(get_all_recipes())
|
||
|
|
||
|
@app.route('/rezepte', methods=['GET'])
|
||
|
def recipes():
|
||
|
return render_template("recipes.html", recipes=get_all_recipes())
|
||
|
|
||
|
@app.route('/neues_rezept', methods=['GET'])
|
||
|
def new_recipe():
|
||
|
return render_template("new_recipe.html")
|
||
|
|
||
|
@app.route('/process_recipe', methods=['POST'])
|
||
|
def process_recipe():
|
||
|
recipe_name = request.form.get("recipe_name").strip()
|
||
|
description = request.form.get("description").strip()
|
||
|
additional_text = request.form.get("additional_text").strip()
|
||
|
servings = request.form.get("servings").strip()
|
||
|
ingredients = request.form.getlist("ingredient[]")
|
||
|
measures = request.form.getlist("measure[]")
|
||
|
amounts = request.form.getlist("amount[]")
|
||
|
preparation = request.form.getlist("preparation[]")
|
||
|
preparation_time = request.form.get("preparation_time")
|
||
|
contains_meat_or_meat_products = request.form.get("contains_meat_or_meatproducts") is not None
|
||
|
contains_fish = request.form.get("contains_fish") is not None
|
||
|
contains_animal_products = request.form.get("contains_animal_products") is not None
|
||
|
source = request.form.get("source").strip()
|
||
|
if recipe_name is None or servings is None \
|
||
|
or len(ingredients) == 0 or len(amounts) == 0 or len(measures) == 0 \
|
||
|
or len(ingredients) != len(amounts) != len(measures) \
|
||
|
or len(preparation) == 0:
|
||
|
# TODO: error
|
||
|
return "error"
|
||
|
amounts, measures, ingredients, preparation = sanitize_lists(amounts, measures, ingredients, preparation)
|
||
|
if len(measures) == 0 or len(amounts) == 0 or len(ingredients) == 0 or len(preparation) == 0:
|
||
|
# TODO: error
|
||
|
return "error after sanitizing"
|
||
|
merged_ingredients = merge_ingredients(amounts, measures, ingredients)
|
||
|
merged_ingredients_json = json.dumps([o.__dict__ for o in merged_ingredients])
|
||
|
preparation_json = json.dumps(preparation)
|
||
|
db = database.get_db()
|
||
|
# TODO: Injection-safe?
|
||
|
db.execute("""INSERT INTO
|
||
|
recipes(name, description, additional_text, servings, ingredients, preparation,
|
||
|
preparation_time, contains_meat_or_meat_products, contains_fish,
|
||
|
contains_animal_products, source)
|
||
|
VALUES (?,?,?,?,?,?,?,?,?,?,?)""",
|
||
|
(recipe_name, description, additional_text, servings, merged_ingredients_json,
|
||
|
preparation_json, preparation_time, contains_meat_or_meat_products,
|
||
|
contains_fish, contains_animal_products, source))
|
||
|
db.commit()
|
||
|
database.close_db()
|
||
|
# TODO: change return value
|
||
|
return repr(request.form)
|
||
|
|
||
|
@app.route('/rezept/<int:id>', methods=['GET'])
|
||
|
def get_recipe(id):
|
||
|
recipe = get_recipe_object_by_id(id)
|
||
|
if recipe is not None:
|
||
|
return render_template("recipe.html", recipe=recipe)
|
||
|
else:
|
||
|
return render_template("error.html", error="Das Rezept existiert (noch) nicht.")
|
||
|
|
||
|
@app.route('/rezept/<int:id>/bearbeiten', methods=['GET'])
|
||
|
def edit_recipe(id):
|
||
|
pass
|
||
|
|
||
|
@app.route('/rezept/<int:id>/process_edit', methods=['POST'])
|
||
|
def change_recipe(id):
|
||
|
pass
|
||
|
|
||
|
return app
|
||
|
|
||
|
|
||
|
def sanitize_lists(amounts, measures, ingredients, preparation):
|
||
|
for i in range(len(ingredients)-1, -1, -1):
|
||
|
amounts[i] = amounts[i].strip()
|
||
|
measures[i] = measures[i].strip()
|
||
|
ingredients[i] = ingredients[i].strip()
|
||
|
if amounts[i] == "" and measures[i] == "" and ingredients[i] == "":
|
||
|
del amounts[i]
|
||
|
del measures[i]
|
||
|
del ingredients[i]
|
||
|
for i in range(len(preparation)-1, -1, -1):
|
||
|
preparation[i] = preparation[i].strip()
|
||
|
if preparation[i] == "":
|
||
|
del preparation[i]
|
||
|
return amounts, measures, ingredients, preparation
|
||
|
|
||
|
|
||
|
def merge_ingredients(amounts, measures, ingredients):
|
||
|
merged_ingredients = []
|
||
|
for i in range(len(ingredients)):
|
||
|
merged_ingredients.append(Ingredient(amounts[i], measures[i], ingredients[i]))
|
||
|
return merged_ingredients
|
||
|
|
||
|
|
||
|
def get_all_recipes():
|
||
|
import database
|
||
|
db = database.get_db()
|
||
|
answer = db.execute("SELECT * FROM recipes")
|
||
|
recipes = answer.fetchall()
|
||
|
database.close_db()
|
||
|
return recipes
|
||
|
|
||
|
|
||
|
def get_recipe_object_by_id(id):
|
||
|
import database
|
||
|
db = database.get_db()
|
||
|
# TODO: injection safe?
|
||
|
print("trying to query recipe with id: " + str(id))
|
||
|
answer = db.execute("SELECT * FROM recipes WHERE id=?", (str(id)))
|
||
|
recipe = answer.fetchone()
|
||
|
database.close_db()
|
||
|
if recipe is None:
|
||
|
return None
|
||
|
return Recipe(recipe['name'],
|
||
|
recipe['description'],
|
||
|
recipe['additional_text'],
|
||
|
recipe['servings'],
|
||
|
recipe['ingredients'],
|
||
|
recipe['preparation'],
|
||
|
recipe['preparation_time'],
|
||
|
recipe['upvotes'],
|
||
|
recipe['downvotes'],
|
||
|
recipe['contains_meat_or_meat_products'],
|
||
|
recipe['contains_fish'],
|
||
|
recipe['contains_animal_products'],
|
||
|
recipe['enhancements'],
|
||
|
recipe['source'])
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
redab_app = create_app()
|
||
|
redab_app.run()
|