-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.py
executable file
·249 lines (185 loc) · 8.64 KB
/
index.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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#!/usr/bin/python
'''index.py
CS 257 Python Web App, Fall 2013, Jeff Ondich
Jiatao Cheng, Erin Wilson, and Adam Canady
This is the code that creates our finalized web app's backend.
Some of the code from this file was derived from Jeff Ondich's tinywebapp.py
'''
import cgi
import cgitb
cgitb.enable()
import datasource
import templates
gen = templates.Templates()
print "Content-type: text/html\r\n\r\n"
# Get the user input
form = cgi.FieldStorage()
# If a form was submitted, do something about it!
if 'form_type' in form:
try:
# Define the database and connect
db = datasource.DataSource()
# Displays all available rooms
if form['form_type'].value == "available_rooms":
title = "Available Rooms"
# Get inputs
building = form['building'].value
if building == "no_preference":
building = ""
environment = form['environment'].value
if environment == "no_preference":
environment = ""
occupancy = int(form['occupancy'].value)
# Query DB for info
result = db.get_rooms_by_preference(building, occupancy, environment)
# Build output
content = gen.make_table(result, ['Average Draw Number', 'Building', 'Room Number'])
gen_page = gen.make_results_page(title, content)
# Send output
print gen_page
# Displays rooms and likeliness given user's draw number
if form['form_type'].value == "which_rooms":
title = "Here's a list of rooms you could get:"
# Get inputs
draw_number = int(form['draw_number'].value)
converted_draw_number = db.convert_number(draw_number)
stretch_number = converted_draw_number - 30
safety_number = converted_draw_number + 30
# Query DB for info
stretch = db.get_rooms_in_range(0, stretch_number)
target = db.get_rooms_in_range(stretch_number, safety_number)
safety = db.get_rooms_in_range(safety_number, 10000)
# Build output
headers = ["Average Draw Number", "Building", "Room", "Occupancy", "Sub Free?", "Quiet?"]
stretch_table = gen.make_table(stretch, headers)
target_table = gen.make_table(target, headers)
safety_table = gen.make_table(safety, headers)
content = ""
if stretch:
content += "<h3>Stretch Rooms</h3>\n"
content += stretch_table
if target:
content += "<h3>Target Rooms</h3>\n"
content += target_table
if safety:
content += "<h3>Safety Rooms</h3>\n"
content += safety_table
gen_page = gen.make_results_page(title, content)
# Send output
print gen_page
# Displays likeliness of a user drawing their dream room
if form['form_type'].value == "dream_room":
title = "Dream Room Results"
# Get inputs
room_number = int(form['room_number'].value)
draw_number = int(form['draw_number'].value)
converted_draw_number = db.convert_number(draw_number)
building = form['building'].value
# Query DB for info
result = db.specific_room_possibility(converted_draw_number, room_number, building)
if result == "Stretch":
content = "This room would be a STRETCH room given your draw number."
elif result == "Target":
content = "This room would be a TARGET room given your draw number."
elif result == "Safety":
content = "This room would be a SAFETY room given your draw number."
elif result == "Draw Not Available":
content = "Either this room is not availabe for drawing, or we don't have data on it"
gen_page = gen.make_results_page(title, content)
# Send output
print gen_page
# Displays lists of rooms that match user indicated preferences and
# the likelihood of getting those rooms
if form['form_type'].value == "room_like_this":
title = "Here are some rooms that fit your preferences:"
# Get inputs
draw_number = int(form['draw_number'].value)
converted_draw_number = db.convert_number(draw_number)
environment = form['environment'].value
if environment == "no_preference":
environment = ""
occupancy = int(form['occupancy'].value)
building = form['building'].value
if building == "no_preference":
building = ""
# Query DB for info
stretch, target, safety = db.get_rooms_like_this(converted_draw_number, environment, occupancy, building)
# Build output
headers = ['Average Draw Number', 'Building', 'Room']
stretch_table = gen.make_table(stretch, headers)
target_table = gen.make_table(target, headers)
safety_table = gen.make_table(safety, headers)
content = ""
if stretch:
content += "<h3>Stretch Rooms</h3>\n"
content += stretch_table
if target:
content += "<h3>Target Rooms</h3>\n"
content += target_table
if safety:
content += "<h3>Safety Rooms</h3>\n"
content += safety_table
gen_page = gen.make_results_page(title, content)
# Send output
print gen_page
# Displays lists of rooms within 250m of user's favorite location and
# the likelihood of getting those rooms
if form['form_type'].value == "best_room_near_location":
title = "Here are some rooms near " + form['favorite_location'].value
# Get inputs
draw_number = int(form['draw_number'].value)
converted_draw_number = db.convert_number(draw_number)
favorite_location = form['favorite_location'].value
# Query DB for info
stretch, target, safety = db.get_rooms_near_location(converted_draw_number, favorite_location)
# Build output
headers = ["Average Draw Number", "Building", "Room", "Occupancy", "Sub Free?", "Quiet?"]
stretch_table = gen.make_table(stretch, headers)
target_table = gen.make_table(target, headers)
safety_table = gen.make_table(safety, headers)
content = ""
if stretch:
content += "<h3>Stretch Rooms</h3>\n"
content += stretch_table
if target:
content += "<h3>Target Rooms</h3>\n"
content += target_table
if safety:
content += "<h3>Safety Rooms</h3>\n"
content += safety_table
gen_page = gen.make_results_page(title, content)
# Send output
print gen_page
# Displays rooms outside of a 250m radius of an enemy's target room
if form['form_type'].value == "mortal_enemy":
title = "Mortal Enemy Avoider"
# Get inputs
enemy_number = int(form['enemy_number'].value)
converted_enemy_number = db.convert_number(enemy_number)
draw_number = int(form['draw_number'].value)
converted_draw_number = db.convert_number(draw_number)
# Query DB for info
enemy_target_room, stretch, target, safety = db.get_rooms_far_away(converted_enemy_number, converted_draw_number)
# Build output
content = "<h2>Your mortal enemy will probably choose %s %s.</h2>\n" % (enemy_target_room[0], enemy_target_room[1])
content += "<h3>So you should choose one of the following:</h3>\n"
headers = ["Average Draw Number", "Building", "Room", "Occupancy", "Sub Free?", "Quiet?"]
stretch_table = gen.make_table(stretch, headers)
target_table = gen.make_table(target, headers)
safety_table = gen.make_table(safety, headers)
if stretch:
content += "<h3>Stretch Rooms</h3>\n"
content += stretch_table
if target:
content += "<h3>Target Rooms</h3>\n"
content += target_table
if safety:
content += "<h3>Safety Rooms</h3>\n"
content += safety_table
gen_page = gen.make_results_page(title, content)
# Send output
print gen_page
except:
print gen.get_error_page()
else:
print gen.get_start_page()