Big refactoring.

- clubs, booking platforms and user are now defined in customization files -> there are less environment variables
- the responsibility of the session moved
- booking cancellation is available
This commit is contained in:
Stanislas Jouffroy 2024-03-17 23:57:50 +01:00 committed by stanislas
parent dbda5a158e
commit 0938fb98b7
27 changed files with 3050 additions and 696 deletions

View file

@ -6,36 +6,11 @@ from pathlib import Path
import pendulum
import yaml
from dotenv import load_dotenv
from models import BookingFilter, Club, User
from models import Action, BookingFilter, Club, User
load_dotenv()
def get_club() -> Club:
"""
Read the environment variables related to the current club
and build the Club object
:return: the club
"""
club_url = os.environ.get("CLUB_URL")
court_ids_tmp = os.environ.get("COURT_IDS") or ""
court_ids = (
[int(court_id) for court_id in court_ids_tmp.split(",")]
if court_ids_tmp
else []
)
club_id = os.environ.get("CLUB_ID")
booking_open_days_before = int(os.environ.get("BOOKING_OPEN_DAYS_BEFORE", "7"))
booking_opening_time_str = os.environ.get("BOOKING_OPENING_TIME", "00:00")
booking_opening_time = pendulum.parse(booking_opening_time_str)
return Club(
id=club_id,
url=club_url,
courts_ids=court_ids,
booking_open_days_before=booking_open_days_before,
booking_opening_time=booking_opening_time.time(),
)
ROOT_DIR = Path(__file__).parent
def get_booking_filter() -> BookingFilter:
@ -45,11 +20,10 @@ def get_booking_filter() -> BookingFilter:
:return: the club
"""
sport_id_tmp = os.environ.get("SPORT_ID")
sport_id = int(sport_id_tmp) if sport_id_tmp else None
sport_name = os.environ.get("SPORT_NAME")
date_time_tmp = os.environ.get("DATE_TIME")
date_time = pendulum.parse(date_time_tmp) if date_time_tmp else None
return BookingFilter(sport_id=sport_id, date=date_time)
return BookingFilter(sport_name=sport_name.lower(), date=date_time)
def get_user() -> User:
@ -64,25 +38,6 @@ def get_user() -> User:
return User(login=login, password=password)
def get_available_users() -> list[User]:
"""
Read the environment variables to get all the available users in order
to increase the chance of having a user with a free slot for a booking
:return: the list of all users that can book a court
"""
available_users_credentials = os.environ.get("AVAILABLE_USERS_CREDENTIALS")
available_users = [
credential for credential in available_users_credentials.split(",")
]
users = []
for user in available_users:
login, password = user.split(":")
users.append(User(login=login, password=password))
return users
def get_post_headers(platform_id: str) -> dict:
"""
Get the headers for the POST endpoint related to a specific booking platform
@ -102,13 +57,90 @@ def init_log_config():
"""
Read the logging.yaml file to initialize the logging configuration
"""
root_dir = os.path.realpath(os.path.dirname(__file__))
logging_file = root_dir + "/logging.yaml"
logging_file = ROOT_DIR / "logging.yaml"
with open(logging_file, "r") as f:
with logging_file.open(mode="r", encoding="utf-8") as f:
logging_config = yaml.safe_load(f.read())
logging.config.dictConfig(logging_config)
logging.config.dictConfig(logging_config)
ROOT_PATH = Path(__file__).parent.resolve()
RESOURCES_DIR = Path(ROOT_PATH, "resources")
def _build_urls(platform_urls: dict) -> dict:
return {url["name"]: url for url in platform_urls}
def _read_clubs(platforms_data: dict, clubs_data: dict) -> dict[str, Club]:
platforms = {platform["id"]: platform for platform in platforms_data}
for club in clubs_data["clubs"]:
club_platform = club["bookingPlatform"]
platform_id = club_platform["id"]
club_platform["urls"] = _build_urls(platforms[platform_id]["urls"])
return {club["id"]: Club(**club) for club in clubs_data["clubs"]}
def get_clubs():
platforms_file = ROOT_DIR / "resources" / "platforms.yaml"
with platforms_file.open(mode="r", encoding="utf-8") as fp:
platforms_data = yaml.safe_load(fp)
clubs_file = ROOT_DIR / "resources" / "clubs.yaml"
with clubs_file.open(mode="r", encoding="utf-8") as fp:
clubs_data = yaml.safe_load(fp)
return _read_clubs(platforms_data["platforms"], clubs_data)
def get_club() -> Club:
"""
Get the club from an environment variable
:return: the club
"""
club_id = os.environ.get("CLUB_ID")
clubs = get_clubs()
return clubs[club_id]
def read_users(data: dict, club_id: str) -> list[User]:
"""
Deserialize users
:param data: the dictionnary of users
:param club_id: the club id
:return: a list if users from the club
"""
for club in data.get("clubs"):
if club.get("id") == club_id:
return [User(**user) for user in club.get("users")]
def get_users(club_id: str) -> list[User]:
"""
Get a list of users from a club
:param club_id: the club to which the users should have an account
:return: the list of all users for that club
"""
users_file = ROOT_DIR / "resources" / "users.yaml"
with users_file.open(mode="r", encoding="utf-8") as fp:
data = yaml.safe_load(fp)
return read_users(data, club_id)
def get_resources_folder() -> Path:
"""
Compute the path to the resources used by the program
:return: the path to the resources folder
"""
default_resources_folder = Path(__file__).parent / "resources"
return Path(os.environ.get("RESOURCES_FOLDER", default_resources_folder))
def get_action() -> Action:
"""
Get the action to perform from an environment variable
:return: the action to perform
"""
return Action(os.environ.get("ACTION"))