-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject.py
174 lines (146 loc) · 6.4 KB
/
project.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
#!/usr/bin/env python3.7
from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine
from flask import Flask, render_template, redirect, url_for, request, flash,\
jsonify
from database_setup import Base, Restaurant, MenuItem
app = Flask(__name__)
# DB Session maker
# Create session and connect to DB
engine = create_engine('sqlite:///restaurantmenu.db',
connect_args={'check_same_thread': False})
Base.metadata.bind = engine
DBSession = sessionmaker(bind=engine)
session = DBSession()
@app.route('/restaurants/JSON')
def restaurantsJSON():
restaurants = session.query(Restaurant).all()
return jsonify(restaurants=[r.serialize for r in restaurants])
@app.route('/restaurants/<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('/restaurants/<int:restaurant_id>/menu/<int:menu_id>/JSON/')
def itemMenuJSON(restaurant_id, menu_id):
restaurant = session.query(Restaurant).filter_by(id=restaurant_id).one()
item = session.query(MenuItem).filter_by(id=menu_id).one()
return jsonify(menuItem=item.serialize)
# Show all restaurants
@app.route('/')
@app.route('/restaurants/')
def showRestaurants():
restaurants = session.query(Restaurant).all()
return render_template('showRestaurants.html', restaurants=restaurants)
# Create a new restaurant
@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(F"The new Restaurant {newRestaurant.name} was created.")
return redirect(url_for('showRestaurants'))
else:
return render_template('newRestaurant.html')
# Update/Edit a restaurant
@app.route('/restaurants/<int:restaurant_id>/edit/', methods=['GET', 'POST'])
def editRestaurant(restaurant_id):
editedRestaurant = session.query(Restaurant).filter_by(
id=restaurant_id).one()
if request.method == 'POST':
if request.form['name']:
oldRestaurantName = editedRestaurant.name
editedRestaurant.name = request.form['name']
session.add(editedRestaurant)
session.commit()
flash(F"The restaurant {oldRestaurantName} was updated to \
{editedRestaurant.name}.")
return redirect(url_for('showRestaurants'))
else:
return render_template('editRestaurant.html',
restaurant=editedRestaurant)
# Delete a restaurant
@app.route('/restaurants/<int:restaurant_id>/delete/', methods=['GET', 'POST'])
def deleteRestaurant(restaurant_id):
restaurantToDelete = session.query(Restaurant).filter_by(
id=restaurant_id).one()
if request.method == 'POST':
session.delete(restaurantToDelete)
session.commit()
flash(F"Restaurant {restaurantToDelete.name} was deleted!")
return redirect(url_for('showRestaurants'))
else:
return render_template('deleteRestaurant.html',
restaurant=restaurantToDelete)
# Show a restaurant Menu
@app.route('/restaurants/<int:restaurant_id>/')
@app.route('/restaurants/<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).all()
return render_template('showMenu.html', restaurant=restaurant, items=items)
# Create a new menu item
@app.route('/restaurants/<int:restaurant_id>/menu/new/',
methods=['GET', 'POST'])
def newMenuItem(restaurant_id):
if request.method == 'POST':
newItem = MenuItem(
name=request.form['name'],
restaurant_id=restaurant_id,
price=request.form['price'],
description=request.form['description'],
course=request.form['course']
)
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)
# Edit a menu item
@app.route('/restaurants/<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']
if request.form['price']:
editedItem.price = request.form['price']
if request.form['description']:
editedItem.description = request.form['description']
if request.form['course']:
editedItem.course = request.form['course']
session.add(editedItem)
session.commit()
flash("menu item updated!")
return redirect(url_for('showMenu', restaurant_id=restaurant_id))
else:
return render_template('editmenuitem.html',
restaurant_id=restaurant_id,
menu_id=menu_id,
item=editedItem
)
# Delete a menu item
@app.route('/restaurants/<int:restaurant_id>/menu/<int:menu_id>/delete/',
methods=['GET', 'POST'])
def deleteMenuItem(restaurant_id, menu_id):
restaurant = session.query(Restaurant).filter_by(id=restaurant_id).one()
menuToDelete = session.query(MenuItem).filter_by(id=menu_id).one()
if request.method == 'POST':
session.delete(menuToDelete)
session.commit()
flash("Menu item deleted!")
return redirect(url_for('showMenu', restaurant_id=restaurant_id))
else:
return render_template('deleteMenuItem.html',
restaurant_id=restaurant_id,
item=menuToDelete,
restaurant=restaurant)
if __name__ == '__main__':
app.secret_key = 'super_secret_key'
app.debug = True
app.run(host='0.0.0.0', port=5000)