From 775409f83d77eb4f98fbcac9419b99721f5c7815 Mon Sep 17 00:00:00 2001 From: user Date: Sun, 7 Apr 2024 18:08:45 +0200 Subject: derive colors from database, verify icons are in sync --- src/db_handler.py | 9 +++++++++ src/shop_map_node5_net.py | 16 ++++++++++------ src/static/main.js | 19 ++++++------------- 3 files changed, 25 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/db_handler.py b/src/db_handler.py index 5798c66..0fd3ec1 100644 --- a/src/db_handler.py +++ b/src/db_handler.py @@ -7,6 +7,15 @@ with open(os.path.join('configs', 'database.yml'), 'r') as file: db_con_params = yaml.safe_load(file.read()) +def get_chains() -> (list[dict]): + with psycopg.connect(**db_con_params, row_factory=psycopg.rows.dict_row) as conn: + with conn.cursor() as cur: + cur.execute(""" + SELECT name, color FROM chain + """) + chains = cur.fetchall() + return chains + def get_all() -> (list[dict]): with psycopg.connect(**db_con_params, row_factory=psycopg.rows.dict_row) as conn: with conn.cursor() as cur: diff --git a/src/shop_map_node5_net.py b/src/shop_map_node5_net.py index 44c6cb5..3be40e2 100644 --- a/src/shop_map_node5_net.py +++ b/src/shop_map_node5_net.py @@ -15,10 +15,16 @@ def index(): @app.route("/chains") def chains(): - icons = os.listdir(os.path.join(os.getcwd(), 'src', 'static', 'icons')) - icons.pop(icons.index('Unknown.png')) - chains = [os.path.splitext(a)[0] for a in icons] - return json.dumps(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("/supermarkets.json") @@ -35,10 +41,8 @@ def voronoi_polygons(): def all(): rows = db_handler.get_all() for row in rows: - print(row['polygon']) coordinates = [] for coordinate in json.loads(row['polygon'])['coordinates'][0]: coordinates.append([coordinate[1], coordinate[0]]) row['polygon'] = coordinates - print(row['polygon']) return json.dumps(rows) diff --git a/src/static/main.js b/src/static/main.js index c37c92b..82d474e 100644 --- a/src/static/main.js +++ b/src/static/main.js @@ -10,14 +10,12 @@ let icons = {}; async function fetchChains() { const response = await fetch("chains"); known_store_chains = await response.json(); - icons["Unknown"] = L.icon({ iconUrl: 'icons/Unknown.png', iconSize: [10, 16], iconAnchor: [16, 16], }); - - for (let known_store_chain of known_store_chains) { + for (let known_store_chain in known_store_chains) { icons[known_store_chain] = L.icon({ iconUrl: `icons/${known_store_chain}.png`, iconAnchor: [8, 8], @@ -28,24 +26,19 @@ async function fetchChains() { function addPolygon(store) { - console.log(store) let color; - if (store.brand === 'Rema 1000'){ - color = 'blue'; - } else if (store.brand === 'Netto'){ - color = 'yellow' + if (known_store_chains.hasOwnProperty(store.brand)) { + color = known_store_chains[store.brand]; + } else { + color = 'grey'; } - else if (store.brand === 'Coop 365discount'){ - color = 'green'; - }else{ - color = 'red'} L.polygon(store.polygon, {color: color}).addTo(map); } function addShop(shop) { let icon; - if (known_store_chains.includes(shop.brand)){ + if (known_store_chains.hasOwnProperty(shop.brand)){ icon = icons[shop.brand]; } else { icon = icons["Unknown"]; -- cgit v1.2.3