-
Notifications
You must be signed in to change notification settings - Fork 0
/
restaurants.py
95 lines (77 loc) · 2.99 KB
/
restaurants.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
from flask import Flask, render_template, request, redirect, url_for
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
app = Flask(__name__)
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from database_setup import Base, Restaurant, MenuItem
engine = create_engine('sqlite:///restaurantmenu.db')
Base.metadata.bind = engine
DBSession = sessionmaker(bind=engine)
session = DBSession()
@app.route('/')
def restaurantMenu(restaurant_id):
restaurant = session.query(Restaurant).first()
items = session.query(MenuItem).filter_by(restaurant_id=restaurant.id)
output = ''
for i in items:
output += i.name
output += '</br>'
output += i.price
output += '</br>'
output += i.description
output += '</br>'
output += '</br>'
return output
@app.route('/restaurants/<int:restaurant_id>/')
def restaurantMenu(restaurant_id):
restaurant = session.query(Restaurant).filter_by(id=restaurant_id).one()
items = session.query(MenuItem).filter_by(restaurant_id=restaurant_id)
output = ''
for i in items:
output += i.name
output += '</br>'
output += i.price
output += '</br>'
output += i.description
output += '</br>'
output += '</br>'
return output
# Task 1: Create route for newMenuItem function here
@app.route('/restaurants/<int:restaurant_id>/new/', methods=['GET', 'POST'])
def newMenuItem(restaurant_id):
if request.method == 'POST':
newItem = MenuItem(
name=request.form['name'],
restaurant_id=restaurant_id
)
session.add(newItem)
session.commit()
return redirect(url_for('restaurantMenu', restaurant_id=restaurant_id))
else:
return render_template('newmenuitem.html', restaurant_id=restaurant_id)
# Task 2: Create route for editMenuItem function here
# @app.route('/restaurants/<int:restaurant_id>/<int:menu_id>/edit/')
@app.route('/restaurants/<int:restaurant_id>/<int:MenuID>/edit',
methods=['GET', 'POST'])
def editMenuItem(restaurant_id, menuID):
editedItem = session.query(MenuItem).filter_by(id=menuID).one()
if request.method == 'POST':
if request.form['name']:
editedItem.name = request.form['name']
session.add(editedItem)
session.commit()
return redirect(url_for('restaurantMenu', restaurant_id=restaurant_id))
else:
# USE THE RENDER_TEMPLATE FUNCTION BELOW TO SEE THE VARIABLES YOU
# SHOULD USE IN YOUR EDITMENUITEM TEMPLATE
return render_template(
'editmenuitem.html',
restaurant_id=restaurant_id,
menu_id=menuID, i=editedItem)
# Task 3: Create a route for deleteMenuItem function here
@app.route('/restaurants/<int:restaurant_id>/<int:menu_id>/delete/')
def deleteMenuItem(restaurant_id, menu_id):
return "page to delete a menu item. Task 3 complete!"
if __name__ == '__main__':
app.debug = True
app.run(host='0.0.0.0', port=5000)