-
Notifications
You must be signed in to change notification settings - Fork 0
/
menuApp.py
154 lines (124 loc) · 6.25 KB
/
menuApp.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
from flask import Flask, render_template, request, redirect, url_for, flash, jsonify
app=Flask(__name__)
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from database_setup import Base, Restaurant, MenuItem
# Fake Restaurants
restaurant = {'name': 'The CRUDdy Crab', 'id': '1'}
restaurants = [{'name': 'The CRUDdy Crab', 'id': '1'}, {
'name': 'Blue Burgers', 'id': '2'}, {'name': 'Taco Hut', 'id': '3'}]
# Fake Menu Items
items = [{'name': 'Cheese Pizza', 'description': 'made with fresh cheese', 'price': '$5.99', 'course': 'Entree', 'id': '1'}, {'name': 'Chocolate Cake', 'description': 'made with Dutch Chocolate', 'price': '$3.99', 'course': 'Dessert', 'id': '2'}, {'name': 'Caesar Salad', 'description':
'with fresh organic vegetables', 'price': '$5.99', 'course': 'Entree', 'id': '3'}, {'name': 'Iced Tea', 'description': 'with lemon', 'price': '$.99', 'course': 'Beverage', 'id': '4'}, {'name': 'Spinach Dip', 'description': 'creamy dip with fresh spinach', 'price': '$1.99', 'course': 'Appetizer', 'id': '5'}]
item = {'name': 'Cheese Pizza', 'description': 'made with fresh cheese',
'price': '$5.99', 'course': 'Entree'}
#create engine
engine=create_engine('sqlite:///restaurantmenu.db?check_same_thread=False')
Base.metadata.bind=engine
#create session
DBSession = sessionmaker(bind=engine)
session=DBSession()
@app.route('/restaurants/JSON')
def restaurantsJSON():
restaurants = session.query(Restaurant).all()
return jsonify(Restaurants=[i.serialize for i in restaurants])
@app.route('/restaurant/<int:restaurant_id>/menu/JSON')
def restaurantMenuJSON(restaurant_id):
restaurant = session.query(Restaurant).filter_by(id=restaurant_id).one()
items = session.query(MenuItem).filter_by(
restaurant_id=restaurant_id).all()
return jsonify(MenuItems=[i.serialize for i in items])
@app.route('/restaurant/<int:restaurant_id>/menu/<int:menu_id>/JSON')
def menuItemJSON(restaurant_id,menu_id):
restaurant = session.query(MenuItem).filter_by(id=restaurant_id).one()
item = session.query(MenuItem).filter_by(
id=menu_id).one()
return jsonify(MenuItem=item.serialize)
@app.route('/')
@app.route('/restaurants')
def showRestaurants():
restaurants=session.query(Restaurant).all()
return render_template('restaurants.html',restaurants=restaurants)
@app.route('/restaurants/new',methods=['GET', 'POST'])
def newRestaurant():
if request.method=='POST':
newRestaurant=Restaurant(name=request.form['name'])
session.add(newRestaurant)
session.commit()
flash("New Restaurant Created!")
return redirect(url_for('showRestaurants'))
else:
return render_template('newRestaurant.html')
@app.route('/restaurant/<int:restaurant_id>/edit',methods=['GET', 'POST'])
def editRestaurant(restaurant_id):
restaurant=session.query(Restaurant).filter_by(id=restaurant_id).one()
if request.method=='POST':
restaurant.name=request.form['name']
session.add(restaurant)
session.commit()
flash("Restaurant Edited!")
return redirect(url_for('showRestaurants'))
else:
return render_template('editRestaurant.html',restaurant = restaurant)
@app.route('/restaurant/<int:restaurant_id>/delete',methods=['GET', 'POST'])
def deleteRestaurant(restaurant_id):
restaurant=session.query(Restaurant).filter_by(id=restaurant_id).one()
if request.method=='POST':
session.delete(restaurant)
session.commit()
flash("Restaurant Deleted!")
return redirect(url_for('showRestaurants'))
else:
return render_template('deleteRestaurant.html',restaurant = restaurant)
@app.route('/restaurant/<int:restaurant_id>')
@app.route('/restaurant/<int:restaurant_id>/menu')
def showMenu(restaurant_id):
restaurant=session.query(Restaurant).filter_by(id=restaurant_id).one()
items=session.query(MenuItem).filter_by(restaurant_id=restaurant_id)
return render_template('menu.html',restaurant = restaurant,items=items)
@app.route('/restaurant/<int:restaurant_id>/menu/new',methods=['GET', 'POST'])
def newMenuItem(restaurant_id):
if request.method == 'POST':
newItem = MenuItem(
name=request.form['name'],
description=request.form['description'],
price=request.form['price'],
course=request.form['course'],
restaurant_id=restaurant_id)
session.add(newItem)
session.commit()
flash("New Menu Item Created!")
return redirect(url_for('showMenu', restaurant_id=restaurant_id))
else:
return render_template('newmenuitem.html', restaurant_id=restaurant_id)
return render_template('newmenuitem.html')
@app.route('/restaurant/<int:restaurant_id>/menu/<int:menu_id>/edit',methods=['GET', 'POST'])
def editMenuItem(restaurant_id,menu_id):
editedItem = session.query(MenuItem).filter_by(id=menu_id).one()
if request.method == 'POST':
if request.form['name']:
editedItem.name = request.form['name']
editedItem.description = request.form['description']
editedItem.price = request.form['price']
editedItem.course = request.form['course']
session.add(editedItem)
session.commit()
flash("Menu Item Edited!")
return redirect(url_for('showMenu', restaurant_id=editedItem.restaurant_id))
else:
return render_template(
'editmenuitem.html', item=editedItem)
@app.route('/restaurant/<int:restaurant_id>/menu/<int:menu_id>/delete',methods=['GET', 'POST'])
def deleteMenuItem(restaurant_id,menu_id):
itemToDelete=session.query(MenuItem).filter_by(id=menu_id).one()
if request.method == 'POST':
session.delete(itemToDelete)
session.commit()
flash("Menu Item Deleted!")
return redirect(url_for('showMenu', restaurant_id=itemToDelete.restaurant_id))
else:
return render_template('deletemenuitem.html',item=itemToDelete)
if __name__ == '__main__':
app.secret_key='super_user_key'
app.debug = True
app.run(host='0.0.0.0', port=5000)