63 lines
1.9 KiB
Python
63 lines
1.9 KiB
Python
import asyncio
|
|
import logging
|
|
|
|
import config
|
|
from gestion_sports.gestion_sports_platform import GestionSportsPlatform
|
|
from models import BookingFilter, Club, User
|
|
|
|
LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
async def book(club: Club, user: User, booking_filter: BookingFilter) -> int | None:
|
|
"""
|
|
Book a court for a user to a club following a booking filter
|
|
|
|
:param club: the club where to book a court
|
|
:param user: the user information
|
|
:param booking_filter: the information related to the booking
|
|
:return: the id of the booked court, or None if no court was booked
|
|
"""
|
|
async with GestionSportsPlatform(club) as platform:
|
|
return await platform.book(user, booking_filter)
|
|
|
|
|
|
async def get_user_without_booking(club: Club, users: list[User]) -> User | None:
|
|
"""
|
|
Return the first user who has no booking
|
|
|
|
:param club: the club where to book
|
|
:param users: the list of users
|
|
:return: any user who has no booking
|
|
"""
|
|
async with GestionSportsPlatform(club) as platform:
|
|
for user in users:
|
|
if await platform.user_has_no_ongoing_booking(user):
|
|
return user
|
|
return None
|
|
|
|
|
|
def main() -> int | None:
|
|
"""
|
|
Main function used to book a court
|
|
|
|
:return: the id of the booked court, or None if no court was booked
|
|
"""
|
|
booking_filter = config.get_booking_filter()
|
|
club = config.get_club()
|
|
user = asyncio.run(get_user_without_booking(club, config.get_available_users()))
|
|
|
|
LOGGER.info(
|
|
"Starting booking court of sport %s for user %s at club %s at %s",
|
|
booking_filter.sport_id,
|
|
user.login,
|
|
club.id,
|
|
booking_filter.date,
|
|
)
|
|
court_booked = asyncio.run(book(club, user, booking_filter))
|
|
if court_booked:
|
|
LOGGER.info(
|
|
"Court %s booked successfully at %s", court_booked, booking_filter.date
|
|
)
|
|
else:
|
|
LOGGER.info("Booking did not work")
|
|
return court_booked
|