-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
134 lines (103 loc) · 5.19 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import re
import time
from flask import Flask, render_template, request, redirect, url_for
from helper import recipes, add_ingredients, add_instructions
from forms import RecipeForm, CommentForm, IngredientsFormSet, IngredientForm, InstructionForm
app = Flask(__name__)
app.config["SECRET_KEY"] = "mysecret"
@app.route("/")
def index():
return render_template("index.html", template_recipes=recipes, title='index')
@app.route("/about")
def about():
return render_template("about.html", title='About')
@app.route("/recipe/<string:name>", methods=["GET", "POST"])
def recipe(name):
comment_form = CommentForm(csrf_enabled=False)
if comment_form.validate_on_submit():
new_comment = comment_form.comment.data
recipes[name]['comments'].append(new_comment)
return render_template("recipe.html",
template_recipe=name,
template_type=recipes[name]['type'],
template_description=recipes[name]['description'],
template_ingredients=recipes[name]['ingredients'],
template_instructions=recipes[name]['instructions'],
template_comments=recipes[name]['comments'],
template_form=comment_form,
title=name)
@app.route('/preparation/<string:name>', methods=["GET", "POST"])
def preparation(name):
return render_template('preparation.html',
template_recipe=name)
@app.route('/preparation_step/<string:name>/<int:step>', methods=["GET", "POST"])
def preparation_step(name, step):
instruction = list(recipes[name]['instructions'].keys())[step - 1]
duration = list(recipes[name]['instructions'].values())[step - 1]
number_steps = len(recipes[name]['instructions'])
duration_int = int(re.findall("(\d+)", duration)[0]) * 60
if step < number_steps:
# timer(duration_int)
return render_template('preparation_step.html',
template_recipe=name,
template_step=step,
template_instruction=instruction,
template_duration=duration_int
)
else:
return redirect(url_for('index'))
@app.route("/add_recipe", methods=["GET", "POST"])
def new_recipe():
recipe_form = RecipeForm(csrf_enabled=False)
if recipe_form.validate_on_submit():
name = recipe_form.recipe.data
recipes[name] = {}
recipes[name]['type'] = recipe_form.recipe_type.data
recipes[name]['description'] = recipe_form.description.data
new_instructions = recipe_form.instructions.data
add_instructions(name, new_instructions)
new_ingredients = recipe_form.ingredients.data
add_ingredients(name, new_ingredients)
recipes[name]['comments'] = []
return redirect(url_for("new_ingredients_quantity", name=name, title=name))
return render_template("new_recipe.html", template_form=recipe_form, title='New Recipe')
@app.route("/add_recipe/new_ingredients/<string:name>", methods=["GET", "POST"])
def new_ingredients_quantity(name):
ingredients_form = IngredientsFormSet()
ingredients = recipes[name]['ingredients']
instructions = recipes[name]['instructions']
if ingredients_form.validate_on_submit():
ingredient_items = get_ingredients_from_set(ingredients_form)
instruction_items = get_instructions_from_set(ingredients_form)
save_ingredient_quantity(ingredient_items, ingredients)
save_instruction_duration(instruction_items, instructions)
return redirect(url_for("index"))
populate_ingredients(ingredients_form, name)
populate_instructions(ingredients_form, name)
return render_template("new_recipe_ingredients.html", template_ingredients_form=ingredients_form)
def save_instruction_duration(instruction_items, instructions):
for instruction_item in instruction_items:
instructions[instruction_item.data['instruction']] = instruction_item.data['step_duration']
def save_ingredient_quantity(ingredient_items, ingredients):
for ingredient_item in ingredient_items:
ingredients[ingredient_item.data['ingredient']] = ingredient_item.data['ingredient_quantity']
def get_instructions_from_set(ingredients_form):
return ingredients_form.instruction_set.entries
def get_ingredients_from_set(ingredients_form):
return ingredients_form.ingredient_set.entries
def populate_instructions(ingredients_form, name):
for instruction in recipes[name]['instructions']:
instruction_form = InstructionForm()
instruction_form.instruction = instruction
instruction_form.step_duration = ""
ingredients_form.instruction_set.append_entry(instruction_form)
def populate_ingredients(ingredients_form, name):
for ingredient in recipes[name]['ingredients']:
ingredient_form = IngredientForm()
ingredient_form.ingredient = ingredient
ingredient_form.ingredient_quantity = ""
ingredients_form.ingredient_set.append_entry(ingredient_form)
def timer(duration):
start_time = time.time()
while time.time() - start_time < duration:
pass