-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
386 lines (357 loc) · 15.3 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from google.appengine.api import users
from google.appengine.ext import ndb
from google.appengine.api.datastore_types import GeoPt
from datastore import User
from datastore import Recipe
import webapp2
import os
import json
import jinja2
#import webapp2_extras.appengine.auth.models
jinja_environment = jinja2.Environment(loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))
def UserId() :
return users.get_current_user().user_id()
def UserInfo() :
user = users.get_current_user()
name = user.nickname()
userID = UserId()
dbUser = User.query().filter(User.user_id == userID).fetch(1)
return dbUser
def GetHeader(type) :
dbUser = UserInfo()
name = users.get_current_user().nickname()
header_values = {
'logout_url' : users.create_logout_url('/'),
'name' : users.get_current_user().nickname()
}
if len(dbUser) > 0 :
user_recipes_url = '/recipes/by/' + str(dbUser[0].user_id)
header_values['user_recipes_url'] = user_recipes_url
homepage_header = jinja_environment.get_template('templates/homepage_header.html').render(header_values)
recipe_header = jinja_environment.get_template('templates/recipes_header.html').render(header_values)
if type == 'homepage' :
return homepage_header
if type == 'recipe' :
return recipe_header
class HomeHandler(webapp2.RequestHandler):
def get(self):
# check if user is already in database
rawRecipes = Recipe.query().fetch()
recipes = []
for rawRecipe in rawRecipes :
if rawRecipe.image :
recipes.append({
'name': rawRecipe.name,
'latlng': str(rawRecipe.location),
'location': rawRecipe.location_name,
'cooktime': rawRecipe.cooktime,
'image': '<div style="background-image: url(' + rawRecipe.imageUrl() + ')"></div>',
'servings': rawRecipe.servings,
'ingredients': json.dumps(rawRecipe.ingredients),
'recipeUrl' : rawRecipe.viewUrl()
})
else :
recipes.append({
'name': rawRecipe.name,
'latlng': str(rawRecipe.location),
'location': rawRecipe.location_name,
'cooktime': rawRecipe.cooktime,
'servings': rawRecipe.servings,
'ingredients': json.dumps(rawRecipe.ingredients),
'recipeUrl' : rawRecipe.viewUrl()
})
user = users.get_current_user()
name = user.nickname()
userID = user.user_id()
dbUser = User.query().filter(User.user_id == userID).fetch(1)
if UserInfo() :
name = dbUser[0].name
template_values = { 'name': name }
firstTime = False
else :
newUser = User(user_id=userID, name=name, savedRecipes=[''])
newUser.put()
name = user.nickname()
firstTime = True
template_values = { 'title': 'Homepage', 'recipes': json.dumps(recipes), 'header': GetHeader('homepage'), 'logout_url': users.create_logout_url('/'), 'first_time': firstTime }
Homepage = jinja_environment.get_template('templates/homepage.html').render(template_values)
self.response.write(Homepage)
class NewRecipe(webapp2.RequestHandler):
def get(self):
# show new recipe form
template_values = { 'title': 'Add New Recipe', 'header': GetHeader('recipe'), 'userID' : UserId() }
NewRecipePage = jinja_environment.get_template('templates/recipes_add.html').render(template_values)
self.response.write(NewRecipePage)
def post(self):
# handles recipe submissions
import re
recipe_added = {
'name' : self.request.get('name'),
'image' : self.request.get('img'),
'cook_time' : self.request.get('cooktime'),
'instructions' : json.loads(str(self.request.get('instructions'))),
'servings' : int(self.request.get('servings')),
'author' : self.request.get('author'),
'location_name' : self.request.get('location_name'),
'location' : GeoPt(re.sub("[()]", "", self.request.get('location'))),
'ingredients' : json.loads(str(self.request.get('ingredients')))
}
newRecipe = Recipe(
name=recipe_added['name'],
cooktime=recipe_added['cook_time'],
instructions=recipe_added['instructions'],
servings=recipe_added['servings'],
author=recipe_added['author'],
location=recipe_added['location'],
location_name=recipe_added['location_name'],
ingredients=recipe_added['ingredients']
)
if recipe_added['image'] :
newRecipe.image = recipe_added['image']
newRecipe.put()
recipe_added['id'] = newRecipe.key.id()
template_values = { 'title': 'New Recipe Added', 'success': True, 'recipe_added': recipe_added, 'header': GetHeader('recipe'), 'userID' : UserId() }
RecipeAddedPage = jinja_environment.get_template('templates/recipes_added.html').render(template_values)
self.response.write(RecipeAddedPage)
class ViewIndividualRecipe(webapp2.RequestHandler):
def get(self, recipe_id):
# check if ID is found in recipe database
recipe = Recipe.get_by_id(int(recipe_id))
if not recipe :
error = "That recipe was not found."
template_values = { 'error' : error }
else :
# check if this recipe belongs to user
if recipe.author == UserId() :
isOwner = True
else :
isOwner = False
recipe_author_name = User.query().filter(User.user_id == recipe.author).fetch(1)[0].name
recipe_author_pic = User.query().filter(User.user_id == recipe.author).fetch(1)[0].picUrl()
template_values = { 'recipe_author_pic' : recipe_author_pic, 'title': recipe.name, 'recipe' : recipe, 'isOwner' : isOwner, 'header': GetHeader('recipe'), 'recipe_author_name': recipe_author_name }
IndividualRecipe = jinja_environment.get_template('templates/recipes_individual.html').render(template_values)
self.response.write(IndividualRecipe)
class ViewRecipesBy(webapp2.RequestHandler):
def get(self, author_id):
# check if there is user that corresponds with the ID
author = User.query().filter(User.user_id == author_id).fetch()
if author :
# check if the user has any recipes
recipes = Recipe.query().filter(Recipe.author == author_id).fetch()
if recipes :
template_values = { 'header': GetHeader('recipe'), 'recipes' : recipes, 'author' : author[0].name ,'author_bio' : author[0].bio , 'title' : 'Recipes by ' + author[0].name}
if author[0].pic :
template_values['author_pic'] = author[0].picUrl()
if author[0].location :
template_values['author_location'] = author[0].location
else :
error = "This user has not uploaded any recipes."
template_values = { 'header': GetHeader('recipe'), 'title': 'Error', 'error' : error }
else :
error = "This user doesn't not exist."
template_values = { 'header': GetHeader('recipe'), 'title': 'Error', 'error' : error }
RecipesBy = jinja_environment.get_template('templates/recipes_by.html').render(template_values)
self.response.write(RecipesBy)
class EditRecipePage(webapp2.RequestHandler):
def get(self, recipe_id):
recipe = Recipe.get_by_id(int(recipe_id))
success = self.request.get('success')
# check if the recipe ID exists
if recipe :
# check if user has permission to edit the recipe
if recipe.author == UserId() :
template_values = { 'title': 'Edit Recipe', 'recipe' : recipe, 'recipe_id' : recipe_id, 'success' : success }
else :
error = "that's not your recipe yo"
template_values = { 'title': 'Error', 'error' : error }
else :
error = "that doesn't exist, yo"
template_values = { 'title': 'Error', 'error' : error }
template_values['header'] = GetHeader('recipe')
EditRecipePage = jinja_environment.get_template('templates/recipes_edit.html').render(template_values)
self.response.write(EditRecipePage)
class EdittedRecipe(webapp2.RequestHandler):
def post(self):
import re
# fetch new values
recipe_editted = {
'id' : self.request.get('recipe_id'),
'name' : self.request.get('name'),
'cook_time' : self.request.get('cooktime'),
'instructions' : self.request.get('instructions'),
'image' : self.request.get('img'),
'servings' : int(self.request.get('servings')),
'author' : self.request.get('author'),
'location' : GeoPt(re.sub("[()]", "", self.request.get('location'))),
'location_name' : self.request.get('location_name'),
'ingredients' : json.loads(str(self.request.get('ingredients'))),
'instructions' : json.loads(str(self.request.get('instructions')))
}
# fetch recipe model corresponding to the recipe we wanna edit
recipe_to_edit = Recipe.get_by_id(int(recipe_editted['id']))
recipe_to_edit.name = recipe_editted['name']
recipe_to_edit.cooktime = recipe_editted['cook_time']
if recipe_editted['image'] != '' :
recipe_to_edit.image = recipe_editted['image']
recipe_to_edit.instructions = recipe_editted['instructions']
recipe_to_edit.servings = recipe_editted['servings']
recipe_to_edit.location = recipe_editted['location']
recipe_to_edit.location_name = recipe_editted['location_name']
if recipe_editted['ingredients'] :
recipe_to_edit.ingredients = recipe_editted['ingredients']
recipe_to_edit.put()
self.redirect(recipe_to_edit.editUrlSuccess())
class ViewAllRecipes(webapp2.RequestHandler):
def get(self):
# fetch all recipes
recipes = Recipe.query().fetch()
template_values = {'title': 'All Recipes', 'header': GetHeader('recipe'), 'recipes' : recipes, 'title': 'All Recipes'}
ViewAllRecipes = jinja_environment.get_template('templates/recipes_all.html').render(template_values)
self.response.write(ViewAllRecipes)
class DeleteRecipe(webapp2.RequestHandler):
def get(self, recipe_id):
recipe = Recipe.get_by_id(int(recipe_id))
# check if the recipe ID exists
if recipe :
# check if user has permission to edit the recipe
if recipe.author == UserId() :
recipe.key.delete()
template_values = {'header' : GetHeader('recipe')}
else :
error = "You do not have permission to do this."
template_values = {'header': GetHeader('recipe'), 'title': 'Error', 'error' : error }
else :
error = "The recipe you are trying to delete does not exist."
template_values = {'header': GetHeader('recipe'), 'title': 'Error', 'error' : error }
DeleteRecipePage = jinja_environment.get_template('templates/recipes_delete.html').render(template_values)
self.response.write(DeleteRecipePage)
class ThumbUpRecipe(webapp2.RequestHandler):
def post(self):
data = json.loads(self.request.body)
recipe = Recipe.get_by_id(int( data['recipeID'] ))
# check if user has already saved this recipe
dbUser = User.query().filter(User.user_id == UserId()).fetch(1)
user = dbUser[0]
matches = 0
for savedRecipe in user.savedRecipes :
if savedRecipe == data['recipeID'] :
matches += 1
break;
if matches == 0 :
if recipe.thumbsUp == None :
recipe.thumbsUp = 1
else :
recipe.thumbsUp += 1
recipe.put()
user.savedRecipes.append( data['recipeID'] );
user.put()
self.response.write(json.dumps(({'recipe_thumbsUp': recipe.thumbsUp, 'net_thumbs': recipe.thumbsUp})))
class SavedRecipes(webapp2.RequestHandler):
def get(self) :
dbUser = UserInfo()
if dbUser :
user = dbUser[0]
recipes = []
for recipe in user.savedRecipes :
if recipe != '' :
savedRecipe = Recipe.get_by_id(int(recipe))
if savedRecipe :
recipes.append(savedRecipe)
template_values = { 'title': 'Your Saved Recipes', 'header': GetHeader('recipe'), 'recipes' : recipes, 'title': 'Your Saved Recipes' }
SavedRecipes = jinja_environment.get_template('templates/recipes_all.html').render(template_values)
self.response.write(SavedRecipes)
else :
self.response.write("EH")
class UserSettings(webapp2.RequestHandler) :
def get(self) :
dbUser = UserInfo()
if dbUser :
user = dbUser[0]
template_values = { 'title': 'Edit User Settings', 'user': user }
else :
error = "an error occurred"
template_values = { 'title': 'Error', 'error': error }
template_values['header'] = GetHeader('recipe')
SettingsPage = jinja_environment.get_template('templates/user_settings.html').render(template_values)
self.response.write(SettingsPage)
def post(self) :
dbUser = UserInfo()
if dbUser :
user = dbUser[0]
updated_location = self.request.get("location")
updated_image = self.request.get("image")
updated_bio = self.request.get("bio")
user.bio = updated_bio
user.location = updated_location
if updated_image != '' :
user.pic = updated_image
user.put()
template_values = { 'title': 'Settings Saved', 'success': True, 'user': user, 'header': GetHeader('recipe') }
SettingsPage = jinja_environment.get_template('templates/user_settings.html').render(template_values)
self.response.write(SettingsPage)
class Image(webapp2.RequestHandler):
def get(self) :
image_id = self.request.get('id')
image = Recipe.get_by_id(int(image_id)).image
self.response.headers['Content-Type'] = 'image/png'
self.response.write(image)
class UserPic(webapp2.RequestHandler):
def get(self) :
image_id = self.request.get('id')
image = User.get_by_id(int(image_id)).pic
self.response.headers['Content-Type'] = 'image/png'
self.response.write(image)
class RemoveSavedRecipe(webapp2.RequestHandler):
def post(self):
recipe_id = self.request.get("recipe_id")
user = UserInfo()
savedRecipes = user[0].savedRecipes
# find index of recipe within savedRecipes property
index = -1
for recipe in savedRecipes :
index += 1
if recipe != '' and int(recipe) == int(recipe_id) :
break;
if index > -1 :
del savedRecipes[index]
user[0].savedRecipes = savedRecipes
user[0].put()
RemoveSavedPage = jinja_environment.get_template('templates/recipes_remove_saved.html').render({'header' : GetHeader('recipe')})
self.response.write(RemoveSavedPage)
class NotFound(webapp2.RequestHandler):
def get(self):
ErrorPage = jinja_environment.get_template('templates/error.html').render()
self.response.write(ErrorPage)
app = webapp2.WSGIApplication([
('/', HomeHandler),
('/recipes/new', NewRecipe),
('/recipes/view/(\d+)', ViewIndividualRecipe),
('/recipes/by/(\d+)', ViewRecipesBy),
('/recipes/edit/(\d+)', EditRecipePage),
('/recipes/editted', EdittedRecipe),
('/recipes/all', ViewAllRecipes),
('/recipes/delete/(\d+)', DeleteRecipe),
('/recipes/thumbsUp', ThumbUpRecipe),
('/recipes/saved', SavedRecipes),
('/images', Image),
('/settings', UserSettings),
('/userpic', UserPic),
('/recipes/saved/remove', RemoveSavedRecipe),
('/.*', NotFound)
], debug=True)