Hello everyone, I have a problem related to booking a time taking into account the coach’s training segment. when I want to choose a date for booking, for example, it tells me that there are no available times for M.D.Y
.
what do I need to do or how to implement the method so that I can select the date and time for booking taking into account the coach’s time period, that is, not from 00:00 to 23:45, and for example from 11:00 to 18:00, depending on coach and what is his schedule
if request.method == 'GET':
current_trainer_description = TrainerDescription.objects.get(id=trainer_id)
specific_service = Service.objects.get(id=service_id, trainer=current_trainer_description.trainer)
selected_date_str = request.GET.get('training_date')
selected_date = None
if selected_date_str:
selected_date = datetime.strptime(selected_date_str, "%Y-%m-%d").date()
print(f"Selected Date: {selected_date}")
schedules = TrainerSchedule.objects.filter(
trainer=current_trainer_description.trainer,
datetime_start__date=selected_date
)
print(f"Schedules: {schedules}")
available_times = []
for schedule in schedules:
start_time = schedule.datetime_start
end_time = schedule.datetime_end
if isinstance(start_time, int):
start_time = datetime.fromtimestamp(start_time / 1000)
if isinstance(end_time, int):
end_time = datetime.fromtimestamp(end_time / 1000)
available_times.append((start_time, end_time))
print(f"Available times: {available_times}")
context = {
'available_times': available_times,
'specific_service': specific_service,
'selected_date': selected_date,
}
return render(request, 'trainer_service_page.html', context)