@@ -78,67 +78,70 @@ def __eq__(self, other):
7878
7979# todo: merge with get_request_params()
8080def get_request_params_from_request_values (vals : dict ) -> Optional [RequestParams ]:
81- news_flash_obj = extract_news_flash_obj (vals )
82- news_flash_description = (
83- news_flash_obj .description
84- if news_flash_obj is not None and news_flash_obj .description is not None
85- else None
86- )
87- news_flash_title = (
88- news_flash_obj .title
89- if news_flash_obj is not None and news_flash_obj .title is not None
90- else None
91- )
92- location = get_location_from_news_flash_or_request_values (news_flash_obj , vals )
93- if location is None :
94- return None
95- years_ago = vals .get ("years_ago" , BE_CONST .DEFAULT_NUMBER_OF_YEARS_AGO )
96- lang = vals .get ("lang" , "he" )
97- location_text = location ["text" ]
98- gps = location .get ("gps" )
99- location_info = location ["data" ]
100-
101- if location_info is None :
102- return None
103- resolution = location_info .pop ("resolution" )
104- if resolution is None or resolution not in BE_CONST .SUPPORTED_RESOLUTIONS :
105- logging .error (f"Resolution empty or not supported: { resolution } ." )
106- return None
107-
108- if all (value is None for value in location_info .values ()):
109- return None
110-
11181 try :
112- years_ago = int (years_ago )
113- except (ValueError , TypeError ):
114- return None
115- if years_ago < 0 or years_ago > 100 :
82+ news_flash_obj = extract_news_flash_obj (vals )
83+ news_flash_description = (
84+ news_flash_obj .description
85+ if news_flash_obj is not None and news_flash_obj .description is not None
86+ else None
87+ )
88+ news_flash_title = (
89+ news_flash_obj .title
90+ if news_flash_obj is not None and news_flash_obj .title is not None
91+ else None
92+ )
93+ location = get_location_from_news_flash_or_request_values (news_flash_obj , vals )
94+ if location is None :
95+ return None
96+ years_ago = vals .get ("years_ago" , BE_CONST .DEFAULT_NUMBER_OF_YEARS_AGO )
97+ lang = vals .get ("lang" , "he" )
98+ location_text = location ["text" ]
99+ gps = location .get ("gps" , {})
100+ location_info = location ["data" ]
101+
102+ if location_info is None :
103+ return None
104+ resolution = location_info .pop ("resolution" )
105+ if resolution is None or resolution not in BE_CONST .SUPPORTED_RESOLUTIONS :
106+ logging .error (f"Resolution empty or not supported: { resolution } ." )
107+ return None
108+
109+ if all (value is None for value in location_info .values ()):
110+ return None
111+
112+ try :
113+ years_ago = int (years_ago )
114+ except (ValueError , TypeError ):
115+ return None
116+ if years_ago < 0 or years_ago > 100 :
117+ return None
118+ last_accident_date = get_latest_accident_date (table_obj = AccidentMarkerView , filters = None )
119+ # converting to datetime object to get the date
120+ end_time = last_accident_date .to_pydatetime ().date ()
121+ start_time = datetime .date (end_time .year + 1 - years_ago , 1 , 1 )
122+
123+ widget_specific = {}
124+ if resolution == BE_CONST .ResolutionCategories .SUBURBAN_ROAD :
125+ widget_specific .update ({"road_segment_name" : location_info .get ("road_segment_name" )})
126+
127+ request_params = RequestParams (
128+ years_ago = years_ago ,
129+ location_text = location_text ,
130+ location_info = location_info ,
131+ # TODO: getting a warning on resolution=resolution: "Expected type 'dict', got 'int' instead"
132+ resolution = resolution ,
133+ gps = gps ,
134+ start_time = start_time ,
135+ end_time = end_time ,
136+ lang = lang ,
137+ news_flash_description = news_flash_description ,
138+ news_flash_title = news_flash_title ,
139+ widget_specific = widget_specific
140+ )
141+ return request_params
142+ except ValueError :
143+ logging .exception (f"Exception while preparing request params. vals:{ vals } ." )
116144 return None
117- last_accident_date = get_latest_accident_date (table_obj = AccidentMarkerView , filters = None )
118- # converting to datetime object to get the date
119- end_time = last_accident_date .to_pydatetime ().date ()
120- start_time = datetime .date (end_time .year + 1 - years_ago , 1 , 1 )
121-
122- widget_specific = {}
123- if resolution == BE_CONST .ResolutionCategories .SUBURBAN_ROAD :
124- widget_specific .update ({"road_segment_name" : location_info .get ("road_segment_name" )})
125-
126- request_params = RequestParams (
127- years_ago = years_ago ,
128- location_text = location_text ,
129- location_info = location_info ,
130- # TODO: getting a warning on resolution=resolution: "Expected type 'dict', got 'int' instead"
131- resolution = resolution ,
132- gps = gps ,
133- start_time = start_time ,
134- end_time = end_time ,
135- lang = lang ,
136- news_flash_description = news_flash_description ,
137- news_flash_title = news_flash_title ,
138- widget_specific = widget_specific
139- )
140- return request_params
141-
142145
143146def get_location_from_news_flash_or_request_values (
144147 news_flash_obj : Optional [NewsFlash ], vals : dict
0 commit comments