Skip to content

Commit

Permalink
Merge branch 'dev' into bug-2542-fix-newsflash-location-qualification
Browse files Browse the repository at this point in the history
  • Loading branch information
atalyaalon authored Jan 10, 2024
2 parents 5e0f6b4 + 30003a4 commit 8b40bd8
Show file tree
Hide file tree
Showing 23 changed files with 520 additions and 459 deletions.
14 changes: 10 additions & 4 deletions .github/ISSUE_TEMPLATE/localize_widget.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ Current Widget Title for localization, if exists.
(Note - if translation in file is not adequate - please create a better translation)

**Flask Babel**
For updating messages.pot with all strings for translation - use the following command: `pybabel extract -F babel.cfg -o messages.pot .`
For updating existing po files with new strings: `pybabel update -i messages.pot -d translations`
For compiling pybabel mo files use: `pybabel compile -d translations`
It's important to compile the files for the transations to take place
For updating messages.pot with all strings for translation - use the following commands:

1. Go to anyway container: `docker exec -it anyway bash`
2. Perform the following updates inside anyway container:
- `pybabel extract -F babel.cfg -o messages.pot .`
- For updating existing po files with new strings: `pybabel update -i messages.pot -d translations`
- Update manually the translations: modify translation files - po files per language.
- For compiling pybabel mo files use: `pybabel compile -d translations`
_It's important to compile the files for the transations to take place_
**Make sure to add all po, mo and pot files to pull request**
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Contributing

## Getting the code

### If you are setting up anyway on Windows using WSL - PLEASE MAKE SURE TO COMPLETE THE FOLLOWING STEPS FROM YOUR WSL TERMINAL!!!
### NOTE: If you are setting up anyway on Windows using WSL - PLEASE MAKE SURE TO COMPLETE THE FOLLOWING STEPS FROM YOUR WSL TERMINAL!!!

1. [Fork](https://github.com/data-for-change/anyway/fork) this repository on GitHub
1. `git clone https://github.com/*you*/anyway`
Expand Down
36 changes: 27 additions & 9 deletions anyway/flask_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
get_downloaded_data,
DEFAULT_LIMIT_REQ_PARAMETER,
DEFAULT_OFFSET_REQ_PARAMETER,
DEFAULT_NUMBER_OF_YEARS_AGO
DEFAULT_NUMBER_OF_YEARS_AGO,
)
from anyway.views.schools.api import (
schools_description_api,
Expand Down Expand Up @@ -321,6 +321,7 @@ def schools():
else:
return Response("Method Not Allowed", 405)


@app.route("/markers", methods=["GET"])
def markers():
logging.debug("getting markers")
Expand Down Expand Up @@ -357,6 +358,7 @@ def markers():
accident_markers, rsa_markers, discussions, is_thin, total_records=result.total_records
)


@app.route("/markers_by_yishuv_symbol", methods=["GET"])
def markers_by_yishuv_symbol():
logging.debug("getting markers by yishuv symbol")
Expand Down Expand Up @@ -1201,9 +1203,12 @@ def get(self):
Returns infographics-data API
"""
parser = reqparse.RequestParser()
parser.add_argument("id", type=int, help="News flash id")
parser.add_argument("news_flash_id", type=int, help="News flash id")
parser.add_argument(
"years_ago", type=int, default=DEFAULT_NUMBER_OF_YEARS_AGO, help=f"Number of years back to consider accidents. Default is {DEFAULT_NUMBER_OF_YEARS_AGO} years"
"years_ago",
type=int,
default=DEFAULT_NUMBER_OF_YEARS_AGO,
help=f"Number of years back to consider accidents. Default is {DEFAULT_NUMBER_OF_YEARS_AGO} years",
)
parser.add_argument("lang", type=str, default="he", help="Language")

Expand Down Expand Up @@ -1261,7 +1266,10 @@ def gps_to_cbs_location():
idbl_parser = reqparse.RequestParser()
idbl_parser.add_argument("road_segment_id", type=int, help="Road Segment id")
idbl_parser.add_argument(
"years_ago", type=int, default=DEFAULT_NUMBER_OF_YEARS_AGO, help=f"Number of years back to consider accidents. Default is {DEFAULT_NUMBER_OF_YEARS_AGO} years"
"years_ago",
type=int,
default=DEFAULT_NUMBER_OF_YEARS_AGO,
help=f"Number of years back to consider accidents. Default is {DEFAULT_NUMBER_OF_YEARS_AGO} years",
)
idbl_parser.add_argument("lang", type=str, default="he", help="Language")

Expand Down Expand Up @@ -1462,6 +1470,7 @@ def post(self):
"road_segment_id", type=int, required=True, help="road segment id"
)


@api.route("/api/streets")
@api.expect(get_streets_parser)
class GetAllStreetsOfYishuv(Resource):
Expand Down Expand Up @@ -1500,17 +1509,26 @@ def get(self):


download_data_parser = reqparse.RequestParser()
download_data_parser.add_argument("format", type=str, default="csv",
help="Format for downloaded data (.csv/.xlsx)")
download_data_parser.add_argument("years_ago", type=int, default=DEFAULT_NUMBER_OF_YEARS_AGO,
help=f"Number of years back to consider accidents. Default is {DEFAULT_NUMBER_OF_YEARS_AGO} years")
download_data_parser.add_argument(
"format", type=str, default="csv", help="Format for downloaded data (.csv/.xlsx)"
)
download_data_parser.add_argument(
"years_ago",
type=int,
default=DEFAULT_NUMBER_OF_YEARS_AGO,
help=f"Number of years back to consider accidents. Default is {DEFAULT_NUMBER_OF_YEARS_AGO} years",
)
"""
Download accidents data with regards to news flash/location
"""


@api.route("/api/download-data", methods=["GET"])
class DownloadData(Resource):
@api.doc("download data")
@api.expect(parser)
def get(self):
args = download_data_parser.parse_args()
return get_downloaded_data(args.get('format', 'csv'), args.get('years_ago', DEFAULT_NUMBER_OF_YEARS_AGO))
return get_downloaded_data(
args.get("format", "csv"), args.get("years_ago", DEFAULT_NUMBER_OF_YEARS_AGO)
)
5 changes: 0 additions & 5 deletions anyway/infographics_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ def get_request_params(
location_info = extract_news_flash_location(news_flash_obj)
if location_info is None:
return None
logging.debug("location_info:{}".format(location_info))
location_text = get_news_flash_location_text(news_flash_obj)
logging.debug("location_text:{}".format(location_text))
gps = location_info["gps"]
Expand Down Expand Up @@ -137,7 +136,6 @@ def get_request_params(
lang=lang,
news_flash_description=news_flash_description
)
logging.debug(f"Ending get_request_params. params: {request_params}")
return request_params


Expand All @@ -148,7 +146,6 @@ def create_infographics_data(news_flash_id, number_of_years_ago, lang: str) -> s


def create_infographics_data_for_location(vals: dict) -> str:
logger.debug(f"create_infographics_data_for_location({vals})")
try:
request_params = get_request_params_from_request_values(vals)
output = create_infographics_items(request_params)
Expand Down Expand Up @@ -181,8 +178,6 @@ def get_dates_comment():
return {}
if number_of_years_ago < 0 or number_of_years_ago > 100:
return {}
logging.debug("location_info:{}".format(request_params.location_info))
logging.debug("location_text:{}".format(request_params.location_text))
output["meta"] = {
"location_info": request_params.location_info.copy(),
"location_text": request_params.location_text,
Expand Down
2 changes: 1 addition & 1 deletion anyway/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,7 @@ def set_critical(
suburban_road_killed_value=3,
urban_severe_value=2,
):
from anyway.widgets.road_segment_widgets.injured_count_by_severity_widget import (
from anyway.widgets.all_locations_widgets.injured_count_by_severity_widget import (
InjuredCountBySeverityWidget,
)
from anyway.request_params import get_latest_accident_date
Expand Down
12 changes: 8 additions & 4 deletions anyway/request_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ class RequestParams:
start_time: datetime.date
end_time: datetime.date
lang: str
news_flash_description: Optional[str]
news_flash_description: Optional[str] = None
news_flash_title: Optional[str] = None

def __str__(self):
return (
Expand All @@ -54,6 +55,11 @@ def get_request_params_from_request_values(vals: dict) -> Optional[RequestParams
if news_flash_obj is not None and news_flash_obj.description is not None
else None
)
news_flash_title = (
news_flash_obj.title
if news_flash_obj is not None and news_flash_obj.title is not None
else None
)
location = get_location_from_news_flash_or_request_values(news_flash_obj, vals)
if location is None:
return None
Expand All @@ -65,8 +71,6 @@ def get_request_params_from_request_values(vals: dict) -> Optional[RequestParams

if location_info is None:
return None
logging.debug("location_info:{}".format(location_info))
logging.debug("location_text:{}".format(location_text))
resolution = location_info.pop("resolution")
if resolution is None or resolution not in BE_CONST.SUPPORTED_RESOLUTIONS:
logging.error(f"Resolution empty or not supported: {resolution}.")
Expand Down Expand Up @@ -98,8 +102,8 @@ def get_request_params_from_request_values(vals: dict) -> Optional[RequestParams
end_time=end_time,
lang=lang,
news_flash_description=news_flash_description,
news_flash_title=news_flash_title,
)
logging.debug(f"Ending get_request_params. params: {request_params}")
return request_params


Expand Down
5 changes: 4 additions & 1 deletion anyway/widgets/all_locations_widgets/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from . import (
accident_count_by_severity_widget,
injured_count_by_severity_widget,
most_severe_accidents_widget,
most_severe_accidents_table_widget,
seriously_injured_killed_in_bicycles_scooter_widget
seriously_injured_killed_in_bicycles_scooter_widget,
killed_and_injured_count_per_age_group_stacked_widget,
killed_and_injured_count_per_age_group_widget
)
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
from typing import Dict

from anyway.request_params import RequestParams
from anyway.backend_constants import InjurySeverity
from anyway.models import InvolvedMarkerView
from anyway.widgets.all_locations_widgets.all_locations_widget import AllLocationsWidget
from anyway.widgets.widget import register
from anyway.widgets.road_segment_widgets.road_segment_widget import RoadSegmentWidget
from anyway.widgets.widget_utils import get_accidents_stats, join_strings
from anyway.widgets.widget_utils import get_accidents_stats, join_strings, get_location_text
from anyway.backend_constants import BE_CONST
from flask_babel import _


@register
class InjuredCountBySeverityWidget(RoadSegmentWidget):
class InjuredCountBySeverityWidget(AllLocationsWidget):
name: str = "injured_count_by_severity"
files = [__file__]

Expand All @@ -22,25 +21,31 @@ def __init__(self, request_params: RequestParams):

def generate_items(self) -> None:
self.items = InjuredCountBySeverityWidget.get_injured_count_by_severity(
self.request_params.location_info["road1"],
self.request_params.location_info["road_segment_name"],
self.request_params.resolution,
self.request_params.location_info,
self.request_params.start_time,
self.request_params.end_time,
)

@staticmethod
def get_injured_count_by_severity(road, segment, start_time, end_time):
def get_injured_count_by_severity(resolution, location_info, start_time, end_time):
filters = {}
filters["injury_severity"] = [
InjurySeverity.KILLED.value,
InjurySeverity.SEVERE_INJURED.value,
InjurySeverity.LIGHT_INJURED.value,
]

if resolution == BE_CONST.ResolutionCategories.STREET:
filters["involve_yishuv_name"] = location_info.get("yishuv_name")
filters["street1_hebrew"] = location_info.get("street1_hebrew")
elif resolution == BE_CONST.ResolutionCategories.SUBURBAN_ROAD:
filters["road1"] = location_info.get("road1")
filters["road_segment_name"] = location_info.get("road_segment_name")

count_by_severity = get_accidents_stats(
table_obj=InvolvedMarkerView,
filters={
"injury_severity": [
InjurySeverity.KILLED.value,
InjurySeverity.SEVERE_INJURED.value,
InjurySeverity.LIGHT_INJURED.value,
],
"road1": road,
"road_segment_name": segment,
},
filters=filters,
group_by="injury_severity",
count="injury_severity",
start_time=start_time,
Expand Down Expand Up @@ -68,49 +73,50 @@ def get_injured_count_by_severity(road, segment, start_time, end_time):
items["total_injured_count"] = total_injured_count
return items


@staticmethod
def get_transcription(request_params: RequestParams, items: Dict):
total_injured_count = items.get("total_injured_count")
if total_injured_count == 0:
return ''
return ""
severity_light_count = items.get("light_injured_count")
if severity_light_count == 0:
severity_light_count_text = ''
severity_light_count_text = ""
elif severity_light_count == 1:
severity_light_count_text = _("one light injured")
else:
severity_light_count_text = f'{severity_light_count} ' + _("light injured plural")
severity_light_count_text = f"{severity_light_count} " + _("light injured plural")
severity_severe_count = items.get("severe_injured_count")
if severity_severe_count == 0:
severity_severe_count_text = ''
severity_severe_count_text = ""
elif severity_severe_count == 1:
severity_severe_count_text = _("one severe injured")
else:
severity_severe_count_text = f'{severity_severe_count} '+ _("severe injured plural")
severity_severe_count_text = f"{severity_severe_count} " + _("severe injured plural")
killed_count = items.get("killed_count")
if killed_count == 0:
killed_count_text = ''
killed_count_text = ""
elif killed_count == 1:
killed_count_text = _("one killed")
else:
killed_count_text = f'{killed_count} ' + _("killed plural")
killed_count_text = f"{killed_count} " + _("killed plural")
if request_params.resolution == BE_CONST.ResolutionCategories.STREET:
text = "{in_yishuv_keyword} {yishuv_name} {in_street_keyword} {street_name} ".format(
in_yishuv_keyword=_("in yishuv"),
yishuv_name=_(request_params.location_info.get('yishuv_name')),
yishuv_name=_(request_params.location_info.get("yishuv_name")),
in_street_keyword=_("in street"),
street_name=_(request_params.location_info.get('street1_hebrew')),
street_name=_(request_params.location_info.get("street1_hebrew")),
)
elif request_params.resolution == BE_CONST.ResolutionCategories.SUBURBAN_ROAD:
text = "{in_road_keyword} {road_num} {in_segment_keyword} {segment_name} ".format(
in_road_keyword=_("in road"),
road_num=request_params.location_info.get('road1'),
road_num=request_params.location_info.get("road1"),
in_segment_keyword=_("in segment"),
segment_name=_(request_params.location_info.get('road_segment_name')),
segment_name=_(request_params.location_info.get("road_segment_name")),
)
else:
raise Exception(f"cannot convert to hebrew for resolution : {request_params.resolution.get('resolution')}")
raise Exception(
f"cannot convert to hebrew for resolution : {request_params.resolution.get('resolution')}"
)
text += "{between_years_keyword} {start_year} - {end_year}, {injured_killed_keyword} {injured_num} {people_phrase}, {out_of_them_keywoard} ".format(
between_years_keyword=_("between the years"),
start_year=request_params.start_time.year,
Expand All @@ -120,19 +126,22 @@ def get_transcription(request_params: RequestParams, items: Dict):
people_phrase=_("people from car accidents"),
out_of_them_keywoard=_("out of them"),
)
text += join_strings([killed_count_text, severity_severe_count_text, severity_light_count_text],
sep_a=" ,",
sep_b=_(" and "))
text += join_strings(
[killed_count_text, severity_severe_count_text, severity_light_count_text],
sep_a=" ,",
sep_b=_(" and "),
)
return text


@staticmethod
def localize_items(request_params: RequestParams, items: Dict) -> Dict:
subtitle = get_location_text(request_params)
items["data"]["text"] = {
"title": _("Number of Injuries in accidents by severity"),
"subtitle": _(request_params.location_info['road_segment_name']),
"transcription": InjuredCountBySeverityWidget.get_transcription(request_params=request_params,
items=items["data"]["items"])
"subtitle": _(subtitle),
"transcription": InjuredCountBySeverityWidget.get_transcription(
request_params=request_params, items=items["data"]["items"]
),
}
return items

Expand All @@ -144,4 +153,4 @@ def localize_items(request_params: RequestParams, items: Dict) -> Dict:
_("one severe injured")
_("severe injured plural")
_("one light injured")
_("light injured plural")
_("light injured plural")
Loading

0 comments on commit 8b40bd8

Please sign in to comment.