This repository was archived by the owner on Aug 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb.py
More file actions
194 lines (179 loc) · 6.7 KB
/
web.py
File metadata and controls
194 lines (179 loc) · 6.7 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
from flask import Flask, render_template, request, jsonify
import encrypt
import forms
from forms import Stats
player_name = None
player_name2 = None
template = None
template2 = None
template3 = None
template4 = None
enter = None
enter2 = None
enter3 = False
app = Flask(__name__)
# app.secret_key = "development-key"
@app.route("/", methods=["GET", "POST"])
def hello():
"""
Takes data input to entry_menu.html and adds it to the DB
also logs visitors to site
:return:
"""
if request.method == "POST":
entry_data = []
temp = []
# data entry, encrypted with shift of 5, chosen by fair dice roll, guaranteed random
coredb = open("data"+"/coredb.txt", "a")
entry_data.append(encrypt.shift_encode(request.form["player"], 5))
entry_data.append(encrypt.shift_encode(request.form["venue"], 5))
entry_data.append(encrypt.shift_encode(request.form["pony"], 5))
entry_data.append(encrypt.shift_encode(request.form["result"], 5))
entry_data.append(encrypt.shift_encode(request.form["conditions"], 5))
# adds . for separation
for i in range(0, len(entry_data)):
for j in range(0, len(entry_data[i])):
temp.append(entry_data[i][j])
temp.append('.')
# temp.append('.')
input_data = ''.join(temp)
# validation
valid = Validation.validate(input_data)
error = valid
if valid:
coredb.write(input_data + '\n')
return render_template('entry_menu.html', error=error), 200
elif request.method == "GET":
# logging
with open("data"+"/visitors.txt", "a") as file:
file.write('ip: ' + request.environ['REMOTE_ADDR'] + " " + str(jsonify({'ip': request.remote_addr})) + "\n")
return render_template('entry_menu.html')
return render_template('entry_menu.html', error=True)
@app.errorhandler(404)
def page_not_found(error):
"""
handles 404 errors, returning page_not_found.html
:param error:
:return:
"""
# logs error
try:
with open("data" + "/visitors.txt", "a") as file:
file.write('ip: ' + request.environ['REMOTE_ADDR'] + "\n" + error)
except TypeError:
pass
return render_template('page_not_found.html'), 404
@app.route("/stats", methods=["POST", "GET"])
def stats():
"""
handles data_menu.html
takes form input on page and gets data from the DB
:return:
"""
if request.method == "POST":
# globals
global template3
template3 = None
global player_name
global player_name2
global enter3
enter3 = False
if 'search' in request.form:
# if first part of check, where only one box is visible
player_name = request.form['player_name']
mean, median, max_val, min_val, dev, error = Stats.single_variable_stats(player_name, 0)
if error:
return render_template('page_not_found.html'), 404
else:
global template
template = [mean, median, max_val, min_val, dev, player_name]
global enter
enter = True
return render_template('data_menu.html', template=template, template3=template3,
enter=True, enter3=enter3)
elif 'compare' in request.form:
# if second name has been entered this handles the second search
player_name2 = request.form['player_name2']
# stats = Stats('pony')
mean, median, max_val, min_val, dev, error = Stats.single_variable_stats(player_name2, 0)
if error:
return render_template('page_not_found.html'), 404
else:
global template2
template2 = [mean, median, max_val, min_val, dev, player_name2]
global enter2
enter2 = True
elif 'query' in request.form:
# if second variable comparison has been chosen, this processes handles the POST
option = request.form['options']
# chooses flag
if option == 'pony':
flag = 2
elif option == 'venue':
flag = 1
elif option == 'conditions':
flag = 4
else:
return render_template('page_not_found.html'), 404
query = request.form['query']
# handles weather conditions
if 'rain' in query:
query = '1'
elif 'snow' in query:
query = '2'
elif 'hot' in query:
query = '3'
elif 'clear' in query:
query = '0'
# calculates stats
mean, median, max_val, min_val, dev, error = forms.double_variable_stats(player_name, query, flag)
if error:
return render_template('page_not_found.html'), 404
else:
query = request.form['query']
template3 = [mean, median, max_val, min_val, dev, query]
if enter2:
global template4
if 'rain' in query:
query = '1'
elif 'snow' in query:
query = '2'
elif 'hot' in query:
query = '3'
elif 'clear' in query:
query = '0'
mean, median, max_val, min_val, dev, error = forms.double_variable_stats(player_name2, query, flag)
if flag == 4:
query = request.form['query']
template4 = [mean, median, max_val, min_val, dev, query]
enter3 = True
# final part 3 variables sent to HTML
return render_template('data_menu.html', template=template, template2=template2, template3=template3,
template4=template4, enter=enter, enter2=enter2, enter3=enter3)
else:
# if request was a GET, sends basic HTML to client
return render_template('data_menu.html', enter=False, enter2=False)
@app.route("/robots.txt")
def robots():
"""
handles robots.txt
:return:
"""
return render_template('robots.txt')
class Validation:
@staticmethod
def validate(data_set):
"""
data validation for input, checks if all fields are filled
:param data_set:
:return:
"""
valid = True
# checks for missing data
for i in range(0, len(data_set)):
# returns False if 2 . next to each other, indicating a data point has not been entered
if data_set[i] == "." and data_set[i-1] == ".":
valid = False
return valid
if __name__ == "__main__":
app.run(debug=True)