-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
77 lines (72 loc) · 3.06 KB
/
app.py
File metadata and controls
77 lines (72 loc) · 3.06 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
from flask import Flask, render_template, redirect,request, session
from flask_session import Session
from departureClasses import getDepartures,departure
from markupsafe import escape
from datetime import datetime
#Command to run the app is: python -m flask --app app --debug run
#Activating virtual environment departureDash\scripts\activate
app = Flask(__name__)
app.config["SECRET_KEY"] = "any random string"
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)
retrieveDepartures = getDepartures()
#app.jinja_env.globals.update(search=search)
@app.route("/")
def index():
now = datetime.now()
current_time = now.strftime("%H:%M")
#If the API call for the randomly selected station fails we'll simply try it again by redirecting to the index route again
try:
randomStation,randomDepartures = retrieveDepartures.getRandomDepartures()
except:
return redirect('/')
session['randomStation'] = randomStation
return render_template("index.html",a_variable = "No station selected",departures = randomDepartures,length = len,order = orderConvert, enumerate = enumerate, str = str, time = current_time)
@app.route("/<requestedStation>")
def displayDepartures(requestedStation):
requestedStation = escape(requestedStation)
requestedStationsDepartures = retrieveDepartures.query(requestedStation.upper())
session['currentStationCode'] = retrieveDepartures.convertStationToCode(requestedStation)
session['currentStation'] = retrieveDepartures.convertCodeToStation(requestedStation)
return render_template("board.html",departures = requestedStationsDepartures, stationCode = retrieveDepartures.getStation(),length = len,stationTitle = requestedStation.upper(),order = orderConvert, enumerate = enumerate, str = str)
@app.route('/board',methods = ['POST', 'GET'])
def result():
if request.method == 'POST':
result = request.form
json_result = dict(result)
print(json_result)
requestedStation = escape(json_result.get("stationBox"))
return redirect(f'/{requestedStation}')
@app.route('/handleOptions',methods=['POST'])
def handle_options():
if request.method == 'POST':
result = request.form
json_result = dict(result)
if json_result.get("button") == "home":
return redirect("/")
else:
return redirect(f'/{json_result.get("button")}')
@app.errorhandler(404)
def page_not_found(error):
return redirect('/XXX')
@app.route('/width', methods=['POST'])
def width():
if request.method == 'POST':
result = request.form
json_result = dict(result)
print(json_result)
session['width'] = float(str(json_result.get('width'))) // 10
return ('', 204)
def orderConvert(n):
num = str(n)
if num == "11" or num == "12" or num == "13":
return num+"th"
if num[-1] == "1":
return num+"st"
if num[-1] == "2":
return num+"nd"
if num[-1] == "3":
return num+"rd"
else:
return num+"th"