-
Notifications
You must be signed in to change notification settings - Fork 0
/
lotsofmenus.py
73 lines (63 loc) · 3.15 KB
/
lotsofmenus.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
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 Restaurant, Base, MenuItem
engine = create_engine('sqlite:///restaurantmenu.db')
Base.metadata.bind = engine
DBSession = sessionmaker(bind=engine)
session = DBSession()
@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 menuItemJSON(restaurant_id, menu_id):
menuItem = session.query(MenuItem).filter_by(id = menu_id).one()
items = session.query(MenuItem).filter_by(restaurant_id = restaurant_id).all()
return jsonify(MenuItems=menuItem.serialize)
@app.route('/restaurants/<int:restaurant_id>/menu/')
def restaurantMenu(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, restaurant_id = restaurant_id)
@app.route('/restaurant/<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()
flash("new menu item created!")
return redirect(url_for('restaurantMenu', restaurant_id = restaurant_id))
else:
return render_template('newmenuitem.html',restaurant_id = restaurant_id)
@app.route('/restaurants/<int:restaurant_id>/<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['name']
editedItem.price = request.form['price']
editedItem.course = request.form['course']
session.add(editedItem)
session.commit()
flash("new menu item edited!")
return redirect(url_for('restaurantMenu', restaurant_id = restaurant_id))
else:
return render_template('editmenuitem.html',restaurant_id = restaurant_id, menu_id = menu_id, i = editedItem)
@app.route('/restaurants/<int:restaurant_id>/<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("new menu item deleted!")
return redirect(url_for('restaurantMenu', restaurant_id = restaurant_id))
else:
return render_template('deletemenuitem.html', i = itemToDelete)
if __name__ == '__main__':
app.secret_key = 'secret_key'
app.debug = True
app.run(host = '0.0.0.0', port = 5000)