forked from InsightDataScience/systems-puzzle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
39 lines (29 loc) · 1.33 KB
/
Copy pathapp.py
File metadata and controls
39 lines (29 loc) · 1.33 KB
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
import datetime
import os
from flask import Flask, render_template, redirect, url_for
from forms import ItemForm
from models import Items
from database import db_session
app = Flask(__name__)
app.secret_key = os.environ['APP_SECRET_KEY']
@app.route("/", methods=('GET', 'POST'))
def add_item():
form = ItemForm()
if form.validate_on_submit():
item = Items(name=form.name.data, quantity=form.quantity.data, description=form.description.data, date_added=datetime.datetime.now())
db_session.add(item)
db_session.commit()
return redirect(url_for('success'))
return render_template('index.html', form=form)
@app.route("/success")
def success():
results = []
head = '<head> <style> table { font-family: arial, sans-serif; border-collapse: collapse; width: 100%; } \
td, th { border: 1px solid #dddddd; text-align: left; padding: 8px; } tr:nth-child(even) { background-color: #dddddd; } </style> </head>'
qry = db_session.query(Items)
results = qry.all()
ret = '<html>{:s}<body><table>{:s}</table></body></html>'.format( head,
''.join( [ '<tr><th>{:s}</th><th>{:s}</th><th>{:s}</th><th>{:s}</th></tr>'.format(x.name, str(x.quantity), x.description, str(x.date_added) ) for x in results ] ) )
return str( ret )
if __name__ == '__main__':
app.run(host='0.0.0.0')