-
Notifications
You must be signed in to change notification settings - Fork 0
/
should_flask.py
executable file
·83 lines (66 loc) · 2.16 KB
/
should_flask.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
app.py - Main Flask application file
- This script is the WSGI form parser/validator.
- This script requires Flask to be installed.
- It expects to be passed:
- zip # from nginx html form
- It sends all returnables to return flask's render_template
"""
import sys
import logging
import flask
from flask import Flask
from flask import request
from flask import render_template
import main
app = Flask(__name__)
@app.route("/search/", methods=["POST", "GET"])
def get_data():
"""
Return a view to Flask with relevant details
zip comes in as a 'str' from the Flash HTML form and most easily is tested
by casting to 'int'. zip is then queried @mongodb and returns HTML
Done this way it catches zip='' and if zip is None with explictly checking.
"""
try:
verbose = False
logger = logging.getLogger(__name__)
if verbose:
logger.setLevel(logging.DEBUG)
else:
logger.setLevel(logging.INFO)
try:
querystring = request.args
logger.debug(f"querystring: {querystring}")
zip = querystring.get("zip")
if len(str(zip)) != 5:
raise ValueError
elif zip[0] == 0:
int(zip[1:])
else:
int(zip)
except (TypeError, ValueError): # Not Numeric/Didn't send "zip="
logging.error(f"Invalid data: querystring: {querystring} : nota5digitzip")
return render_template("nota5digitzip.html")
except Exception as e:
msg = f"Bug: querystring:{querystring}, Error: {str(e)}"
logging.exception(msg)
flask.abort(500)
else:
zip = str(zip)
all_posts, all_links, all_cust, city, state = main.main(zip)
len_items = len(all_posts)
return render_template(
"craig_list_local_items.html",
zip=zip,
city=city,
state=state,
all_posts=all_posts,
len_items=len_items,
all_links=all_links,
all_cust=all_cust,
)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000)