-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
147 lines (118 loc) · 4.77 KB
/
main.py
File metadata and controls
147 lines (118 loc) · 4.77 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
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
from flask import Flask, render_template, request, jsonify
from datastore import Datastore
from validation import Validator
from features import FareCalculator, NearestBus
# Initialize datastore
datastore = Datastore("busdata.db")
datastore.init_all()
# Initialize app features
nearestbus = NearestBus(datastore)
farecalculator = FareCalculator(datastore)
# Initialize validation
validator = Validator()
app = Flask(__name__)
@app.route("/", methods=["GET", "POST"])
def main():
return render_template("index.html")
@app.route("/nearestbusstop", methods=["GET", "POST"])
def nearestbusstop():
"""Find the nearest bus stop from a given coordinate
Coordinate can be retrieved automatically or
manually entered
NOTE: Rendering a external HTML file is a workaround
to prevent the error from Google Map API where it could
not find the element to load into upon DOM load completion
(aka bus stop search not carried out thus no map can exist)
"""
if request.method == "GET":
return render_template("nearestbusstop.html")
if request.method == "POST":
if validator.inputCheck(request.form):
latitude = request.form["latitudetext"]
longitude = request.form["longitudetext"]
recordmax = request.form["recordmax"]
return jsonify(
{
"data": render_template(
"bustable.html",
results=nearestbus.getBusStops(latitude, longitude, recordmax),
)
}
)
else:
return jsonify(
{
"data": render_template(
"bustable.html",
error="Please input all fields and ensure that they are valid numbers.",
)
}
)
@app.route("/farecalculator", methods=["GET", "POST"])
def farecalculate():
"""Calculate fare given fare type, direction, boarding stop,
alighting stop and service number
NOTE:
1. No need for data validation because no manual input
is needed
2. Due to insufficient data for some fare types (SBST, CITY LINK),
the search will sometimes not work and database search will
return None. In this case, please search another service.
Currently unknown fare types defaults to EXPRESS fare.
3. Did not directly pass variables into functions for more readability
"""
if request.method == "GET":
busservices = datastore.get_records("get_busservices")
return render_template("farecalculator.html", busservices=busservices)
if request.method == "POST":
if request.form["type"] == "getDirections":
serviceno = request.form["serviceno"]
return jsonify(data=farecalculator.getDirections(serviceno))
if request.form["type"] == "getBoardingAt":
direction = request.form["direction"]
serviceno = request.form["serviceno"]
return jsonify(data=farecalculator.getBoardingAt(direction, serviceno))
if request.form["type"] == "getAlightingAt":
direction = request.form["direction"]
serviceno = request.form["serviceno"]
boardingno = request.form["boardingat"]
return jsonify(
data=farecalculator.getAlightingAt(direction, serviceno, boardingno)
)
if request.form["type"] == "addJourney":
faretype = request.form["faretype"]
serviceno = request.form["busserviceno"]
direction = request.form["direction"]
boardingno = request.form["boardingat"]
alightingno = request.form["alightingat"]
if validator.selectCheck(
faretype, serviceno, direction, boardingno, alightingno
):
return jsonify(
{
"data": render_template(
"faretable.html",
results=farecalculator.calculateFare(
faretype, direction, serviceno, boardingno, alightingno
),
)
}
)
else:
return jsonify(
{
"data": render_template(
"faretable.html",
error="Please select a valid option for all fields.",
)
}
)
@app.route("/help", methods=["GET", "POST"])
def help():
return render_template("help.html")
@app.errorhandler(404)
def page_not_found(e):
return "The resource could not be found."
# reminder to set debug to false for production
if __name__ == "__main__":
app.run(debug=True)