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
|
import json
import os
import flask
import db_handler
import logging
class ColorFormatter(logging.Formatter):
grey = "\x1b[90;20m"
cyan = "\x1b[96;20m"
yellow = "\x1b[33;20m"
red = "\x1b[31;20m"
bold_red = "\x1b[31;1m"
reset = "\x1b[0m"
format = "%(asctime)s,%(msecs)03d %(levelname)-8s [%(filename)s:%(lineno)d] %(message)s"
FORMATS = {
logging.DEBUG: grey + format + reset,
logging.INFO: cyan + format + reset,
logging.WARNING: yellow + format + reset,
logging.ERROR: red + format + reset,
logging.CRITICAL: bold_red + format + reset
}
def format(self, record):
log_fmt = self.FORMATS.get(record.levelno)
formatter = logging.Formatter(log_fmt)
return formatter.format(record)
logger = logging.getLogger(__name__) # Instantiate a logger to be used in this module
# Display every message Change this to INFO to see INFO and above (filter out DEBUG)
# See: https://docs.python.org/3/howto/logging.html#logging-levels
logger.root.setLevel(logging.DEBUG)
stream_handler = logging.StreamHandler() # This catches and handles log messages on the root handler
stream_handler.setFormatter(ColorFormatter())
logger.root.addHandler(stream_handler)
app = flask.Flask(__name__, template_folder='templates', static_folder='static', static_url_path='')
@app.route("/")
def index():
return flask.render_template('/index.html')
@app.route("/chains.json")
def chains():
files = os.listdir(os.path.join(os.getcwd(), 'src', 'static', 'icons'))
files.pop(files.index('Unknown.png'))
icon_names = set([os.path.splitext(a)[0] for a in files])
chains = db_handler.get_chains()
chain_names = set((a['name'] for a in chains))
if icon_names != chain_names:
print(f'WARNING ICON MISSING, OR DB ENTRY MISSING')
color_by_name = {a['name']: a['color'] for a in chains}
return json.dumps(color_by_name)
@app.route("/categories.json")
def categories():
categories = db_handler.categories
return json.dumps(categories)
@app.route("/all.json")
def all():
category_RADIOACTIVE = flask.request.args.get('category') # User input is RADIOACTIVE
country_RADIOACTIVE = flask.request.args.get('country') # User input is RADIOACTIVE
#logger.debug(f'/all.json requested, input params: category: {category_RADIOACTIVE} country: {country_RADIOACTIVE}')
rows = db_handler.get_all(country_RADIOACTIVE, category_RADIOACTIVE)
for row in rows:
coordinates = []
for coordinate in json.loads(row['polygon'])['coordinates'][0]:
coordinates.append([coordinate[1], coordinate[0]])
row['polygon'] = coordinates
return json.dumps(rows)
|