forked from Support/sp-hydra-veil-gui
88 lines
2.7 KiB
Python
88 lines
2.7 KiB
Python
from core.models.orm_calls.location_calls import execute_all_locations
|
|
from core.models.enum_models import ProtocolChoice
|
|
from core.errors.logger import logger
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
def get_all_locations():
|
|
"""Use core to fetch all locations with SQL"""
|
|
location_object = execute_all_locations()
|
|
|
|
if not location_object.valid:
|
|
critical_error = f"Got invalid SQL Query which could not be solved by the wrapper, with error message {location_object.message} and type {location_object.error_type}"
|
|
logger.error(critical_error)
|
|
return None
|
|
|
|
list_of_all_locations = location_object.data
|
|
return list_of_all_locations
|
|
|
|
|
|
def get_location_display_data(button_path: str) -> dict|None:
|
|
"""Organize display data for the UI"""
|
|
all_locations = get_all_locations()
|
|
|
|
if all_locations is None:
|
|
return None
|
|
|
|
location_display_dto = {}
|
|
|
|
for each_location in all_locations:
|
|
location_id = each_location.id
|
|
location_name = each_location.country_name
|
|
country_code = each_location.country_code
|
|
provider = each_location.operator.name
|
|
code = each_location.code
|
|
button_image_path = f"button_{country_code}_{code}.png"
|
|
|
|
full_image_path = Path(button_path, button_image_path)
|
|
default_path = Path(button_path, "default_location_button.png")
|
|
|
|
data = {
|
|
'path': default_path,
|
|
'name': location_name,
|
|
'second_line': provider,
|
|
'existing_icon': full_image_path
|
|
}
|
|
|
|
location_display_dto[location_id] = data
|
|
|
|
return location_display_dto
|
|
|
|
|
|
def get_location_icon_path(image_path: Path, country_code: str, code: str, larger_version: bool = False):
|
|
"""Return a Path for the location's icon, with the default if not found."""
|
|
|
|
if larger_version:
|
|
icon_image_path = f"icon_{country_code}_{code}.png"
|
|
else:
|
|
icon_image_path = f"icon_mini_{country_code}_{code}.png"
|
|
|
|
print(f"icon_image_path is {icon_image_path}")
|
|
full_image_path = Path(image_path, icon_image_path)
|
|
|
|
if full_image_path.exists():
|
|
return full_image_path
|
|
else:
|
|
return Path(image_path, "default_location_mini.png")
|
|
|
|
|
|
def get_larger_protocol_image(image_path: Path, country_code: str, code: str, protocol: ProtocolChoice):
|
|
"""Return a Path for the location's full screen image by protocol, with None if not found."""
|
|
|
|
if protocol is None:
|
|
return None
|
|
|
|
elif isinstance(protocol, ProtocolChoice):
|
|
protocol = protocol.value
|
|
|
|
icon_image_path = f"{protocol}_{country_code}_{code}.png"
|
|
|
|
print(f"icon_image_path is {icon_image_path}")
|
|
full_image_path = Path(image_path, icon_image_path)
|
|
|
|
if full_image_path.exists():
|
|
return full_image_path
|
|
else:
|
|
return None
|
|
|