27
27
APPLICATION_NAME = "Restaurant Menu Application"
28
28
29
29
30
- # Create anti-forgery state token
31
- @app .route ('/login' )
32
- def showLogin ():
33
- state = '' .join (random .choice (string .ascii_uppercase + string .digits )
34
- for x in xrange (32 ))
35
- login_session ['state' ] = state
36
- # return "The current session state is %s" % login_session['state']
37
- return render_template ('login.html' , STATE = state )
38
-
39
-
40
30
@app .route ('/gconnect' , methods = ['POST' ])
41
31
def gconnect ():
42
32
# Validate state token
@@ -109,6 +99,11 @@ def gconnect():
109
99
login_session ['picture' ] = data ['picture' ]
110
100
login_session ['email' ] = data ['email' ]
111
101
102
+ user_id = getUserID (login_session ['email' ])
103
+ if not user_id :
104
+ user_id = createUser (login_session )
105
+ login_session ['user_id' ] = user_id
106
+
112
107
output = ''
113
108
output += '<h1>Welcome, '
114
109
output += login_session ['username' ]
@@ -122,6 +117,39 @@ def gconnect():
122
117
return output
123
118
124
119
120
+ # Create anti-forgery state token
121
+ @app .route ('/login' )
122
+ def showLogin ():
123
+ state = '' .join (random .choice (string .ascii_uppercase + string .digits )
124
+ for x in xrange (32 ))
125
+ login_session ['state' ] = state
126
+ # return "The current session state is %s" % login_session['state']
127
+ return render_template ('login.html' , STATE = state )
128
+
129
+
130
+
131
+ def createUser (login_session ):
132
+ newUser = User (name = login_session ['username' ], email = login_session [
133
+ 'email' ], picture = login_session ['picture' ])
134
+ session .add (newUser )
135
+ session .commit ()
136
+ user = session .query (User ).filter_by (email = login_session ['email' ]).one ()
137
+ return user .id
138
+
139
+
140
+ def getUserInfo (user_id ):
141
+ user = session .query (User ).filter_by (id = user_id ).one ()
142
+ return user
143
+
144
+
145
+ def getUserID (email ):
146
+ try :
147
+ user = session .query (User ).filter_by (email = email ).one ()
148
+ return user .id
149
+ except :
150
+ return None
151
+
152
+
125
153
@app .route ('/gdisconnect' )
126
154
def gdisconnect ():
127
155
access_token = login_session .get ('access_token' )
@@ -188,7 +216,7 @@ def showRestaurants():
188
216
@app .route ('/restaurant/new/' , methods = ['GET' , 'POST' ])
189
217
def newRestaurant ():
190
218
if request .method == 'POST' :
191
- newRestaurant = Restaurant (name = request .form ['name' ])
219
+ newRestaurant = Restaurant (name = request .form ['name' ], user_id = login_session [ 'user_id' ] )
192
220
session .add (newRestaurant )
193
221
flash ('New Restaurant %s Successfully Created' % newRestaurant .name )
194
222
session .commit ()
@@ -236,7 +264,17 @@ def showMenu(restaurant_id):
236
264
restaurant = session .query (Restaurant ).filter_by (id = restaurant_id ).one ()
237
265
items = session .query (MenuItem ).filter_by (
238
266
restaurant_id = restaurant_id ).all ()
239
- return render_template ('menu.html' , items = items , restaurant = restaurant )
267
+
268
+ creator = getUserInfo (login_session ['user_id' ])
269
+
270
+ if creator .id == restaurant .user_id :
271
+ # return True
272
+ return render_template ('menu.html' , items = items , restaurant = restaurant , creator = creator )
273
+
274
+ creator = getUserInfo (restaurant .user_id )
275
+
276
+ return render_template ('publicmenu.html' , items = items , restaurant = restaurant , creator = creator )
277
+
240
278
241
279
242
280
# Create a new menu item
@@ -245,10 +283,10 @@ def newMenuItem(restaurant_id):
245
283
restaurant = session .query (Restaurant ).filter_by (id = restaurant_id ).one ()
246
284
if request .method == 'POST' :
247
285
newItem = MenuItem (name = request .form ['name' ], description = request .form [
248
- 'description' ], price = request .form ['price' ], course = request .form ['course' ], restaurant_id = restaurant_id )
286
+ 'description' ], price = request .form ['price' ], course = request .form ['course' ], restaurant_id = restaurant_id , user_id = login_session [ 'user_id' ] )
249
287
session .add (newItem )
250
288
session .commit ()
251
- flash ('New Menu %s Item Successfully Created' % ( newItem .name ) )
289
+ flash ('New Menu %s Item Successfully Created' % newItem .name )
252
290
return redirect (url_for ('showMenu' , restaurant_id = restaurant_id ))
253
291
else :
254
292
return render_template ('newmenuitem.html' , restaurant_id = restaurant_id )
@@ -281,7 +319,6 @@ def editMenuItem(restaurant_id, menu_id):
281
319
# Delete a menu item
282
320
@app .route ('/restaurant/<int:restaurant_id>/menu/<int:menu_id>/delete' , methods = ['GET' , 'POST' ])
283
321
def deleteMenuItem (restaurant_id , menu_id ):
284
- restaurant = session .query (Restaurant ).filter_by (id = restaurant_id ).one ()
285
322
itemToDelete = session .query (MenuItem ).filter_by (id = menu_id ).one ()
286
323
if request .method == 'POST' :
287
324
session .delete (itemToDelete )
@@ -292,13 +329,7 @@ def deleteMenuItem(restaurant_id, menu_id):
292
329
return render_template ('deleteMenuItem.html' , item = itemToDelete )
293
330
294
331
295
- def CreateUser (login_session ):
296
- new_user = User (name = login_session ['username' ], email = login_session ['email' ], picture = login_session ['picture' ])
297
- session .add (new_user )
298
- session .commit ()
299
-
300
-
301
332
if __name__ == '__main__' :
302
333
app .secret_key = 'super_secret_key'
303
334
app .debug = True
304
- app .run (host = '0.0.0.0' , port = 5000 )
335
+ app .run (host = '0.0.0.0' , port = 5000 , threaded = False )
0 commit comments