-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.py
488 lines (345 loc) · 16.8 KB
/
server.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
"""Server for landlord review app."""
# || Imports
from flask import (Flask, render_template, request, flash, session,
redirect, jsonify)
from model import connect_to_db
import crud
from jinja2 import StrictUndefined
import os
import requests
from collections import OrderedDict
import operator
app = Flask(__name__)
# app.secret_key = os.environ.get('secret_key')
# print(app.secret_key)
app.jinja_env.undefined = StrictUndefined
####### Routes for Rendering HTML Pages
@app.route('/')
def render_homepage():
return render_template('homepage.html')
@app.route('/user_login')
def render_login():
return render_template('login.html')
@app.route('/write_review')
def render_write_reviews():
return render_template('write_review.html')
@app.route('/new_user')
def render_new_user():
return render_template('new_user.html')
####### Routes for Viewing All Entries for each DB Class
@app.route('/reviews')
def all_reviews():
"""Show all reviews by calling get_reviews function"""
reviews = crud.get_reviews()
return render_template('all_reviews.html', reviews=reviews)
@app.route('/landlords')
def all_landlords():
"""Show all reviews by calling get_landlords function"""
landlords = crud.get_landlords()
return render_template('all_landlords.html', landlords=landlords)
@app.route('/buildings')
def all_buildings():
"""Show all buildings by calling get_buildings function"""
buildings = crud.get_buildings()
return render_template('all_buildings.html', buildings=buildings)
@app.route('/users')
def all_users():
"""Show all users by calling get_users function."""
users = crud.get_users()
return render_template('all_users.html', users=users)
####### Routes for Viewing Individual Entry for each DB Class
@app.route('/buildings/<building_id>')
def show_building(building_id):
"""Show details of a particular building"""
building = crud.get_building_by_id(building_id)
return render_template('building_details.html', building=building)
@app.route('/landlords/<landlord_id>')
def show_landlord(landlord_id):
"""Show details of a particular landlord"""
landlord = crud.get_landlord_by_id(landlord_id)
return render_template('landlord_details.html', landlord=landlord)
@app.route('/users/<user_id>')
def show_user(user_id):
"""Show details of a particular user"""
user = crud.get_user_by_id(user_id)
return render_template('user_details.html', user=user)
@app.route('/reviews/<review_id>')
def show_review(review_id):
"""Show details of a particular review"""
review = crud.get_review_by_id(review_id)
return render_template('review_details.html', review=review)
####### Routes for User Creation
@app.route("/new_user", methods=['POST'])
def register_user():
"""Create a new user"""
email = request.form.get("email")
username = request.form.get("username")
password = request.form.get("password")
user = crud.get_user_by_email(email)
get_username = crud.get_user_by_username(username)
if user:
flash(u"""Can't create account. Account with this email already
exists. Please try again.""", "error")
return redirect("/new_user")
elif get_username:
flash (u"""Account with this username already exists. Please enter
another username and try again.""", "error")
return redirect("/new_user")
else:
crud.create_user(email, username, password)
flash(u"Account created! Please log in.", "success")
return redirect("/user_login")
####### Routes for Login/Logout
@app.route("/login", methods=["POST"])
def login():
"""Allow existing users to log in"""
email = request.form.get("email")
password = request.form.get("password")
user = crud.get_user_by_email_and_password(email, password)
if user:
session["user_email"] = user.email
session["user_id"] = user.user_id
flash(f"Welcome back, {user.username}. You are now logged in.", "success")
return "None"
else:
return "Please enter correct email and password or register for a new account."
@app.route("/logout")
def logout():
"""Allow currently logged in user to log out"""
if "user_id" in session and "user_email" in session:
del session["user_id"]
del session["user_email"]
flash(u"You are now logged out.", "success")
return redirect('/user_login')
else:
flash(u"You are not currently logged in.", "error")
return redirect('/user_login')
####### Routes for Reviews
@app.route('/write_review', methods=['POST'])
def write_review():
"""If user logged in, allow submission from write review form on homepage.html
to be added to reviews database"""
logged_in_email = session.get("user_email")
reviewed_landlord = request.form.get("landlord")
reviewed_building = request.form.get("building")
written_review = request.form.get("review_body")
landlord_office = request.form.get("landlord_office")
if logged_in_email is None:
flash(u"You must log in to review a landlord.", "error")
return redirect('/user_login')
elif not reviewed_landlord:
flash(u"You didn't enter the Landlord for your review.", "error")
elif not reviewed_building:
flash(u"You didn't enter the Address for your review.", "error")
elif not written_review:
flash(u"You didn't enter the Review Text for your review.", "error")
else:
user = crud.get_user_by_email(logged_in_email)
# Pass reviewed building address to NYC Geosearch API to return
# housenumber, street and postalcode variables
url = "https://geosearch.planninglabs.nyc/v1/autocomplete"
payload = {"text": reviewed_building}
res = requests.get(url, params=payload)
data = res.json()
features = data["features"]
properties = features[0]["properties"]
housenumber = properties["housenumber"]
streetname = properties["street"]
postalcode = properties["postalcode"]
building = crud.get_building_by_address(housenumber,
streetname,
postalcode)
landlord = crud.get_landlord_by_name(reviewed_landlord)
# If both not in database, create landlord and building entries
# in respective databases
if not building and not landlord:
landlord = crud.create_landlord(reviewed_landlord, landlord_office)
building = crud.create_building(housenumber, streetname,
postalcode, landlord.landlord_id)
elif not building and landlord:
building = crud.create_building(housenumber, streetname,
postalcode, landlord.landlord_id)
elif not landlord and building:
landlord = crud.create_landlord(reviewed_landlord, landlord_office)
crud.create_review(written_review, user, building)
flash(f"""You wrote a review of {building.building_housenumber} {building.building_streetname}.
Thanks for your review submission!""", "success")
return redirect('/')
####### Routes for Search
@app.route("/search_address")
def search_nyc_address():
"""Takes in user input from html form and makes API call to NYC Geosearch
based on user input. Then passes API response to crud function to
check if entry exists in database. If exists, return details page
for that building."""
text = request.args.get("search_nyc_address")
if text is None:
flash(u"You must enter an address to search.", "error")
return redirect('/')
url = "https://geosearch.planninglabs.nyc/v1/autocomplete"
payload = {"text": text}
res = requests.get(url, params=payload)
data = res.json()
features = data["features"]
coordinates = features[0]["geometry"]["coordinates"]
print("!!!!!!*****<<<>>>>>>!!!!!!")
print(f"coordinates = {coordinates}")
properties = features[0]["properties"]
searched_housenumber = properties["housenumber"]
searched_streetname = properties["street"]
searched_postalcode = properties["postalcode"]
borough = properties["borough"]
building = crud.get_building_by_address(searched_housenumber, searched_streetname, searched_postalcode)
violation_list = crud.get_violation_by_address(searched_housenumber, searched_streetname,
searched_postalcode)
registrations = crud.get_hpdregistration_by_address(searched_housenumber, searched_streetname,
searched_postalcode)
# Sort violation list by inspectiondate attribute
violation_list.sort(key=lambda violation: violation.inspectiondate, reverse=True)
contacts = []
if registrations:
for registration in registrations:
contact = crud.get_hpdcontact_by_registration(registration.registrationid)
contacts.append(contact)
if contacts:
contact_list = contacts[0]
violations = {}
for violation in violation_list:
if violation.violation_class in violations:
violations[violation.violation_class] +=1
else:
violations[violation.violation_class] = 1
# Order dictionary keys alphabetically
# violations = OrderedDict(sorted(list(violations.keys())))
# violations = dict( sorted(violations.items(), key=operator.itemgetter(1),reverse=True))
violation_counts = list(violations.values())
print("~~~~~~~~~~~~~~~~~~~")
print(violation_counts)
violation_types = list(violations.keys())
violation_types.sort()
# violation_types = violation_types.sorted()
violation_types = '['+','.join(['"'+x+'"' for x in violation_types])+']'
print("~~~~~~~~~~~~~~~~~~~")
print(violation_types)
violation_dates = {}
for violation in violation_list:
if violation.inspectiondate.year in violation_dates:
violation_dates[violation.inspectiondate.year] +=1
else:
violation_dates[violation.inspectiondate.year] = 1
# print("!!!!!!*****<<<>>>>>>!!!!!!")
# print(f"violation_dates = {violation_dates}")
violation_year_counts = list(violation_dates.values())
violation_years = list(violation_dates.keys())
# data = '{"labels":' + f'{violation_types}' + ', "datasets":[{"data":' + f'{violation_counts}' + '}]}'
data = '{"labels":' + f'{violation_types}' + ', "datasets":[{"data":' + f'{violation_counts}' + ', "backgroundColor":["#358f49", "#E8A33C", "#E74C3C", "#414f7a"] }]}'
bar_data = '{"labels":' + f'{violation_years}' + ', "datasets":[{"data":' + f'{violation_year_counts}' + ', "backgroundColor":["#8c84a1"] }] }'
# print("!!!!!!!!!!!!!!!!!!!!!")
# print(data)
# "datasets":[{"data":[2, 4, 8]}]
# "labels":["does", "this", "work"],
if building is None and not violation_list:
flash(u"No reviews or violations exist for that address.", "error")
return redirect('/')
elif building is None and violation_list:
length_violation_list = len(violation_list)
return render_template("violation_details.html", violation_list=violation_list,
length_violation_list=length_violation_list, data=data, registrations=registrations,
contact_list=contact_list, bar_data=bar_data)
# Add data=data if rendering chart.js in violation_details.html
elif not violation_list and building:
return render_template('building_details.html', building=building,
violation_list=violation_list)
length_violation_list = len(violation_list)
return render_template('building_details.html', building=building, borough=borough,
violation_list=violation_list, length_violation_list=length_violation_list,
data=data, registrations=registrations, contact_list=contact_list, bar_data=bar_data)
# @app.route("/search_by_address")
# def search_by_building_and_violation():
# """Takes in user input from html form and passes to crud function to
# check if entry exists in database. If exists, return details page
# for that building."""
# searched_housenumber = request.args.get("search_review_by_housenumber")
# searched_streetname = request.args.get("search_review_by_streetname")
# searched_postalcode = request.args.get("search_review_by_postalcode")
# if searched_housenumber is None or searched_streetname is None or searched_postalcode is None:
# flash(u"You must enter a complete address to search.", "error")
# return redirect('/')
# else:
# building = crud.get_building_by_address(searched_housenumber, searched_streetname, searched_postalcode)
# violation_list = crud.get_violation_by_address(searched_housenumber, searched_streetname,
# searched_postalcode)
# if building is None and not violation_list:
# flash("No reviews or violations exist for that address.")
# return redirect('/')
# elif building is None and violation_list:
# length_violation_list = len(violation_list)
# return render_template("violation_details.html", violation_list=violation_list,
# length_violation_list=length_violation_list)
# elif not violation_list and building:
# return render_template('building_details.html', building=building,
# violation_list=violation_list)
# length_violation_list = len(violation_list)
# return render_template('building_details.html', building=building,
# violation_list=violation_list, length_violation_list=length_violation_list)
@app.route("/search_by_landlord")
def search_by_landlord():
"""Takes in user input from html form, passes to crud function to check
if exists in database. If exists, return details page for that landlord."""
searched_landlord = request.args.get("search_landlord")
# print(f"searched_landlord = {searched_landlord}")
if searched_landlord is None:
flash(u"You must enter a landlord to search.", "error")
return redirect('/')
else:
landlord = crud.get_landlord_by_name(searched_landlord)
if landlord is None:
flash("No reviews exist for that landlord.")
return redirect("/")
return render_template("landlord_details.html", landlord=landlord)
@app.route("/search_hpdviolations_by_address")
def search_violations_by_address():
"""Takes in user input from html form, breaks into house number, street name,
and postal code and then queries hpd_violations database. If address exists
in database, return associated violation objects."""
searched_housenumber = request.args.get("search_hpd_by_housenumber")
searched_streetname = request.args.get("search_hpd_by_streetname")
searched_postalcode = request.args.get("search_hpd_by_postalcode")
if searched_housenumber is None or searched_streetname is None or searched_postalcode is None:
flash(u"You must enter a complete address to search.", "error")
return redirect('/')
else:
violation_list = crud.get_violation_by_address(searched_housenumber, searched_streetname,
searched_postalcode)
length_violation_list = len(violation_list)
if violation_list is None:
flash("No violations exist for that address.")
return redirect("/")
return render_template("violation_details.html", violation_list=violation_list,
length_violation_list=length_violation_list)
####### Routes for Autocomplete
@app.route("/autocomplete")
def autocomplete():
"""Get list of 10 matching addresses for any given user input."""
search = request.args.get('q')
url = "https://geosearch.planninglabs.nyc/v1/autocomplete"
payload = {"text": search}
res = requests.get(url, params=payload)
data = res.json()
features = data["features"]
first_ten = features[0:9]
# print("!!!!!!!!!!")
# print(f"first_ten = {first_ten}")
address_list = []
for location in first_ten:
address_list.append(location["properties"]["label"])
print("!!!!!!!!!!")
print(f"address_list = {address_list}")
return jsonify(matching_results=address_list)
# return address_list
# Modify port and debug for deployment to Heroku
PORT = int(os.environ.get("PORT", 5000))
DEBUG = "NO_DEBUG" not in os.environ
if __name__ == "__main__":
connect_to_db(app)
app.run(host='0.0.0.0', port=PORT, debug=DEBUG)