This repository has been archived by the owner on Apr 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathapp.py
69 lines (48 loc) · 1.86 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
import os
from flask import Flask, redirect, render_template
from helpers import get_data, random_class, random_code
# Configure application
app = Flask(__name__)
# Ensure templates are auto-reloaded on change
app.config["TEMPLATES_AUTO_RELOAD"] = True
# Get data using helper function get_data()
data = get_data('data.json')
# Ensure responses aren't cached
@app.after_request
def after_request(response):
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
response.headers["Expires"] = 0
response.headers["Pragma"] = "no-cache"
return response
@app.route('/')
def index():
""" Show status-code per responses class """
return render_template('index.html', data = data)
@app.route('/status', strict_slashes=False)
def status():
""" Redirect to a random status-code page """
# Get random status-code
code = random_code(random_class(data), data)
# Redirect to random status-code page
return redirect('/status/{}'.format(code))
@app.route('/status/<code>', strict_slashes=False)
def status_code(code):
""" Show status-code information """
# Set code number as index
i = code
# Loop through status code classes
for k, v in data.items():
# Check if current code is part of this class status codes
if i in v['status']:
code = v['status'][i]
code.update({ 'code': i })
code.update({ 'classname': k})
code.update({ 'class_definition': v['definition']})
return render_template('status-code.html', code = code)
# If code is Not Found
return render_template('apology.html')
# Get setup so that if we call the app directly (and it isn't being imported elsewhere)
# it will then run the app with the debug mode as True
# More info - https://docs.python.org/3/library/__main__.html
if __name__ == '__main__':
app.run()