-
Notifications
You must be signed in to change notification settings - Fork 10
/
server.py
142 lines (100 loc) · 4.35 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
""" Investable Server """
from flask import Flask, render_template, redirect, flash, request, jsonify, json
from flask_debugtoolbar import DebugToolbarExtension
import jinja2
import os
import geocoder
from zillow_utilities import *
from account_utilities import *
from mortgage_calculator import *
from db_queries import *
from model import *
app = Flask(__name__)
app.secret_key = os.environ['APP_KEY']
app.zwsid = os.environ['ZWSID']
app.gmaps = os.environ['GMAPS_JS']
# jinja debugger
app.jinja_env.undefined = jinja2.StrictUndefined
app.jinja_env.auto_reload = True
##############################################################################
# Route definitions
@app.route('/')
def homepage():
""" Brings user to the homepage. """
return render_template('index.html', GMAPS_JS=app.gmaps)
@app.route('/search.json')
def search():
""" Returns user search results from Zillow's API and PostgreSQL. """
# Search address entered by the user
full_address = {}
full_address.update(request.args.items())
# Gets API response data from zillow_utilities
response_code, price, message, hoa = get_unit_price(full_address)
# If the location is found in Zillow's API
if response_code == 100:
unit_details = get_zillow_unit_details(full_address)
# Returns the response code and unit details from Zillow's API and PostgreSQL
listing = { 'response': response_code,
'price': price,
'message': message,
'neighborhood': unit_details['neighborhood'],
'street': unit_details['street'],
'city': unit_details['city'],
'state': unit_details['state'],
'zipcode': unit_details['zipcode'],
'bedrooms': unit_details['bedrooms'],
'bathrooms': unit_details['bathrooms'],
'sqft': unit_details['sqft'],
'hoa': hoa,
'latitude': unit_details['latitude'],
'longitude': unit_details['longitude'],
'latlng_point': unit_details['latlng_point'],
'zpid': unit_details['zpid']
}
# Adds a listing to the database
add_listing_to_db(listing)
else:
listing = { 'response': response_code, 'price': price, 'message': message }
return jsonify(listing)
@app.route('/avgrent.json')
def get_rent_avgs():
""" Gets average rent data from db_queries. """
listing = request.args.get('listing')
listing_dict = json.loads(listing)
# If source data contains a latlng_point
if listing_dict.get('latlng_point'):
latlng_point = listing_dict['latlng_point']
# Otherwise, make a latlng point
else:
latlng_point = 'POINT({} {})'.format(listing_dict['latitude'],listing_dict['longitude'])
rent_avgs = get_avg_rent(listing_dict['bedrooms'], listing_dict['bathrooms'], listing_dict['sqft'], latlng_point)
return jsonify(rent_avgs)
@app.route('/listings.json')
def get_listings():
""" Finds listings in the area filtered by bedrooms, bathrooms, and/or prices. """
bounds = json.loads(request.args.get('geoBounds'))
bedrooms = float(request.args.get('bedroomFilter'))
bathrooms = float(request.args.get('bathroomFilter'))
low_price = int(request.args.get('lowPrice'))
high_price = int(request.args.get('highPrice'))
# Retrieves listings from db_queries
filtered_listings = find_all_listings(bounds, bedrooms, bathrooms, low_price, high_price)
return jsonify(filtered_listings)
@app.route('/calculator')
def calculate_monthly_payment():
""" Calculates the monthly mortgage payment based on user's details. """
# User mortgage data pulled from AJAX
mortgage_details = {}
mortgage_details.update(request.args.items())
mortgage, total_mortgage = calculate_mortgage(mortgage_details)
return jsonify({ 'mortgage': mortgage, 'total_mortgage': total_mortgage })
##############################################################################
# Helper functions
if __name__ == "__main__":
app.debug = False
# Use the DebugToolbar
DebugToolbarExtension(app)
# Connect DB to Flask before running app
connect_to_db_flask(app, os.environ.get("DATABASE_URL"))
PORT = int(os.environ.get('PORT', 5000))
app.run(host="0.0.0.0", port=PORT)