forked from tanisman/ege-cloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
71 lines (53 loc) · 2.11 KB
/
app.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
from time import sleep
from flask import Flask, stream_with_context, request, Response, flash, render_template, redirect, url_for
from database import Database
app = Flask(__name__)
app.secret_key = '!$w4wW~o|~8OVFX' # !!change this with random key!!
def stream_template(template_name, **context):
app.update_template_context(context)
t = app.jinja_env.get_template(template_name)
rv = t.stream(context)
rv.disable_buffering()
return rv
@app.route('/')
def index():
return render_template("index.html")
@app.route('/list')
def list_items():
def generate(): # our generator for list items
db = Database()
with db.get_cursor() as cursor:
cursor.execute("SELECT * FROM inventory;")
rows = cursor.fetchall()
for row in rows:
# yield returns iterable handle of this loop
yield {"id": int(row[0]), "name": str(row[1]), "qty": int(row[2])}
'''
this is just an example for 'flash' usage
in practical usage this operation would result query twice
for 1 listing operation
'''
count = sum(1 for item in generate()) # count generator
flash("Loaded {} items from database".format(count)) # show message
# stream with context helps us to return iterable as response
return Response(stream_with_context(stream_template("list.html", rows=generate())))
@app.route('/add', methods=("GET", "POST"))
def add_item():
if request.method == "POST": # add item
item_name = request.form["item_name"]
qty = request.form["quantity"]
error = None
if not item_name:
error = "Item Name is required"
elif not qty:
error = "Quantity is required"
if error is None:
db = Database()
with db.get_cursor() as cursor:
cursor.execute("INSERT INTO inventory (name, quantity) VALUES (%s, %s);", (item_name, qty))
db.commit()
return redirect(url_for("list_items"))
flash(error)
return render_template("add.html") # serve page
if __name__ == '__main__':
app.run()