Skip to content

How to build a calendar slot booking app in Django?

    Datetime utility functionality is a common programming challenge that most organizations and developers at some point must tackle in order to build slot booking systems with timezone compatibility. 

    In this blog, we will walk you through the process of building a real-time slot booking system with timezone compatibility using the module Django Slot Booking.

    We have implemented this module in various projects to get slot booking done with ease. The module is still being developed to achieve some advanced DateTime-based operational tasks.

    To start building a slot-booking system, clone the slot-booking repo from wolfpack. It is an open-source repo and can be cloned here: https://github.com/wolfpack-studio/slot-booking

    Prerequisites

    To follow along, you will need to have Python version 3.5 or higher installed, along with the pytz and Django libraries. This will allow you to run the sample code and see how some of the libraries work.

    Assume we have two time slots 09:00-11:00 and 12:00-17:00  for the date 25-01-2023.

    First of all, import the slot booking module below,

    >>> from slot_booking.module import *

    Arrange the available slots in a dictionary like this

    >>> avail_slots = { “25-01-2023” : [(09:00, 11:00), (12:00, 17:00)] }
    >>> availa_slots_tz = “Asia/Kolkata”  

    Assume we have some booked slots for the same date,

    >>> booked_slots = { “25-01-2023” : [(10:00, 10:30), (13:00, 14:00), (16:00, 16:30)] }
    >>> booked_slots_tz = “Asia/Kolkata”

    Now since the time zones are the same for both available slots and booked slots, we can validate the remaining slots without converting them

    >>> rem_slots = validate_slots_for_a_date(“25-01-2023”, [(10:00, 10:30), (13:00, 14:00), (16:00, 16:30)], [(09:00, 11:00), (12:00, 17:00)])

    Where the first argument is a date, the second argument is a list of booked slots for that specific date and the third one is available slots. 

    And, in return, we will get the remaining slots,

    >>> print(rem_slots)
    [(09:00, 10:00), (10:30, 11:00), (12:00, 13:00), (14:00, 16:00), (16:30, 17:00)]

    If the time zone of the booked slots is different, we can use  convert_date_timestamp_slot function

    For example, if the time zone of the booked slots is “UTC” and the available slots are in the “Asia/Kolkata” time zone, we can convert the booked slots’ time zone to “Asia/Kolkata” by adding the date and time zone to the timestamps as follows,

    >>> booked_slots = { “25-01-2023” : [(13:00, 15:00)] }
    >>> booked_slots_tz =UTC
    
    >>> converted_slots = convert_date_timestamp_slot ([(13:00 25-01-2023 5:30+, 15:00 25-01-2023 5:30+)], “Asia/Kolkata”)

    Here, the first argument is a list of timezone aware DateTime slots and the second argument is the time zone that the slots need to be converted to.

    >>> print(converted_slots)
    [(07:30, 09:00)]

    And then later we can convert them with convert_date_timestamp_slot  function.

    We can achieve the date overrides by replacing the date as a key from the dictionary that we are passing.

    Summary

    We hope the above tutorial helps to get started with slot booking. If you need assistance finding a sophisticated scheduling application built with Django, please contact us. We would be happy to assist you.