-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.py
272 lines (199 loc) · 10.1 KB
/
main.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import os
import json
from flask import (
Flask,
flash,
redirect,
render_template,
session,
url_for,
request)
import gsheets_automate
import find_location
from add_records import add_new_rating_to_db
from models import Locations
# Configure application
app = Flask(__name__)
app.config["SECRET_KEY"] = os.environ.get("KEY")
# ------------------- Google Sheets API Setup ------------------- #
import gspread
from oauth2client.service_account import ServiceAccountCredentials
# # Use credentials to create a client to interact with the Google Drive API
# # Use credentials to create a client to interact with the Google Drive API
# scope = ["https://spreadsheets.google.com/feeds"]
# credentials = ServiceAccountCredentials.from_json_keyfile_name(
# "client_secret.json", scope)
# client = gspread.authorize(credentials)
# # Find a workbook by name and open the first sheet
# sheet = client.open("LocationsAccessMap").sheet2
# print(sheet.get_all_values())
# print(sheet.get_all_values())
@app.route("/", methods=["GET", "POST"])
def home():
return render_template("Home.html")
@app.route("/sign_up", methods=["GET", "POST"])
def sign_up():
"""
Allows a user to register for an account and view maps tailored
to their accessibility interests
"""
if request.method == "POST":
# Get user's input from the form
username = request.form.get("txt")
email = request.form.get("email")
password = request.form.get("pswd")
print(username, email, password)
# TODO: Hash password
# TODO: Save new user to the database
# Redirect to sign up part 2
return redirect(url_for("choose_accessibility_filters"))
return render_template("signup.html")
@app.route("/choose_accessibility_filters", methods=["GET", "POST"])
def choose_accessibility_filters():
"""
Allows the user to see maps customized to their accessibility needs
"""
if request.method == "POST":
# Handle the form submission
print("Form submitted")
# TODO: Save accessibility preferences to user's profile
return redirect(url_for("home"))
return render_template("accessibilityneeds.html")
@app.route("/log_in", methods=["GET", "POST"])
def log_in():
"""Logs an existing user in"""
return render_template("Home.html")
@app.route("/find_a_location", methods=["GET", "POST"])
def find_a_location():
"""Allows a user to search for a location by name or address"""
if request.method == "POST":
print("Looking for location")
# Get form information from the user
location_name = request.form.get("location_name")
address = request.form.get("location_address")
print(f"Location Name: {location_name}",
f"Address: {address}")
print(f"Location Name: {type(location_name)}",
f"Address: {(address)}")
if not location_name and not address:
# User must choose either a location or an address in order to update
# a location's accessibility rating
print(f"Location Name: {location_name}",
f"Address: {address}")
flash("Please add either a location or an address to search for.")
else:
print("Either a location name or an address has been added!")
try:
# If the user searches by location name and not address
if location_name and not address:
# Find similar location names to what the user input using database query
matching_locations = find_location.query_find_by_location_name(location_name)
#If the user searches by an address
if address and not location_name:
# Find similar addresses to what the user input using database query
matching_locations = find_location.query_find_by_address(address)
print(matching_locations)
# Store the matching location in a session variable
session["locations"] = matching_locations
return redirect(url_for("location_list"))
except Exception as e:
flash(f"Unable to find location. Exception: {e}")
return redirect(url_for("find_a_location"))
return render_template("FindLocation.html")
@app.route("/location_list", methods=["GET", "POST"])
def location_list():
"""Shows the user a dropdown of matching locations"""
locations = session.get("locations", [])
print(f"Locations: {locations} ")
if request.method == "POST":
# If the user has made a selection from the dropdown
chosen_location = request.form.get("location")
print(chosen_location)
return redirect(url_for("update_rating"))
# TODO: Index the address or name that matches the unique dropdown ID
# TODO: Edit the row that matches the unique location ID only
# TODO: Redirect to update_rating if successful
return render_template("LocationList.html", locations=locations)
@app.route("/update_rating", methods=["GET", "POST"])
def update_rating():
"""Allows a user to update a location's accessibility rating"""
if request.method == "POST":
print("posted")
# Get form information from the user
location_name = request.form.get("location_name")
address = request.form.get("location_address")
sensory_rating = request.form.get("sensoryrating")
mobility_rating = request.form.get("mobilityrating")
service_dog_relief_rating = request.form.get("sdogreliefrating")
wheelchair_rating = request.form.get("wheelchairrating")
common_allergens_rating = request.form.get("commonallergens")
# Check information
print(f"Sensory Rating: {sensory_rating}",
f"Mobility Rating: {mobility_rating}",
f"Service Dog Relief Rating: {service_dog_relief_rating}",
f"Wheelchair Rating: {wheelchair_rating}",
f"Common Allergens Rating: {common_allergens_rating}")
if not sensory_rating and not mobility_rating and not service_dog_relief_rating \
and not wheelchair_rating and not common_allergens_rating:
# The user must choose to update either a sensory, mobility, service dog relief,
# wheelchair, or common allergens rating
flash("Please add a category rating.")
return redirect(url_for("update_rating"))
else:
# If the user puts an accessibility rating in, update database
print("A category's rating has been updated!")
# TODO: Use new rating to recalculate a location's average accessibility score
## for that category
new_record = Locations(Name=location_name, Address=address, SensoryRating=sensory_rating,
MobilityRating=mobility_rating,
ServiceDogRelief=service_dog_relief_rating, WheelchairAccessible=wheelchair_rating,
CommonAllergenRisk=common_allergens_rating)
print(new_record)
try:
add_new_rating_to_db(new_record)
# Attempt to save the updated score
return redirect(url_for("save_score"))
except Exception as e:
print(f"Couldn't reroute to save the updated score. Exception: {e}")
if not sensory_rating and not mobility_rating and not service_dog_relief_rating \
and not wheelchair_rating and not common_allergens_rating:
# The user must choose to update either a sensory, mobility, service dog relief,
# wheelchair, or common allergens rating
flash("Please add a category rating.")
return redirect(url_for("update_rating"))
else:
# If the user puts an accessibility rating in, update database
print("A category's rating has been updated!")
# TODO: Use new rating to recalculate a location's average accessibility score
## for that category
new_record = Locations(Name=location_name, Address=address, SensoryRating=sensory_rating,
MobilityRating=mobility_rating,
ServiceDogRelief=service_dog_relief_rating, WheelchairAccessible=wheelchair_rating,
CommonAllergenRisk=common_allergens_rating)
print(new_record)
try:
add_new_rating_to_db(new_record)
# Attempt to save the updated score
return redirect(url_for("save_score"))
except Exception as e:
print(f"Couldn't reroute to save the updated score. Exception: {e}")
return redirect(url_for("update_rating"))
return render_template("UpdateRating.html")
@app.route("/save_score", methods=["GET"])
def save_score():
"""Updates the Google Sheet from update_rating"""
try:
# Flask won't update both the database and write to GSheet within the same route.
# But you can call this function to update the spreadsheet for manual re-upload.
gsheets_automate.write_agg_scores_to_gsheets()
# TODO: Insert the new average into the database
# TODO: Display new average on the success page
except Exception as e:
print(f"Can't update spreadsheet. Exception: {e}")
else:
# Redirect to the success page if successful
return redirect(url_for("successfully_posted"))
@app.route("/success", methods=["GET"])
def successfully_posted():
"""Informs the user they have successfully updated the rating"""
return render_template("SuccessfullyUpdated.html")