I am writing an application for time_slot in python
please look at this code
from datetime import datetime ,timedelta
appointments = [(datetime(2012, 5, 22, 10), datetime(2012, 5, 22, 10, 30)),
(datetime(2012, 5, 22, 12), datetime(2012, 5, 22, 13)),
(datetime(2012, 5, 22, 15, 30), datetime(2012, 5, 22, 17, 10))]
hours = (datetime(2012, 5, 22, 9), datetime(2012, 5, 24, 18))
duration = timedelta(minutes = 120)
def get_slots(hours, appointments,duration):
slots = sorted([(hours[0], hours[0])] + appointments + [(hours[1], hours[1])])
for start, end in ((slots[i][1], slots[i+1][0]) for i in range(len(slots)-1)):
assert start <= end, "Cannot attend all appointments"
while start + duration <= end:
dt_obj = start
date_str = dt_obj.strftime("%Y-%m-%d %H:%M:%S")
today18 = start.replace(hour=17, minute=59, second=59, microsecond=0)
if start < today18:
print "{:%d:%H:%M} - {:%d:%H:%M}".format(start, start + duration)
start += duration
else:
start += timedelta(hours = 15)
if __name__ == "__main__":
get_slots(hours, appointments,duration)
The above code is working fine for me when i am running it like python time_slots.py
But when i am using above code in djanog view like
from datetime importdatetime ,timedelta
duration = timedelta(minutes = 60)
appointments = [(datetime(2012, 5, 22, 10), datetime(2012, 5, 22, 10, 30)),
(datetime(2012, 5, 22, 12), datetime(2012, 5, 22, 13)),
(datetime(2012, 5, 22, 15, 30), datetime(2012, 5, 22, 17, 10))]
# hours = (datetime.datetime(2012, 5, 24, 18), datetime.datetime(2012, 5, 22, 9))
hours = (datetime(2012, 5, 22, 9), datetime(2012, 5, 24, 18))
slots = sorted([(hours[0], hours[0])] + appointments + [(hours[1], hours[1])])
for start, end in ((slots[i][1], slots[i+1][0]) for i in range(len(slots)-1)):
assert start <= end, "Cannot attend all appointments"
while start + duration <= end:
today18 = start.replace(hour=17, minute=59, second=59, microsecond=0)
if start < today18:
print "{:%H:%M} - {:%H:%M}".format(start, start + duration)
else:
start += timedelta(hours = 15)
and when i am calling this view it’s returning an infinite loop in console
Please help me what i am doing wrong here i want the available time slots as i am getting in time_slots.py
In the Django view, you have forgotten to add duration to start in the
if start < today18clause.