I have a DRF API that is supposed to feed a frontend Android and IOS app. The API has three types of users namely: Driver, Client and Admin.
The client is supposed to create a booking record and get assigned a driver automatically by the API. Based on availability days and times. At the moment every day (Sun, Mon, Tue, Wed, Thu, Fri) is a working day and working hours range from 7:00 am to 9:00 pm with working hours inclusive of transition being an hour. For example, booking of 9:00am will take an hour, hence finishing time will be 10:00am, all factors inclusive. Many drivers can have work at the same time. The app should give feedback to the user if they try booking already picked slots.
My problem at this time is to loop through already existing drivers from drivers table, left join them to the booking table and and assign them one by one. I was able to assign one driver a job. But when I added drivers, it became difficult. There is something I am not getting well and I do not know what it is.
Here are my models.
""" models.py """
""" Helper function to check overlapping times """
def check_time_overlap(self, fixed_start, fixed_end, new_start, new_end):
time_overlap = False
if new_start == fixed_end or new_end == fixed_start: # edge case
time_overlap = False
elif (new_start >= fixed_start and new_start <= fixed_end) or
(new_end >= fixed_start and new_end <= fixed_end):
# innner limits
time_overlap = True
elif new_start <= fixed_start and new_end >= fixed_end:
# outter limits
time_overlap = True
return time_overlap
""" Function to check overlapping bookings """
def overlapping_bookings(self):
if self.finishing_at <= self.booking_time:
raise ValidationError(
'Finishing times must be after booking times'
)
bookings = Booking.objects.filter(
booking_date=self.booking_date, driver_id=self.driver
)
if bookings.exists():
for booking in bookings:
""" Check whether date and time overlaps """
if self.check_time_overlap(
booking.booking_time, booking.finishing_at,
self.booking_time, self.finishing_at
):
""" If all drivers are allocated, raise an error
message. """
raise ValidationError(
'All our drivers are booked at: ' +
str(booking.booking_date) + ', ' + str(
booking.booking_time) + '-' +
str(booking.finishing_at))
def save(self, *args, **kwargs):
self.calc_booking_total()
self.calc_finishing_at()
self.overlapping_bookings()
super(Booking, self).save(*args, **kwargs)
From the above models, I have written a function to check overlapping times for the same booking_date, booking_time and finishing_at times. This is inspired by ALEXANDRE PINTO on >https://alexpnt.github.io/2017/07/15/django-calendar/
Below are the serializers
# BOOKING SERIALIZER
# ––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––#
class BookingSerializer(serializers.ModelSerializer):
package = serializers.SerializerMethodField()
vehicle_type = serializers.SerializerMethodField()
client_name = serializers.SerializerMethodField()
client_phone = serializers.SerializerMethodField()
class Meta:
model = Booking
# fields = '__all__'
exclude = ('driver',)
read_only_fields = (
'booking_total', 'booking_status',
'booked_at', 'finishing_at', 'client'
)
def get_package(self, obj):
return obj.service.package_name
def get_vehicle_type(self, obj):
return obj.service.vehicle_category.name
def get_client_name(self, obj):
return obj.client.name
def get_client_phone(self, obj):
return str(obj.client.phone)
""" Function to create booking based on authenticated client and available drivers """
def create(self, validated_data):
validated_data['client'] = self.context['request'].user
validated_data['driver'] = Driver.objects.get(active=True)
bookings = Booking.objects.create(**validated_data)
return bookings
Here are my server logs:
Server Log
See Question&Answers more detail:
os