From 35cf0d663fa9cefdaaf8a2c96e79aeadcb552ef6 Mon Sep 17 00:00:00 2001 From: Atalya Alon Date: Wed, 18 Dec 2024 16:00:16 +0200 Subject: [PATCH 1/7] remove fake GPS, add newsflash GPS --- anyway/request_params.py | 13 +++---------- tests/test_request_params.py | 3 +-- 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/anyway/request_params.py b/anyway/request_params.py index 2e35ca9f..e07dc52d 100644 --- a/anyway/request_params.py +++ b/anyway/request_params.py @@ -95,7 +95,7 @@ def get_request_params_from_request_values(vals: dict) -> Optional[RequestParams years_ago = vals.get("years_ago", BE_CONST.DEFAULT_NUMBER_OF_YEARS_AGO) lang = vals.get("lang", "he") location_text = location["text"] - gps = location["gps"] + gps = {"lat": news_flash_obj.lat, "lon": news_flash_obj.lon} location_info = location["data"] if location_info is None: @@ -220,9 +220,7 @@ def extract_road_segment_location(road_segment_id): data["road_segment_name"] = road_segment_name data["road_segment_id"] = int(road_segment_id) text = get_road_segment_location_text(road1, road_segment_name) - # fake gps - todo: fix - gps = {"lat": 32.825610, "lon": 35.165395} - return {"name": "location", "data": data, "gps": gps, "text": text} + return {"name": "location", "data": data, "text": text} # todo: fill both codes and names into location @@ -233,9 +231,7 @@ def extract_street_location(input_vals: dict): for k in ["yishuv_name", "yishuv_symbol", "street1", "street1_hebrew"]: data[k] = vals[k] text = get_street_location_text(vals["yishuv_name"], vals["street1_hebrew"]) - # fake gps - todo: fix - gps = {"lat": 32.825610, "lon": 35.165395} - return {"name": "location", "data": data, "gps": gps, "text": text} + return {"name": "location", "data": data, "text": text} def extract_street_location_suggestion_version(input_vals: dict): @@ -271,12 +267,9 @@ def extract_non_urban_intersection_location(input_vals: dict): data = {"resolution": BE_CONST.ResolutionCategories.SUBURBAN_JUNCTION} for k in ["non_urban_intersection", "non_urban_intersection_hebrew", "road1", "road2"]: data[k] = vals[k] - # fake gps - todo: fix - gps = {"lat": 32.825610, "lon": 35.165395} return { "name": "location", "data": data, - "gps": gps, "text": vals["non_urban_intersection_hebrew"], } diff --git a/tests/test_request_params.py b/tests/test_request_params.py index ec0b35a6..4f7ac957 100644 --- a/tests/test_request_params.py +++ b/tests/test_request_params.py @@ -30,7 +30,6 @@ class TestRequestParams(unittest.TestCase): 'resolution': BE_CONST.ResolutionCategories.SUBURBAN_JUNCTION, 'road1': 669, 'road2': 71}, - 'gps': {'lat': 32.82561, 'lon': 35.165395}, 'name': 'location', 'text': 'צומת השיטה'} nf = NewsFlash() @@ -46,7 +45,7 @@ class TestRequestParams(unittest.TestCase): lang='he', news_flash_description=nf.description, news_flash_title=nf.title, - gps={"lat": 32.825610, "lon": 35.165395} + gps={"lat": None, "lon": None} ) @patch("anyway.request_params.fill_missing_non_urban_intersection_values") From 3eb7c1c53b030642dca7e49db7e2d0fa905aa859 Mon Sep 17 00:00:00 2001 From: Atalya Alon Date: Fri, 20 Dec 2024 15:52:52 +0200 Subject: [PATCH 2/7] add is_included, use get instead of [] --- anyway/request_params.py | 2 +- anyway/widgets/road_segment_widgets/street_view_widget.py | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/anyway/request_params.py b/anyway/request_params.py index e07dc52d..4a6e394c 100644 --- a/anyway/request_params.py +++ b/anyway/request_params.py @@ -95,7 +95,7 @@ def get_request_params_from_request_values(vals: dict) -> Optional[RequestParams years_ago = vals.get("years_ago", BE_CONST.DEFAULT_NUMBER_OF_YEARS_AGO) lang = vals.get("lang", "he") location_text = location["text"] - gps = {"lat": news_flash_obj.lat, "lon": news_flash_obj.lon} + gps = location.get("gps") location_info = location["data"] if location_info is None: diff --git a/anyway/widgets/road_segment_widgets/street_view_widget.py b/anyway/widgets/road_segment_widgets/street_view_widget.py index 17da2739..72939e72 100644 --- a/anyway/widgets/road_segment_widgets/street_view_widget.py +++ b/anyway/widgets/road_segment_widgets/street_view_widget.py @@ -16,10 +16,13 @@ def __init__(self, request_params: RequestParams): def generate_items(self) -> None: self.items = { - "longitude": self.request_params.gps["lon"], - "latitude": self.request_params.gps["lat"], + "longitude": self.request_params.gps.get("lon"), + "latitude": self.request_params.gps.get("lat"), } + def is_included(self): + return self.request_params.gps is not None + @staticmethod def localize_items(request_params: RequestParams, items: Dict) -> Dict: items["data"]["text"] = {"title": _("Street view widget")} From 8f2bbb5380bf11047a96be5002a6c82600f135b9 Mon Sep 17 00:00:00 2001 From: Atalya Alon Date: Fri, 20 Dec 2024 16:06:47 +0200 Subject: [PATCH 3/7] fix is_included --- anyway/widgets/road_segment_widgets/street_view_widget.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/anyway/widgets/road_segment_widgets/street_view_widget.py b/anyway/widgets/road_segment_widgets/street_view_widget.py index 72939e72..fc89e709 100644 --- a/anyway/widgets/road_segment_widgets/street_view_widget.py +++ b/anyway/widgets/road_segment_widgets/street_view_widget.py @@ -21,7 +21,7 @@ def generate_items(self) -> None: } def is_included(self): - return self.request_params.gps is not None + return self.request_params.gps @staticmethod def localize_items(request_params: RequestParams, items: Dict) -> Dict: From 402cb0f383b5637fdf20374e4b211555cc06d73c Mon Sep 17 00:00:00 2001 From: Atalya Alon Date: Fri, 20 Dec 2024 16:08:20 +0200 Subject: [PATCH 4/7] fix text --- tests/test_request_params.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_request_params.py b/tests/test_request_params.py index 4f7ac957..aa7f7a78 100644 --- a/tests/test_request_params.py +++ b/tests/test_request_params.py @@ -45,7 +45,7 @@ class TestRequestParams(unittest.TestCase): lang='he', news_flash_description=nf.description, news_flash_title=nf.title, - gps={"lat": None, "lon": None} + gps={} ) @patch("anyway.request_params.fill_missing_non_urban_intersection_values") From 9ee40d8adbf07daf4b27a2a9ac779c9b58266009 Mon Sep 17 00:00:00 2001 From: Atalya Alon Date: Sat, 28 Dec 2024 16:25:59 +0200 Subject: [PATCH 5/7] add update_result to street_view --- .../road_segment_widgets/street_view_widget.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/anyway/widgets/road_segment_widgets/street_view_widget.py b/anyway/widgets/road_segment_widgets/street_view_widget.py index 68d75a15..13c7571b 100644 --- a/anyway/widgets/road_segment_widgets/street_view_widget.py +++ b/anyway/widgets/road_segment_widgets/street_view_widget.py @@ -1,7 +1,8 @@ +import logging from anyway.request_params import RequestParams from anyway.widgets.widget import register from anyway.widgets.road_segment_widgets.road_segment_widget import RoadSegmentWidget -from typing import Dict +from typing import Dict, Optional from flask_babel import _ @@ -23,8 +24,18 @@ def generate_items(self) -> None: def is_included(self): return self.request_params.gps and self.request_params.gps.get("lon") and self.request_params.gps.get("lat") - @staticmethod def localize_items(request_params: RequestParams, items: Dict) -> Dict: items["data"]["text"] = {"title": _("Street view widget")} - return items \ No newline at end of file + return items + + @classmethod + def update_result(cls, request_params: RequestParams, cached_items: Dict) -> Optional[Dict]: + if cls.is_relevant(request_params): + w = cls(request_params) # pylint: disable=E1120 + try: + w.generate_items() + updated_widget_data = w.serialize() + return updated_widget_data if w.is_included() else None + except Exception as e: + logging.debug(f"Encountered error when generating items for {w.name} : {e}") \ No newline at end of file From 682f4b5e583e1bcac5d6758d7ffdc7bd69ebc988 Mon Sep 17 00:00:00 2001 From: Atalya Alon Date: Sat, 28 Dec 2024 16:26:56 +0200 Subject: [PATCH 6/7] update_output before filtering empty widgets --- anyway/infographics_utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/anyway/infographics_utils.py b/anyway/infographics_utils.py index 6eb57f53..10c2c04a 100755 --- a/anyway/infographics_utils.py +++ b/anyway/infographics_utils.py @@ -212,8 +212,8 @@ def get_infographics_data_for_location(request_params: RequestParams) -> Dict: elif WIDGETS not in output: logging.error(f"get_infographics_data: 'widgets' key missing from output:{output}") else: - non_empty = list(filter(lambda x: x[DATA][ITEMS], output[WIDGETS])) - output[WIDGETS] = update_cache_results(request_params, non_empty) + updated_output = update_cache_results(request_params, output[WIDGETS]) + output[WIDGETS] = list(filter(lambda x: x[DATA][ITEMS], updated_output)) return output From 5b0afb80507e3be6c722fa46d5431748f413f189 Mon Sep 17 00:00:00 2001 From: Atalya Alon Date: Sat, 28 Dec 2024 16:31:39 +0200 Subject: [PATCH 7/7] use generate_widget_data in update_result --- .../road_segment_widgets/street_view_widget.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/anyway/widgets/road_segment_widgets/street_view_widget.py b/anyway/widgets/road_segment_widgets/street_view_widget.py index 13c7571b..bb6ff27a 100644 --- a/anyway/widgets/road_segment_widgets/street_view_widget.py +++ b/anyway/widgets/road_segment_widgets/street_view_widget.py @@ -31,11 +31,7 @@ def localize_items(request_params: RequestParams, items: Dict) -> Dict: @classmethod def update_result(cls, request_params: RequestParams, cached_items: Dict) -> Optional[Dict]: - if cls.is_relevant(request_params): - w = cls(request_params) # pylint: disable=E1120 - try: - w.generate_items() - updated_widget_data = w.serialize() - return updated_widget_data if w.is_included() else None - except Exception as e: - logging.debug(f"Encountered error when generating items for {w.name} : {e}") \ No newline at end of file + try: + return cls.generate_widget_data(request_params) + except Exception as e: + logging.debug(f"Encountered error when generating items for widget class {cls} : {e}")