/
main.py
285 lines (219 loc) · 9.31 KB
/
main.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import webapp2
import os
import jinja2
import ast
from models import Recipe
from models import Allergy
from models import Questions
from google.appengine.api import urlfetch
theJinjaEnvironment = jinja2.Environment(
loader = jinja2.FileSystemLoader(os.path.dirname(__file__)),
extensions = [],
autoescape = True)
# formats a string to all lower case, spaces taken out, and separated (by comma) into a list
def formatString(string):
# formattedString = string.lower()
# formattedString = string.replace(",", ";")
formattedString = string.replace("; ", ";")
stringList = formattedString.split(";")
return stringList
# Finds and returns the allergy entity (with the allergy information)
# in the database of all allergens
def allergySearch(allergySearch):
allergySearch = allergySearch[0].upper() + allergySearch[1:]
allergyDatabase = Allergy.query().fetch()
allergy = None
for i in range(len(allergyDatabase)):
if (allergyDatabase[i].allergy == allergySearch):
allergy = allergyDatabase[i]
return allergy
# Finds and returns a list of recipes that fist the allergen-free parameter in the recipes database
def recipesSearch(allergenFree):
recipesDatabase = Recipe.query().fetch()
recipes = []
for i in range(len(recipesDatabase)):
if (allergenFree in recipesDatabase[i].allergenFree):
recipes.append(recipesDatabase[i])
return recipes
# RecipePuppy API call. Returns a list of recipe dictionaries.
# All recipes have "title", "href", "ingredients", and "thumbnail"
def recipeFetch(ingredients = "", type = ""):
ingredients = ingredients.lower()
ingredients = ingredients.replace(" ", "")
type = type.lower()
recipeSearch = "http://www.recipepuppy.com/api/?i="+ ingredients + "&q=" + type
searchResult = urlfetch.fetch(recipeSearch)
searchResult = ast.literal_eval(searchResult.content)
recipes = searchResult["results"]
return recipes
class WelcomePage(webapp2.RequestHandler):
def get(self):
welcomeTemplate = theJinjaEnvironment.get_template('templates/welcome.html')
self.response.write(welcomeTemplate.render())
class RecipeSubmitPage(webapp2.RequestHandler):
def get(self):
self.post()
def post(self):
recipeSubmitTemplate = theJinjaEnvironment.get_template('templates/recipeSubmit.html')
allergyName = self.request.get("allergyName")
templateDict = {
"allergyName": allergyName
}
self.response.write(recipeSubmitTemplate.render(templateDict))
class GenInfoPage(webapp2.RequestHandler):
def get(self):
self.post()
def post(self):
genInfoTemplate = theJinjaEnvironment.get_template('templates/genInfo.html')
questionsDatabase = Questions.query().fetch()
userQuestion = self.request.get("userQuestion")
question = None
for i in range(len(questionsDatabase)):
if (questionsDatabase[i].question == userQuestion):
question = questionsDatabase[i]
if question == None and not userQuestion == "":
question = Questions(question = userQuestion)
question.put()
answerName = self.request.get("answerName")
answer = self.request.get("answer")
questionKey = self.request.get("questionKey")
if (not answerName == "" and not answer == ""):
for i in range(len(questionsDatabase)):
if (questionsDatabase[i].question == questionKey):
question = questionsDatabase[i]
question.answerNames.append(answerName)
question.answers.append(answer)
question.put()
questionsDatabase = Questions.query().fetch()
# if (not question == None):
# if (not question in questionsDatabase):
# questionsDatabase.append(question)
#
# print(questionsDatabase)
templateDict = {
"questionsDatabase": questionsDatabase
}
self.response.write(genInfoTemplate.render(templateDict))
class AllergyInfoPage(webapp2.RequestHandler):
def get(self):
self.post()
def post(self):
allergyTemplate = theJinjaEnvironment.get_template('templates/allergyInfo.html')
allergyName = self.request.get("allergyName")
allergy = allergySearch(allergyName)
# posts the selected recipe- if there is a link, goes to the link. if not, go to recipe html template
if (allergy == None):
self.redirect("/submitAllergy")
return
ingredientsSearch = self.request.get("ingredients") + ",-" + allergyName
typeSearch = self.request.get("type")
# comments about the allergy
commentName = self.request.get("commentName")
comment = self.request.get("comment")
if (not commentName == "" and not comment == ""):
allergy.commentNames.append(commentName)
allergy.comments.append(comment)
allergy.put()
templateDict = {
"allergyName": allergy.allergy,
"symptoms": allergy.symptoms,
"toAvoid": allergy.toAvoid,
"images": allergy.images,
"dataRecipes": recipesSearch(allergy.allergy),
"apiRecipes": recipeFetch(ingredientsSearch, typeSearch),
"commentName": allergy.commentNames,
"comment": allergy.comments
}
self.response.write(allergyTemplate.render(templateDict))
# allergy submit page will just go back home
class AllergySubmitPage(webapp2.RequestHandler):
def get(self):
self.post()
def post(self):
allergySubmitTemplate = theJinjaEnvironment.get_template('templates/allergySubmit.html')
self.response.write(allergySubmitTemplate.render())
class RecipePage(webapp2.RequestHandler):
def get(self):
self.post()
def post(self):
recipeTemplate = theJinjaEnvironment.get_template('templates/recipe.html')
recipeName = self.request.get("recipeName")
recipesDatabase = Recipe.query().fetch()
recipe = None
for i in range(len(recipesDatabase)):
if (recipesDatabase[i].title == recipeName):
recipe = recipesDatabase[i]
allergyName = self.request.get("allergyName")
templateDict = {
"allergyName": allergyName,
"title": recipe.title,
"allergenFree": recipe.allergenFree,
"otherTags": recipe.otherTags,
"basicIngredients": recipe.basicIngredients,
"ingredients": recipe.ingredients,
"steps": recipe.steps,
}
self.response.write(recipeTemplate.render(templateDict))
class ThanksPage(webapp2.RequestHandler):
def post(self):
thanksTemplate = theJinjaEnvironment.get_template('templates/thanks.html')
allergyName = self.request.get("allergyName")
submission = self.request.get("submission")
message = ""
destination = ""
if (submission == "recipe"):
#recipe submit
title = self.request.get("title")
allergenFree = self.request.get("allergenFree")
basicIngredients = self.request.get("basicIngredients") #list
ingredients = self.request.get("ingredients") #list
ingredients = formatString(ingredients)
otherTags = self.request.get("otherTags") #list
otherTags = formatString(otherTags)
steps = self.request.get("steps") #list
steps = steps.replace("\r", "")
steps = formatString(steps)
recipe = Recipe(title = title, allergenFree = allergenFree, basicIngredients = basicIngredients)
for ingredient in ingredients:
recipe.ingredients.append(ingredient)
for otherTag in otherTags:
recipe.otherTags.append(otherTag)
for step in steps:
recipe.steps.append(step)
recipe.put()
message = "Thanks for submitting a new recipe."
destination = "/allergyInfo?allergyName=" + allergenFree
if (submission == "allergy"):
# allergy submit
allergy = self.request.get("allergen")
symptoms = self.request.get("symptoms")
symptoms = formatString(symptoms)
toAvoid = self.request.get("toAvoid")
toAvoid = formatString(toAvoid)
images = self.request.get("allergenImg")
images = formatString(images)
allergy = Allergy(allergy = allergy)
for symptom in symptoms:
allergy.symptoms.append(symptom)
for product in toAvoid:
allergy.toAvoid.append(product)
for link in images:
allergy.images.append(link)
allergy.put()
message = "Thanks for submitting a new allergy."
destination = "/"
templateDict = {
# "allergyName": allergyName,
"message": message,
"destination": destination
}
self.response.write(thanksTemplate.render(templateDict))
app = webapp2.WSGIApplication([
('/', WelcomePage),
('/submitRecipe', RecipeSubmitPage),
('/genInfo', GenInfoPage),
('/allergyInfo', AllergyInfoPage),
('/submitAllergy', AllergySubmitPage),
('/recipe', RecipePage),
('/thanks', ThanksPage)
], debug=True)