Skip to content

Commit

Permalink
fix black issues
Browse files Browse the repository at this point in the history
  • Loading branch information
EliorGigi committed Dec 22, 2023
1 parent 090f220 commit 9737275
Show file tree
Hide file tree
Showing 43 changed files with 1,220 additions and 800 deletions.
3 changes: 2 additions & 1 deletion anyway/backend_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class ResolutionCategories(Enum):
SUPPORTED_RESOLUTIONS: List[ResolutionCategories] = [
ResolutionCategories.STREET,
ResolutionCategories.SUBURBAN_ROAD,
ResolutionCategories.SUBURBAN_JUNCTION
ResolutionCategories.SUBURBAN_JUNCTION,
]

class Source(Enum):
Expand Down Expand Up @@ -110,6 +110,7 @@ def _missing_(cls, value):

YISHUV_NAME = "yishuv_name"


BE_CONST = BackEndConstants()


Expand Down
938 changes: 592 additions & 346 deletions anyway/db_views.py

Large diffs are not rendered by default.

39 changes: 27 additions & 12 deletions anyway/flask_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@
from anyway.clusters_calculator import retrieve_clusters
from anyway.config import ENTRIES_PER_PAGE
from anyway.constants import CONST
from anyway.infographics_utils import (
get_infographics_mock_data,
get_infographics_data_for_location,
)
from anyway.infographics_utils import get_infographics_mock_data, get_infographics_data_for_location


from anyway.models import (
Expand Down Expand Up @@ -68,7 +65,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 +318,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 +355,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 @@ -1196,7 +1195,10 @@ def get(self):
parser = reqparse.RequestParser()
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 @@ -1254,7 +1256,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 @@ -1455,6 +1460,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 @@ -1499,17 +1505,26 @@ def put(self, id):


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)
)
27 changes: 14 additions & 13 deletions anyway/infographic_image_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
NEWSFLASH_PAGE_BASE_URL = "https://media.anyway.co.il/newsflash"
IMAGES_DOWNLOAD_PATH_IN_CONTAINER = "/var/selenium/tempdata"

selenium_url = secrets.get('SELENIUM_URL')
selenium_url = secrets.get("SELENIUM_URL")
selenium_hub_url = f"https://{selenium_url}/wd/hub"
selenium_remote_results_url = f"https://{selenium_url}/tempdata"
CHROME_PARTIALLY_DOWNLOADED_FILE_EXTENSION = "crdownload"
Expand All @@ -29,10 +29,7 @@ def create_chrome_browser_session(newsflash_id):
}
options.add_experimental_option("prefs", prefs)

browser = webdriver.Remote(
command_executor=selenium_hub_url,
options=options
)
browser = webdriver.Remote(command_executor=selenium_hub_url, options=options)
return browser


Expand Down Expand Up @@ -66,17 +63,18 @@ def contains_partial_files(filenames):


def fetch_generated_image_filenames_for_newsflash(newsflash_id):
contents = requests.get(
f"{selenium_remote_results_url}/{newsflash_id}/").json()
filenames = [item['name'] for item in contents]
contents = requests.get(f"{selenium_remote_results_url}/{newsflash_id}/").json()
filenames = [item["name"] for item in contents]
return get_unique_filenames(filenames)


def wait_for_folder_to_contain_all_files(newsflash_id, number_of_expected_files, timeout):
for _ in range(timeout):
time.sleep(1)
image_filenames = fetch_generated_image_filenames_for_newsflash(newsflash_id)
if len(image_filenames) == number_of_expected_files and not contains_partial_files(image_filenames):
if len(image_filenames) == number_of_expected_files and not contains_partial_files(
image_filenames
):
return True, image_filenames
return False, []

Expand All @@ -94,9 +92,10 @@ def generate_infographics_in_selenium_container(browser, newsflash_id):
if buttons_found > 0:
for element in elements:
ActionChains(browser).move_to_element(element).click().perform()
time.sleep(1) #prevents click arriving before the last finished
is_download_done, generated_images_names = wait_for_folder_to_contain_all_files(newsflash_id,
buttons_found, timeout=60)
time.sleep(1) # prevents click arriving before the last finished
is_download_done, generated_images_names = wait_for_folder_to_contain_all_files(
newsflash_id, buttons_found, timeout=60
)
except Exception as e:
logging.error(e)
finally:
Expand All @@ -113,7 +112,9 @@ def upload_infographics_images_to_s3(newsflash_id, should_download=True):
local_infographics_folder = get_local_infographics_folder_name(newsflash_id)
if should_download:
generated_images_names = fetch_generated_image_filenames_for_newsflash(newsflash_id)
download_infographics_images(generated_images_names, newsflash_id, local_infographics_folder)
download_infographics_images(
generated_images_names, newsflash_id, local_infographics_folder
)
upload_directory_to_s3(local_infographics_folder, newsflash_id)


Expand Down
3 changes: 2 additions & 1 deletion anyway/infographics_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import anyway.widgets.road_segment_widgets
import anyway.widgets.all_locations_widgets
import anyway.widgets.no_location_widgets

# pylint: enable=unused-import

logger = logging.getLogger("infographics_utils")
Expand Down Expand Up @@ -135,7 +136,7 @@ def get_request_params(
start_time=start_time,
end_time=end_time,
lang=lang,
news_flash_description=news_flash_description
news_flash_description=news_flash_description,
)
logging.debug(f"Ending get_request_params. params: {request_params}")
return request_params
Expand Down
Loading

0 comments on commit 9737275

Please sign in to comment.