-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapp.py
More file actions
executable file
·357 lines (306 loc) · 10.7 KB
/
app.py
File metadata and controls
executable file
·357 lines (306 loc) · 10.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
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
from dash import Dash, clientside_callback, Input, Output, dcc, ClientsideFunction
from flask import request, jsonify, abort, Blueprint
from flask_compress import Compress
from loguru import logger
from typing import Any
import bleach
import dash
import dash_bootstrap_components as dbc
import dash_mantine_components as dmc
import logging
import sqlite3
logging.getLogger().setLevel(logging.INFO)
external_stylesheets = [
dbc.themes.BOOTSTRAP,
dbc.icons.BOOTSTRAP,
dbc.icons.FONT_AWESOME
]
external_scripts = [
'https://cdn.jsdelivr.net/npm/@turf/turf@6/turf.min.js', # Turf.js for convex hulls
'https://cdn.jsdelivr.net/npm/sweetalert2@11' # SweetAlert2 for popups
]
# Create the app
app = Dash(
external_scripts = external_scripts,
external_stylesheets = external_stylesheets,
health_endpoint="health", # https://dash.plotly.com/app-monitoring
# Add meta tags for mobile devices
# https://community.plotly.com/t/reorder-website-for-mobile-view/33669/5?
meta_tags = [
{"name": "viewport", "content": "width=device-width, initial-scale=1"}
],
name = __name__,
use_pages = True,
)
# Set the page title
app.title = "WhereToLive.LA"
app.description = "An interactive map of available rental & for-sale properties in Los Angeles County."
# Configure compression
app.server.config["COMPRESS_MIN_SIZE"] = 1024 # only compress responses >= 1KB
app.server.config["COMPRESS_MIMETYPES"] = [
"text/html",
"text/css",
"application/json",
"application/javascript",
"text/javascript",
]
# Enable Flask-Compress for compression
Compress(app.server)
# For Gunicorn
server = app.server
# Plausible privacy-friendly analytics
# https://dash.plotly.com/external-resources#usage (Option 1)
# Probably won't get past adblockers and NoScript but whatever, good enough
app.index_string = """<!DOCTYPE html>
<html>
<head>
<script defer data-domain="wheretolive.la" src="https://plausible.automateordie.io/js/plausible.js" type="application/javascript"></script>
{%metas%}
<title>{%title%}</title>
{%favicon%}
{%css%}
</head>
<body>
{%app_entry%}
<footer>
{%config%}
{%scripts%}
{%renderer%}
</footer>
</body>
</html>
"""
app.layout = dmc.MantineProvider(
dmc.Container([
dcc.Store(id="theme-switch-store", storage_type="local"),
dbc.Row( # Second row: the rest
[
dash.page_container
],
# Remove the whitespace/padding between the two cards (aka the gutters)
# https://stackoverflow.com/a/70495385
className="g-0",
),
#html.Link(href='/assets/style.css', rel='stylesheet'),
],
fluid = True,
className = "dmc",
),
#forceColorScheme="dark",
)
ALLOWED_OPTIONS = {
"Wrong Location",
"Unavailable/Sold/Rented",
"Wrong Details",
"Incorrect Price",
"Other"
}
clientside_callback(
ClientsideFunction(namespace='clientside', function_name='themeSwitch'),
Output("theme-switch-store", "data"),
Input("color-scheme-switch", "checked"),
)
# Create a custom route for the report form submission
@app.server.route('/report_listing', methods=['POST'])
def report_listing() -> tuple:
"""Handle listing reports. If marked 'Unavailable/Sold/Rented', update the database flag."""
data = request.get_json()
mls_number: str = data.get('mls_number')
option: str = data.get('option')
text_report: str = data.get('text')
page_path: str = (data.get("page_path") or "").lower()
if option not in ALLOWED_OPTIONS:
abort(400, "Invalid option provided.")
# Sanitize text input (disallow any tags)
sanitized_text = bleach.clean(text_report, tags=[], attributes={}, strip=True)
logger.info(f"Received report for MLS {mls_number}: Option='{option}', Details='{sanitized_text}'")
try:
# Determine which table to update based on context (lease or buy)
if page_path.startswith("/buy"):
table_name = "buy"
elif page_path == "" or page_path == "/":
table_name = "lease"
else:
abort(400, f"Invalid page context: {page_path}")
conn = sqlite3.connect("assets/datasets/larentals.db")
cur = conn.cursor()
# make sure our two new columns exist
cur.execute(f"PRAGMA table_info({table_name});")
cols = [r[1] for r in cur.fetchall()]
if 'report_option' not in cols:
cur.execute(f"ALTER TABLE {table_name} ADD COLUMN report_option TEXT;")
if 'report_text' not in cols:
cur.execute(f"ALTER TABLE {table_name} ADD COLUMN report_text TEXT;")
if option == "Unavailable/Sold/Rented":
# mark tenatively inactive
cur.execute(
f"UPDATE {table_name} SET reported_as_inactive = 1 WHERE mls_number = ?",
(mls_number,)
)
logger.success(f"Marked MLS {mls_number} as inactive in '{table_name}' table.")
else:
# record the other option + free‐form text
cur.execute(
f"""UPDATE {table_name}
SET report_option = ?, report_text = ?
WHERE mls_number = ?""",
(option, sanitized_text, mls_number)
)
logger.success(
f"Saved user-submitted report for MLS {mls_number}: option={option}, text='{sanitized_text}'"
)
conn.commit()
conn.close()
return jsonify(status="success"), 200
except Exception as e:
logger.error(f"Error handling report for MLS {mls_number}: {e}")
return jsonify(status="error", message="Internal error, please try again later."), 500
# Create a custom route for fetching ISP options
def register_isp_routes(server: Any, db_path: str = 'assets/datasets/larentals.db') -> None:
"""
Register HTTP routes for fetching ISP options on-demand.
Args:
server: The Flask server instance (typically `app.server` in Dash).
db_path: Path to the SQLite database file. Defaults to 'assets/datasets/larentals.db'.
"""
bp = Blueprint("isp_api", __name__)
@bp.get("/api/lease/isp-options/<listing_id>")
def get_lease_isp_options(listing_id: str):
"""
Return top ISP options for a given listing_id (mls_number).
Args:
listing_id: MLS/listing identifier used in `lease_provider_options.listing_id`.
Returns:
JSON array of provider option dicts matching the structure expected by popup.js.
"""
sql = """
SELECT
DBA,
-- Normalize Service_Type based on TechCode (same logic as the SQL view)
CASE
-- DSL (copper)
WHEN TechCode IN (10, 11, 12, 20) THEN 'DSL'
-- Cable / Fiber / Satellite
WHEN TechCode = 40 THEN 'Cable'
WHEN TechCode = 50 THEN 'Fiber'
WHEN TechCode = 60 THEN 'Satellite'
-- Fixed wireless
WHEN TechCode IN (70, 71, 72) THEN 'Terrestrial Fixed Wireless'
-- Fallback to whatever was provided
ELSE COALESCE(Service_Type, 'Unknown')
END AS Service_Type,
TechCode,
MaxAdDn,
MaxAdUp,
MaxDnTier,
MaxUpTier,
MinDnTier,
MinUpTier,
CASE
WHEN TechCode = 50 THEN 'best'
WHEN TechCode IN (40, 43) AND COALESCE(MaxAdDn, 0) >= 1000 THEN 'best'
WHEN COALESCE(MaxAdDn, 0) >= 1000 THEN 'best'
WHEN TechCode IN (40, 43) THEN 'good'
WHEN TechCode IN (70, 71, 72) AND COALESCE(MaxAdDn, 0) >= 100 THEN 'good'
WHEN COALESCE(MaxAdDn, 0) >= 100 THEN 'good'
ELSE 'fallback'
END AS bucket
FROM lease_provider_options
WHERE listing_id = ?
AND DBA IS NOT NULL
AND NOT (COALESCE(MaxAdDn, 0) = 0 AND COALESCE(MaxAdUp, 0) = 0)
ORDER BY COALESCE(MaxAdDn, -1) DESC
LIMIT 8;
"""
with sqlite3.connect(db_path) as conn:
conn.row_factory = sqlite3.Row
rows = conn.execute(sql, (listing_id,)).fetchall()
result: list[dict[str, Any]] = []
for r in rows:
result.append(
{
"dba": r["DBA"],
"service_type": r["Service_Type"], # ← Now returns "Fiber", "Cable", "DSL", etc.
"tech_code": r["TechCode"],
"max_dn_mbps": r["MaxAdDn"],
"max_up_mbps": r["MaxAdUp"],
"max_dn_tier": r["MaxDnTier"],
"max_up_tier": r["MaxUpTier"],
"min_dn_tier": r["MinDnTier"],
"min_up_tier": r["MinUpTier"],
"bucket": r["bucket"],
}
)
return jsonify(result)
@bp.get("/api/buy/isp-options/<listing_id>")
def get_buy_isp_options(listing_id: str):
"""
Return top ISP options for a given listing_id (mls_number) from the buy table.
Args:
listing_id: MLS/listing identifier used in `buy_provider_options.listing_id`.
Returns:
JSON array of provider option dicts matching the structure expected by popup.js.
"""
sql = """
SELECT
DBA,
-- Normalize Service_Type based on TechCode (same logic as the SQL view)
CASE
-- DSL (copper)
WHEN TechCode IN (10, 11, 12, 20) THEN 'DSL'
-- Cable / Fiber / Satellite
WHEN TechCode = 40 THEN 'Cable'
WHEN TechCode = 50 THEN 'Fiber'
WHEN TechCode = 60 THEN 'Satellite'
-- Fixed wireless
WHEN TechCode IN (70, 71, 72) THEN 'Terrestrial Fixed Wireless'
-- Fallback to whatever was provided
ELSE COALESCE(Service_Type, 'Unknown')
END AS Service_Type,
TechCode,
MaxAdDn,
MaxAdUp,
MaxDnTier,
MaxUpTier,
MinDnTier,
MinUpTier,
CASE
WHEN TechCode = 50 THEN 'best'
WHEN TechCode IN (40, 43) AND COALESCE(MaxAdDn, 0) >= 1000 THEN 'best'
WHEN COALESCE(MaxAdDn, 0) >= 1000 THEN 'best'
WHEN TechCode IN (40, 43) THEN 'good'
WHEN TechCode IN (70, 71, 72) AND COALESCE(MaxAdDn, 0) >= 100 THEN 'good'
WHEN COALESCE(MaxAdDn, 0) >= 100 THEN 'good'
ELSE 'fallback'
END AS bucket
FROM buy_provider_options
WHERE listing_id = ?
AND DBA IS NOT NULL
AND NOT (COALESCE(MaxAdDn, 0) = 0 AND COALESCE(MaxAdUp, 0) = 0)
ORDER BY COALESCE(MaxAdDn, -1) DESC
LIMIT 8;
"""
with sqlite3.connect(db_path) as conn:
conn.row_factory = sqlite3.Row
rows = conn.execute(sql, (listing_id,)).fetchall()
result: list[dict[str, Any]] = []
for r in rows:
result.append(
{
"dba": r["DBA"],
"service_type": r["Service_Type"],
"tech_code": r["TechCode"],
"max_dn_mbps": r["MaxAdDn"],
"max_up_mbps": r["MaxAdUp"],
"max_dn_tier": r["MaxDnTier"],
"max_up_tier": r["MaxUpTier"],
"min_dn_tier": r["MinDnTier"],
"min_up_tier": r["MinUpTier"],
"bucket": r["bucket"],
}
)
return jsonify(result)
server.register_blueprint(bp)
register_isp_routes(server, db_path="assets/datasets/larentals.db")
if __name__ == '__main__':
app.run(debug=True)