Skip to content

Commit

Permalink
Merge pull request #2698 from data-for-change/dev
Browse files Browse the repository at this point in the history
merge dev into master
  • Loading branch information
atalyaalon authored Sep 7, 2024
2 parents 3d2fc3d + 83105ba commit 00dda3f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 177 deletions.
54 changes: 4 additions & 50 deletions anyway/flask_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,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,
Expand Down Expand Up @@ -1094,12 +1092,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")
Expand Down Expand Up @@ -1176,11 +1172,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/<int:news_flash_id>", methods=["GET", "PATCH", "OPTIONS"])
Expand All @@ -1200,47 +1195,6 @@ def options(self, news_flash_id):
return single_news_flash(news_flash_id)


def filter_json_fields(json_data, fields):
return {field: json_data[field] for field in fields if field in json_data}


@api.route("/api/news-flash/by-resolution", methods=["GET"])
class RetrieveNewsFlashByResolution(Resource):
@api.doc("get news flash records by resolution")
@api.expect(nfbr_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 = nfbr_parser.parse_args()
limit = args["limit"] if "limit" in args else None
query = search_newsflashes_by_resolution(db.session, args["resolutions"], args["include"], limit)
res = query.all()
news_flashes_jsons = [n.serialize() for n in res]
if not args["fields"]:
filtered_jsons = news_flashes_jsons
else:
filtered_jsons = [filter_json_fields(json_data, args["fields"]) for json_data in news_flashes_jsons]
return Response(json.dumps(filtered_jsons, default=str), mimetype="application/json")


@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}


"""
Returns infographics-data API
"""
Expand Down
130 changes: 6 additions & 124 deletions anyway/views/news_flash/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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]
Expand All @@ -122,95 +87,10 @@ 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
):
def gen_news_flash_query(session, valid_params: dict):
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):
query = session.query(NewsFlash)
for param, value in valid_params.items():
if param == "road_number":
query = query.filter(NewsFlash.road1 == value)
Expand All @@ -233,8 +113,10 @@ def gen_news_flash_query_v2(session, valid_params: dict):
not_(and_(NewsFlash.lat == None, NewsFlash.lon == None)),
)
).order_by(NewsFlash.date.desc())
query = query.offset(valid_params["offset"])
query = query.limit(valid_params["limit"])
if "offset" in valid_params:
query = query.offset(valid_params["offset"])
if "limit" in valid_params:
query = query.limit(valid_params["limit"])
return query


Expand Down
6 changes: 3 additions & 3 deletions tests/test_news_flash_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,21 +202,21 @@ 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(
news_flashes[0].description, self.district_description, "district description"
)

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")

Expand Down

0 comments on commit 00dda3f

Please sign in to comment.