-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathapp.py
107 lines (95 loc) · 3.36 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
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
from flask import Flask, render_template, request, jsonify
import sqlite3 as sql
app = Flask(__name__)
DATABASE_FILE = "database.db"
DEFAULT_BUGGY_ID = "1"
BUGGY_RACE_SERVER_URL = "http://rhul.buggyrace.net"
#------------------------------------------------------------
# the index page
#------------------------------------------------------------
@app.route('/')
def home():
return render_template('index.html', server_url=BUGGY_RACE_SERVER_URL)
#------------------------------------------------------------
# creating a new buggy:
# if it's a POST request process the submitted data
# but if it's a GET request, just show the form
#------------------------------------------------------------
@app.route('/new', methods = ['POST', 'GET'])
def create_buggy():
if request.method == 'GET':
return render_template("buggy-form.html")
elif request.method == 'POST':
msg=""
try:
qty_wheels = request.form['qty_wheels']
msg = f"qty_wheels={qty_wheels}"
with sql.connect(DATABASE_FILE) as con:
cur = con.cursor()
cur.execute("UPDATE buggies set qty_wheels=? WHERE id=?", (qty_wheels, DEFAULT_BUGGY_ID))
con.commit()
msg = "Record successfully saved"
except:
con.rollback()
msg = "error in update operation"
finally:
con.close()
return render_template("updated.html", msg = msg)
#------------------------------------------------------------
# a page for displaying the buggy
#------------------------------------------------------------
@app.route('/buggy')
def show_buggies():
con = sql.connect(DATABASE_FILE)
con.row_factory = sql.Row
cur = con.cursor()
cur.execute("SELECT * FROM buggies")
record = cur.fetchone();
return render_template("buggy.html", buggy = record)
#------------------------------------------------------------
# a page for displaying the buggy
#------------------------------------------------------------
@app.route('/new')
def edit_buggy():
return render_template("buggy-form.html")
#------------------------------------------------------------
# get JSON from current record
# this is still probably right, but we won't be
# using it because we'll be dipping diectly into the
# database
#------------------------------------------------------------
@app.route('/json')
def summary():
con = sql.connect(DATABASE_FILE)
con.row_factory = sql.Row
cur = con.cursor()
cur.execute("SELECT * FROM buggies WHERE id=? LIMIT 1", (DEFAULT_BUGGY_ID))
return jsonify(
{k: v for k, v in dict(zip(
[column[0] for column in cur.description], cur.fetchone())).items()
if (v != "" and v is not None)
}
)
#------------------------------------------------------------
# delete the buggy
# don't want DELETE here, because we're anticipating
# there always being a record to update (because the
# student needs to change that!)
#------------------------------------------------------------
@app.route('/delete', methods = ['POST'])
def delete_buggy():
try:
msg = "deleting buggy"
with sql.connect(DATABASE_FILE) as con:
cur = con.cursor()
cur.execute("DELETE FROM buggies")
con.commit()
msg = "Buggy deleted"
except:
con.rollback()
msg = "error in delete operation"
finally:
con.close()
return render_template("updated.html", msg = msg)
if __name__ == '__main__':
app.run(debug = True, host="0.0.0.0")