[Solved]-Django calendar free/busy/availabilitty

16👍

What about using Django’s range test.

For example:

appoinment = Appointment()
appointment.start_time = datetime.datetime.now()
# 1 hour appointment
appointment.end_time = appointment.start_time + datetime.timedelta(hours=1)
# more stuff here
appointment.save()

# Checking for collision
# where the start time for an appointment is between the the start and end times
# You would want to filter this on user, etc 
# There is also a problem if you book an appointment within another appointment
start_conflict = Appointment.objects.filter(
                     start_time__range=(appointment.start_time,
                                        appointment.end_time))
end_conflict = Appointment.objects.filter(
                   end_time__range=(appointment.start_time,
                                    appointment.end_time))

during_conflict = Appointment.objects.filter(
                      start_date__lte=appointment.start_time, 
                      end_date__gte=appointment.end_time)

if (start_conflict or end_conflict or during_conflict):
    # reject, for there is a conflict

Something like that? I haven’t tried this myself so you may have to tweak it a bit.

EDIT: Added the during_conflict bit.

0👍

One caveat here is the different timezones of different users, and bring Daylight saving time into the mix things become very complicated.

You might want to take a look at pytz module for taking care of the timezone issue.

Leave a comment