foad-heidari / dj-booking

free and complete booking package for Django
Other
59 stars 28 forks source link

Infinite loop on the get_available_time function #42

Closed iamfeysal closed 6 months ago

iamfeysal commented 1 year ago

@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 commented 1 year ago

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 commented 1 year ago

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

https://user-images.githubusercontent.com/38492170/209449060-460b1ca4-48ca-4655-9dfb-b926b5dbc829.mp4

foad-heidari commented 1 year ago

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

https://user-images.githubusercontent.com/38492170/209449060-460b1ca4-48ca-4655-9dfb-b926b5dbc829.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 commented 1 year ago

@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 commented 1 year ago

@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