Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infinite loop on the get_available_time function #42

Closed
iamfeysal opened this issue Dec 22, 2022 · 5 comments
Closed

Infinite loop on the get_available_time function #42

iamfeysal opened this issue Dec 22, 2022 · 5 comments

Comments

@iamfeysal
Copy link

@foad-heidari Hello, if I try to edit the period_of_each_booking field on the BookingSettings model to I hour through the admin and try to access the time in the template when I ran the app, it does not render and instead goes to an infinite loop. I investigated the issue and found out it is coming from the get_available_time function on the if next_time > booking_settings.end_time: break section. for some reason, the time_list doesn't break because next_time is not greater than booking_settings.end_time. please check it and let me know if it's also occurring on your side and anyone else reading this.

The infinite loop only occurs when I set the period_of_each_booking field on the BookingSettings to 1 hour or to 2 hours or to 3 hours in the admin

def add_delta(time: datetime.time, delta: datetime.datetime) -> datetime.time:
 # transform to a full datetime first
return (datetime.datetime.combine(
        datetime.date.today(), time
    ) + delta).time() 

def get_available_time(date: datetime.date) -> List[Dict[datetime.time, bool]]:
    """
    Check for all available time for selected date
    The times should ne betwwen start_time and end_time
    If the time already is taken -> is_taken = True
    """
    booking_settings = BookingSettings.objects.first()
    existing_bookings = Booking.objects.filter(
        date=date).values_list('time')
    max_booking_per_time = booking_settings.max_booking_per_time

    next_time = booking_settings.start_time
    time_list = []
    while True:
        is_taken = False  # Add this line
        if existing_bookings.count() == max_booking_per_time:  # Add this if
            is_taken = any([x[0] == next_time for x in existing_bookings])
        time_list.append(
            {"time": ":".join(str(next_time).split(":")[:-1]),
             "is_taken": is_taken})
        next_time = add_delta(next_time, datetime.timedelta(
            minutes=int(booking_settings.period_of_each_booking)))
        if next_time > booking_settings.end_time:
            break

    return time_list


@foad-heidari
Copy link
Owner

hello @iamfeysal thanks for your report
it works for me , can you provide more information?
and send me the step by step what you do?
and send me the error you get

@iamfeysal
Copy link
Author

hello @iamfeysal thanks for your report it works for me , can you provide more information? and send me the step by step what you do? and send me the error you get

@foad-heidari sure. you can please check the below video for more information of the error

infinite.loop.mp4

@foad-heidari
Copy link
Owner

foad-heidari commented Dec 25, 2022

hello @iamfeysal thanks for your report it works for me , can you provide more information? and send me the step by step what you do? and send me the error you get

@foad-heidari sure. you can please check the below video for more information of the error

infinite.loop.mp4

Thanks for the information, i will check it.
But you can try to use the admin optionsform "/booking/admin"

Hopefully it will work for you until it will be fixed

@foad-heidari
Copy link
Owner

foad-heidari commented Dec 25, 2022

@iamfeysal okay so your end time is 23:00 and each booking is 1 hour.

When the while loop get to 00:00, the if state ment is not working

"If (00:00 > 23:00) break"

So it will never stop the loop.

You can change the code to

"If (next_time => booking_settings.end_time) break"

And move it one line before updating the next_time

@iamfeysal
Copy link
Author

@iamfeysal okay so your end time is 23:00 and each booking is 1 hour.

When the while loop get to 00:00, the if state ment is not working

"If (00:00 > 23:00) break"

So it will never stop the loop.

You can change the code to

"If (next_time => booking_settings.end_time) break"

And move it one line before updating the next_time

@foad-heidari yeah, I have been using that since then. it worked only when I set in I hours but I checked the admin optionsform "/booking/admin" and it seems to work there for even 2 hours and beyond. No idea why it does not work when i use the django admin section though

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants