diff --git a/anyway/flask_app.py b/anyway/flask_app.py index 7be06ebb..b3e75865 100755 --- a/anyway/flask_app.py +++ b/anyway/flask_app.py @@ -61,10 +61,8 @@ ) from anyway.request_params import get_request_params_from_request_values from anyway.views.news_flash.api import ( - news_flash, - news_flash_new, single_news_flash, - news_flash_v2, + news_flash, update_news_flash_qualifying, get_downloaded_data, DEFAULT_LIMIT_REQ_PARAMETER, @@ -1091,12 +1089,10 @@ def acc_in_area_query(): view_func=injured_around_schools_api, methods=["GET"], ) -app.add_url_rule("/api/news-flash", endpoint=None, view_func=news_flash_v2, methods=["GET", "PATCH", "OPTIONS"]) +app.add_url_rule("/api/news-flash", endpoint=None, view_func=news_flash, methods=["GET", "PATCH", "OPTIONS"]) app.add_url_rule("/api/comments", endpoint=None, view_func=get_comments, methods=["GET"]) app.add_url_rule("/api/comments", endpoint=None, view_func=create_comment, methods=["POST"]) -app.add_url_rule("/api/v1/news-flash", endpoint=None, view_func=news_flash, methods=["GET"]) - nf_parser = reqparse.RequestParser() nf_parser.add_argument("id", type=int, help="News flash id") nf_parser.add_argument("source", type=str, help="news flash source") @@ -1162,11 +1158,10 @@ def datetime_to_str(val: datetime.datetime) -> str: "road_segment_id": fields.Integer(), "newsflash_location_qualification": fields.Integer(), "location_qualifying_user": fields.Integer(), + "curr_cbs_location_text": fields.String(), + "critical": fields.Boolean(), }, ) -news_flash_list_model = api.model( - "news_flash_list", {"news_flashes": fields.List(fields.Nested(news_flash_fields_model))} -) @api.route("/api/news-flash/", methods=["GET", "PATCH", "OPTIONS"]) @@ -1184,22 +1179,6 @@ def patch(self, news_flash_id): return update_news_flash_qualifying(news_flash_id) def options(self, news_flash_id): return single_news_flash(news_flash_id) - - -@api.route("/api/news-flash-new", methods=["GET"]) -class RetrieveNewsFlash(Resource): - @api.doc("get news flash records") - @api.expect(nf_parser) - @api.response(404, "Parameter value not supported or missing") - @api.response( - 200, "Retrieve news-flash items filtered by given parameters", news_flash_list_model - ) - def get(self): - args = nf_parser.parse_args() - res = news_flash_new(args) - for d in res: - d["date"] = datetime_to_str(d["date"]) if "date" in d else "None" - return {"news_flashes": res} """ diff --git a/anyway/views/news_flash/api.py b/anyway/views/news_flash/api.py index 4b5e4d10..5786cb74 100644 --- a/anyway/views/news_flash/api.py +++ b/anyway/views/news_flash/api.py @@ -69,41 +69,6 @@ def check_supported_resolutions(cls, v): def news_flash(): - news_flash_id = request.values.get("id") - - if news_flash_id is not None: - query = db.session.query(NewsFlash) - news_flash_obj = query.filter(NewsFlash.id == news_flash_id).first() - if news_flash_obj is not None: - if is_news_flash_resolution_supported(news_flash_obj): - return Response( - json.dumps(news_flash_obj.serialize(), default=str), mimetype="application/json" - ) - else: - return Response("News flash location not supported", 406) - return Response(status=404) - - query = gen_news_flash_query( - db.session, - source=request.values.get("source"), - start_date=request.values.get("start_date"), - end_date=request.values.get("end_date"), - interurban_only=request.values.get("interurban_only"), - road_number=request.values.get("road_number"), - road_segment=request.values.get("road_segment_only"), - last_minutes=request.values.get("last_minutes"), - offset=request.values.get("offset", DEFAULT_OFFSET_REQ_PARAMETER), - limit=request.values.get("limit", DEFAULT_LIMIT_REQ_PARAMETER), - ) - news_flashes = query.all() - - news_flashes_jsons = [n.serialize() for n in news_flashes] - for news_flash in news_flashes_jsons: - set_display_source(news_flash) - return Response(json.dumps(news_flashes_jsons, default=str), mimetype="application/json") - - -def news_flash_v2(): requested_query_params = normalize_query(request.args) try: validated_query_params = NewsFlashQuery(**requested_query_params).dict(exclude_none=True) @@ -113,7 +78,7 @@ def news_flash_v2(): if "id" in validated_query_params: return get_news_flash_by_id(validated_query_params["id"]) - query = gen_news_flash_query_v2(db.session, validated_query_params) + query = gen_news_flash_query(db.session, validated_query_params) news_flashes = query.all() news_flashes_jsons = [n.serialize() for n in news_flashes] @@ -122,94 +87,7 @@ def news_flash_v2(): return Response(json.dumps(news_flashes_jsons, default=str), mimetype="application/json") -def news_flash_new(args: dict) -> List[dict]: - news_flash_id = args["id"] - - if news_flash_id is not None: - return single_news_flash(news_flash_id) - - query = gen_news_flash_query( - db.session, - source=args.get("source"), - start_date=args.get("start_date"), - end_date=args.get("end_date"), - interurban_only=args.get("interurban_only"), - road_number=args.get("road_number"), - road_segment=args.get("road_segment_only"), - offset=args.get("offset"), - limit=args.get("limit"), - last_minutes=args.get("last_minutes"), - ) - news_flashes = query.all() - - news_flashes_jsons = [n.serialize() for n in news_flashes] - for news_flash in news_flashes_jsons: - set_display_source(news_flash) - return news_flashes_jsons - - -def gen_news_flash_query( - session, - source=None, - start_date=None, - end_date=None, - interurban_only=None, - road_number=None, - road_segment=None, - offset=None, - limit=None, - last_minutes=None -): - query = session.query(NewsFlash) - # get all possible sources - sources = [ - str(source_name[0]) for source_name in db.session.query(NewsFlash.source).distinct().all() - ] - if source: - if source not in sources: - return Response( - '{"message": "Requested source does not exist"}', - status=404, - mimetype="application/json", - ) - else: - query = query.filter(NewsFlash.source == source) - - if start_date and end_date: - query = filter_by_timeframe(end_date, query, start_date) - # when only one of the dates is sent - elif start_date or end_date: - return Response( - '{"message": "Must send both start_date and end_date"}', - status=404, - mimetype="application/json", - ) - supported_resolutions = set([x.value for x in BE_CONST.SUPPORTED_RESOLUTIONS]) - query = query.filter(NewsFlash.resolution.in_(supported_resolutions)) - if interurban_only == "true" or interurban_only == "True": - query = query.filter(NewsFlash.resolution.in_(["כביש בינעירוני"])) - if road_number: - query = query.filter(NewsFlash.road1 == road_number) - if road_segment == "true": - query = query.filter(not_(NewsFlash.road_segment_id == None)) - if last_minutes: - last_timestamp = datetime.datetime.now() - datetime.timedelta(minutes=last_minutes) - query = query.filter(NewsFlash.date >= last_timestamp) - query = query.filter( - and_( - NewsFlash.accident == True, - not_(and_(NewsFlash.lat == 0, NewsFlash.lon == 0)), - not_(and_(NewsFlash.lat == None, NewsFlash.lon == None)), - ) - ).order_by(NewsFlash.date.desc()) - - query = query.offset(offset) - query = query.limit(limit) - - return query - - -def gen_news_flash_query_v2(session, valid_params: dict): +def gen_news_flash_query(session, valid_params: dict): query = session.query(NewsFlash) for param, value in valid_params.items(): if param == "road_number": diff --git a/tests/test_news_flash_api.py b/tests/test_news_flash_api.py index f86f5deb..06deabd5 100644 --- a/tests/test_news_flash_api.py +++ b/tests/test_news_flash_api.py @@ -202,7 +202,7 @@ def _test_update_news_flash_qualifying_not_manual_exists_location_db(self): def test_gen_news_flash_query(self): orig_supported_resolutions = BE_CONST.SUPPORTED_RESOLUTIONS BE_CONST.SUPPORTED_RESOLUTIONS = [BE_CONST.ResolutionCategories.DISTRICT] - actual = gen_news_flash_query(self.session, road_number=12345678) + actual = gen_news_flash_query(self.session, {"road_number": 12345678}) news_flashes = actual.all() self.assertEqual(len(news_flashes), 1, "single news flash") self.assertEqual( @@ -210,13 +210,13 @@ def test_gen_news_flash_query(self): ) BE_CONST.SUPPORTED_RESOLUTIONS = [BE_CONST.ResolutionCategories.REGION] - actual = gen_news_flash_query(self.session, road_number=12345678) + actual = gen_news_flash_query(self.session, {"road_number": 12345678}) news_flashes = actual.all() self.assertEqual(len(news_flashes), 1, "single news flash") self.assertEqual(news_flashes[0].description, self.region_description, "region description") BE_CONST.SUPPORTED_RESOLUTIONS = [BE_CONST.ResolutionCategories.CITY] - actual = gen_news_flash_query(self.session, road_number=12345678) + actual = gen_news_flash_query(self.session, {"road_number": 12345678}) news_flashes = actual.all() self.assertEqual(len(news_flashes), 0, "zero news flash")